标签:
作者:iamlaosong
用VBA对Excel文件进行处理的时候,关键字段的列号编程时往往是不知道的,需要通过参数设定才能知道,因此,我们编程的时候,就不能用这样的语句取有效行数:
lineno = [B65536].End(xlUp).Row
上述语句中的列名“B”如果是变量,可以用字符串连接的方式实现,即:
pos_ems = "C"
lineno = Range(pos_ems & "65536").End(xlUp).Row
如果给的是列号,则用下列语句:
pos_ems = 3
lineno = Cells(65536, pos_ems).End(xlUp).Row
上述语句中的变量pos_ems可以读取单元格的值,以便面对不同文件时随时设置:
pos_fst = Cells(2, 7)
pos_ems = Cells(3, 7)
pos_sav = Cells(4, 7)
lineno = [B65536].End(xlUp).Row '行数,文件数量
For unit_num = 5 To lineno '文件循环
datfile = Cells(unit_num, 2) '文件名称
datFullName = ThisWorkbook.Path & "\" & datfile
If Dir(datFullName, vbNormal) <> vbNullString Then
Workbooks.Open Filename:=datFullName '打开订单文件
If Application.Version >= "12.0" And ActiveWorkbook.FileFormat = 51 Then
maxrow = Cells(1048576, pos_ems).End(xlUp).Row
Else
maxrow = Cells(65536, pos_ems).End(xlUp).Row
End If
Else
MsgBox "数据文件不存在!", vbOKOnly, "iamlaosong"
Exit Sub
End If
。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/iamlaosong/article/details/46983703