|
Sub 数据接收()
Set st11 = ThisWorkbook.Worksheets("花名册")
fileopen = Application.GetOpenFilename("Excel File(*.xls),*.xls", , "请选择文件(可多选)", , True)
If IsArray(fileopen) = False Then
If fileopen = False Then
Exit Sub
End If
End If
ThisWorkbook.Activate
Application.ScreenUpdating = False '窗体实时跳动
'接收情况
file_a = 0: file_y = 0:
jl = 0
For Each file In fileopen '逐一处理所选接收文件
file_a = file_a + 1
ifopen = "0" '判别文件是否已打开
'开始逐个文件的处理————————————————————————
For i = 1 To Len(file) '获取文件名,不含路径
If Left(Right(file, i), 1) <> "\" Then
bm = Right(file, i)
Else
Exit For
End If
Next
If InStr(bm, "[") > 0 Or InStr(bm, "]") > 0 Then
cause = "文件名中含有方括号,请删之..."
structure = False
End If
For Each sht In Workbooks '判断文件是否打开
If sht.Name = bm Then 'sht.name包含扩展名 , workbooks()的函数和操作系统是否隐藏扩展名一致
ifopen = "1"
Exit For
End If
Next
If ifopen = "0" Then '上报表未打开则将其打开
Workbooks.Open (file)
End If
Set nt11 = Workbooks(bm).Sheets(1)
Set nt1r = nt11.Range("A65536").End(xlUp)
nt1row = nt1r.Row
rs = 0
For i = 2 To nt1row
If nt11.Cells(i, 29) > 0 And (nt11.Cells(i, 2) = "公务员" Or nt11.Cells(i, 2) = "机关技术工人") Then
rs = rs + 1
If nt11.Cells(i, 1) = nt11.Cells(i + 1, 1) And nt11.Cells(i + 1, 29) > 0 Then
i = i + 1
End If
End If
Next
If rs > 18 Then
For j = 1 To rs - 18
Worksheets("花名册").Rows(9).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Next
End If
For i = 2 To nt1row
If nt11.Cells(i, 29) > 0 And (nt11.Cells(i, 2) = "公务员" Or nt11.Cells(i, 2) = "机关技术工人") And Trim(nt11.Cells(i, 14)) <> "" Then
For j = 6 To rs + 5
If st11.Cells(j, 1) = "" Then
Exit For
End If
Next
st11.Cells(j, 1) = "=row()-5"
If nt11.Cells(i, 1) = nt11.Cells(i + 1, 1) And nt11.Cells(i, 29) > 0 Then
If nt11.Cells(i, 14) > nt11.Cells(i + 1, 14) Then
st11.Cells(j, 2) = nt11.Cells(i, 1)
st11.Cells(j, 4) = nt11.Cells(i, 14)
st11.Cells(j, 5) = nt11.Cells(i, 15)
st11.Cells(j, 6) = nt11.Cells(i, 22)
st11.Cells(j, 7) = nt11.Cells(i, 29)
i = i + 1
Else
st11.Cells(j, 2) = nt11.Cells(i + 1, 1)
st11.Cells(j, 4) = nt11.Cells(i + 1, 14)
st11.Cells(j, 5) = nt11.Cells(i + 1, 15)
st11.Cells(j, 6) = nt11.Cells(i + 1, 22)
st11.Cells(j, 7) = nt11.Cells(i + 1, 29)
i = i + 1
End If
Else
st11.Cells(j, 2) = nt11.Cells(i, 1)
st11.Cells(j, 4) = nt11.Cells(i, 14)
st11.Cells(j, 5) = nt11.Cells(i, 15)
st11.Cells(j, 6) = nt11.Cells(i, 22)
st11.Cells(j, 7) = nt11.Cells(i, 29)
End If
End If
Next
Next
Workbooks(bm).Saved = True '退出时不提示保存
Application.CutCopyMode = False '清除剪贴版
Workbooks(bm).Close
ThisWorkbook.Activate
Application.ScreenUpdating = False '窗体实时跳动
End Sub
在文件已经打开的情况接收数据没有问题,文件没有打开接收就提示“越界”,请高手斧正一下。
|
|