| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3682 人关注过本帖
标题:VFP中有类似VLOOKUP功能的函数吗
只看楼主 加入收藏
依风听暮
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-12-2
结帖率:0
收藏
已结贴  问题点数:10 回复次数:11 
VFP中有类似VLOOKUP功能的函数吗
EXCEL中VLOOKUP函数的功能非常强大,VPF中有这类的函数吗
搜索更多相关主题的帖子: EXCEL 
2016-12-08 22:59
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9844
专家分:27213
注 册:2012-2-5
收藏
得分:2 
这个函数的功能是什么

坚守VFP最后的阵地
2016-12-08 23:09
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:0 
VLOOKUP(查找的值,查找区域,返回列的位置,查找的方式)
2016-12-09 07:12
zyxxzhyg
Rank: 3Rank: 3
来 自:江西
等 级:论坛游侠
威 望:5
帖 子:59
专家分:134
注 册:2014-6-26
收藏
得分:2 
没有就自己编一个,很容易的
2016-12-09 08:17
星光悠蓝
Rank: 9Rank: 9Rank: 9
来 自:山水甲天下
等 级:贵宾
威 望:52
帖 子:525
专家分:1278
注 册:2010-1-11
收藏
得分:2 
设计数据库时定义约束
2016-12-09 08:59
mywisdom88
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:191
帖 子:3147
专家分:8408
注 册:2015-3-25
收藏
得分:2 
*!* 表a(序号,查找,返回,区域1,区域2,区域3,区域4)
*!* int(rand()*100)+1 &&产生1到100的随机整数
create cursor 表a(序号 i,查找 c(10),返回 c(20),区域1 c(20),区域2 c(20),区域3 c(20),区域4 c(20))
local ii
for ii=1 to 100
insert into 表a(序号,查找,返回,区域1,区域2,区域3,区域4) ;
 values(ii,tran(int(rand()*100)+1),'',tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1))
endfor

LOCAL lookup_value,start_row_num,start_col_num,end_row_num,end_col_num,col_index_num,range_lookup,table_name

=vlookup(2,1,4,1000,7,1,0,'表a')


BROWSE

*!* 表a(序号,查找,返回,区域1,区域2,区域3,区域4),“序号,查找,返回”等3个字段不能少,其他的字段“区域1,区域2,区域3,区域4”可以随便增加、修改

FUNCTION vlookup(lookup_value,start_row_num,start_col_num,end_row_num,end_col_num,col_index_num,range_lookup,table_name)
LOCAL nReccount,cFcount,lcAlias
nReccount = 0
cFcount = 0
lcAlias=ALIAS() &&保存现场

IF TYPE("table_name") = "C" OR !EMPTY(table_name)
   IF !USED(table_name)
      MESSAGEBOX("表没有打开" + table_name,16,"提示")
      RETURN .f.
   ELSE
      nReccount = RECCOUNT(table_name)
      nFcount = FCOUNT(table_name)
   ENDIF
ELSE
   MESSAGEBOX("表名参数不正确" + TRANSFORM(table_name),16,"提示")
   RETURN .f.
ENDIF

IF TYPE("start_row_num") # "N" OR !BETWEEN(start_row_num,1,nReccount)
   start_row_num = 1
ENDIF
IF TYPE("start_col_num") # "N" OR !BETWEEN(start_col_num,1,nFcount)
   start_col_num = 1
ENDIF
IF TYPE("end_row_num") # "N" OR !BETWEEN(end_row_num,1,nReccount)
   end_row_num = nReccount &&记录数
ENDIF
IF TYPE("start_col_num") # "N" OR !BETWEEN(end_col_num,1,nFcount)
   end_col_num = nFcount &&字段数
ENDIF

IF TYPE("col_index_num") # "N"
   col_index_num = 1
ENDIF

IF TYPE("range_lookup") = "N" AND (range_lookup = 0 OR range_lookup = 1)
ELSE
   range_lookup = 0
ENDIF

LOCAL c查找,c返回,c查找1,a字段[nFcount],tmp_table_name

tmp_table_name = 'tmp_' + table_name

SELECT (table_name)
FOR ii = 1 TO nFcount
    a字段[ii] = Field(ii)
ENDFOR
   
SELECT 序号,查找,返回 FROM (table_name) INTO CURSOR (tmp_table_name) READWRITE

SELECT (tmp_table_name)
GO TOP
SCAN
  c查找1 = ALLTRIM(TRANSFORM(查找))
  c查找=""
  FOR ii = start_col_num TO end_col_num
      c查找 = IIF(EMPTY(c查找), a字段[ii] + "=TRANSFORM(" + c查找1 + ")",c查找 + " or " + a字段[ii] + "=TRANSFORM(" + c查找1 + ")")
  ENDFOR
  c查找 = "LOCATE FOR (" + c查找 + ") and BETWEEN(序号,start_row_num,end_row_num)"

  SELECT (table_name)

  &c查找
  IF FOUND()
     c返回 = TRANSFORM(&a字段[col_index_num])
  ELSE
     c返回 =""
  ENDIF
  
  SELECT (tmp_table_name)
  REPLACE 返回 WITH c返回
  
ENDSCAN
UPDATE t1 SET t1.返回=t2.返回 from (table_name) as t1,(tmp_table_name) as t2 WHERE t1.序号 = t2.序号
*!* 恢复现场
IF !EMPTY(lcAlias)
   SELECT (lcAlias)
ENDIF

RETURN .t.

