|
第一步: Workbook_Open() 事件 ,打开工作薄时将sheet2的“I3”单元格添加数据有效性,方便选择序号。
Private Sub Workbook_Open()
Dim arr(), i, m, sjStr
arr = Sheet1.Range("a3").CurrentRegion
For i = 4 To UBound(arr)
If i <> "" Then
m = arr(i, 1)
End If
sjStr = sjStr & "," & m
Next
sjStr = VBA.Right(sjStr, Len(sjStr) - 1)
With Sheet2.Range("i3").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=sjStr
End With
End Sub
第二步:添加模块 “依据序号添加数据”
Sub 依据序号获取数据()
Dim arr(), s
arr = Sheet1.Range("a3").CurrentRegion
With Sheet2
For s = 4 To UBound(arr)
If arr(s, 1) = .Range("i3") Then
.Cells(4, 3) = ""
.Cells(4, 3) = arr(s, 2)
.Cells(4, 5) = ""
.Cells(4, 5) = arr(s, 3)
.Cells(4, 7) = ""
.Cells(4, 7) = arr(s, 4)
.Cells(4, 9) = ""
.Cells(4, 9) = VBA.Left(arr(s, 5), 4) & "年" & VBA.Mid(arr(s, 5), 5, 2) & "月"
.Cells(5, 3) = ""
.Cells(5, 3) = arr(s, 12)
.Cells(5, 5) = ""
.Cells(5, 5) = arr(s, 14)
.Cells(5, 8) = ""
.Cells(5, 8) = "'" & arr(s, 7)
.Cells(6, 3) = ""
.Cells(6, 3) = arr(s, 8)
End If
Next
End With
End Sub
第三步:在sheet2添加程序,只要单元格"I3"有变动,则执行模块“依据序号获取数据”
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$I$3" Then
Call 依据序号获取数据
End If
End Sub
见附件。 |
|