ENDFUNC
收到的鲜花
  • 梦幻倩影2016-12-10 10:11 送鲜花  10朵   附言:我很赞同
2016-12-09 15:40
hyswcyh
Rank: 8Rank: 8
等 级:贵宾
威 望:20
帖 子:391
专家分:896
注 册:2004-11-23
收藏
得分:2 
建议给楼上加分!哈哈。辛苦!
2016-12-09 16:15
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
收藏
得分:2 
这么长代码?
2016-12-10 07:47
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1744
专家分:3333
注 册:2012-2-29
收藏
得分:2 
高手!学习,谢谢!
2016-12-11 08:26
mywisdom88
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:191
帖 子:3147
专家分:8408
注 册:2015-3-25
收藏
得分:0 
*!* 表a(序号,查找,返回,区域1,区域2,区域3,区域4)
*!* int(rand()*100)+1 &&产生1到100的随机整数
create cursor 表a(序号 i,查找 c(10),返回 c(20),区域1 c(20),区域2 c(20),区域3 c(20),区域4 c(20),区域5 c(20),区域6 c(20),区域7 c(20))
local ii
for ii=1 to 10
insert into 表a(序号,查找,返回,区域1,区域2,区域3,区域4,区域5,区域6,区域7) ;
 values(ii,tran(int(rand()*100)+1),'',tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1),tran(int(rand()*100)+1))
endfor

LOCAL lookup_value,start_row_num,start_col_num,end_row_num,end_col_num,col_index_num,range_lookup,table_name

=vlookup(2,1,4,100,9,1,0,'表a')


BROWSE

*!* 表a(序号,查找,返回,区域1,区域2,区域3,区域4),“序号,查找,返回”等3个字段不能少,其他的字段“区域1,区域2,区域3,区域4”可以随便增加、修改
*!* 如果 查找的值含有 ' 就会报错。不知道怎么处理。。
FUNCTION vlookup(lookup_value,start_row_num,start_col_num,end_row_num,end_col_num,col_index_num,range_lookup,table_name)
LOCAL nReccount,cFcount,lcAlias
nReccount = 0
cFcount = 0
lcAlias=ALIAS() &&保存现场

IF TYPE("table_name") = "C" OR !EMPTY(table_name)
   IF !USED(table_name)
      MESSAGEBOX("表没有打开" + table_name,16,"提示")
      RETURN .f.
   ELSE
      nReccount = RECCOUNT(table_name)
      nFcount = FCOUNT(table_name)
   ENDIF
ELSE
   MESSAGEBOX("表名参数不正确" + TRANSFORM(table_name),16,"提示")
   RETURN .f.
ENDIF

IF TYPE("start_row_num") # "N" OR !BETWEEN(start_row_num,1,nReccount)
   start_row_num = 1
ENDIF
IF TYPE("start_col_num") # "N" OR !BETWEEN(start_col_num,1,nFcount)
   start_col_num = 1
ENDIF
IF TYPE("end_row_num") # "N" OR !BETWEEN(end_row_num,1,nReccount)
   end_row_num = nReccount &&记录数
ENDIF
IF TYPE("start_col_num") # "N" OR !BETWEEN(end_col_num,1,nFcount)
   end_col_num = nFcount &&字段数
ENDIF

IF TYPE("col_index_num") # "N"
   col_index_num = 1
ENDIF

IF TYPE("range_lookup") = "N" AND (range_lookup = 0 OR range_lookup = 1)
ELSE
   range_lookup = 0
ENDIF

LOCAL c查找,c返回,c查找1,a字段[nFcount],tmp_table_name

tmp_table_name = 'tmp_' + table_name

SELECT (table_name)
FOR ii = 1 TO nFcount
    a字段[ii] = Field(ii)
ENDFOR
   
SELECT 序号,查找,返回 FROM (table_name) INTO CURSOR (tmp_table_name) READWRITE

SELECT (tmp_table_name)
GO TOP
SCAN
  c查找1 = "'" + ALLTRIM(TRANSFORM(查找)) + "'"
  c查找=""
  FOR ii = start_col_num TO end_col_num
     IF range_lookup = 0
        c查找 = IIF(EMPTY(c查找), "allt(" + a字段[ii] + ") == tran(" + c查找1 + ")",c查找 + " or " + "allt(" + a字段[ii] + ") == tran(" + c查找1 + ")")
     ELSE
        c查找 = IIF(EMPTY(c查找), "tran(" + c查找1 + ") $ allt(" + a字段[ii] + ")" ,c查找 + " or " + "tran(" + c查找1 + ") $ allt(" + a字段[ii] + ")")
     ENDIF
  ENDFOR
  c查找 = "LOCATE FOR (" + c查找 + ") and betw(序号,start_row_num,end_row_num)"

  SELECT (table_name)

  &c查找 &&宏查找
  IF FOUND()
     c返回 = TRANSFORM(&a字段[col_index_num])
  ELSE
     c返回 =""
  ENDIF
  
  SELECT (tmp_table_name)
  REPLACE 返回 WITH c返回
  
ENDSCAN
UPDATE t1 SET t1.返回=t2.返回 from (table_name) as t1,(tmp_table_name) as t2 WHERE t1.序号 = t2.序号
*!* 恢复现场
IF !EMPTY(lcAlias)
   SELECT (lcAlias)
ENDIF

RETURN .t.

ENDFUNC
2016-12-11 11:56
快速回复:VFP中有类似VLOOKUP功能的函数吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027938 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved