从PickupFormGenerator.xlsm中的EXCEL命令按钮激活宏会打开另外两个EXCEL文件:Donation Data.xlsm和Company Data.xlsx。我需要先确定捐赠数据中有多少行。但是,即使在“激活”工作簿/工作表 - “DonationDataQuery”之后,代码也默认为PickupFormGenerator.xlsm。有人可以帮忙吗?
Private Sub CommandButton21_Click()
Application.ScreenUpdating = False
Application.CutCopyMode = False 'clear any thing on clipboard to maximize available memory
'Open Company Data.xlsx and then Donation Data.xlsm
Dim strFilePath As String
strFilePath = "C:\Users\dads\Downloads\"
Dim strFileName As String
strFileName = "Company Data.xlsx"
Workbooks.Open Filename:=strFilePath & Dir$(strFilePath & strFileName), ReadOnly:=False
Workbooks("Company Data.xlsx").Activate
Worksheets("Company_Data").Select
strFileName = "Donation Data.xlsm"
Workbooks.Open Filename:=strFilePath & Dir$(strFilePath & strFileName), ReadOnly:=False
Workbooks("Donation Data.xlsm").Activate
Worksheets("DonationDataQuery").Select
'With Donation Data.xlsm open, determine how many Donations are in the collection; the number of rows will be LastRow-1 due to headers...
Dim Lastrow, NumPickups As Integer
Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
NumPickups = Lastrow - 1
'Now identify the Customer ID numbers for each of the Donation Pickups in [Donation_Data] and store in CusArray...max pickups in one day is 35
Dim i As Integer, CusArray(35) As Variant
Erase CusArray
For i = 1 To NumPickups 'note Row 1 is the Headers
CusArray(i) = Cells(i + 1, 1).Value 'identify the Customer ID number starts in row 2, in first colulmn of Donation_Data.xlsx
Next i
Debug.Print "NumPickups is " & NumPickups '=2
Debug.Print "CusArray(1) is " & CusArray(1) '=blank
Debug.Print "CusArray(2) is " & CusArray(2) '=What Date would you like to generate Picku Forms For?
Debug.Print "CusArray(3) is " & CusArray(3) '=blank
End Sub
在下面实施了相同结果的建议......现在是代码:
Workbooks.Open Filename:="C:\Users\dads\Downloads\Donation Data.xlsm"
Workbooks.Open Filename:="C:\Users\dads\Downloads\Company Data.xlsx"
'With Donation Data.xlsm open, determine how many Donations are in the collection; the number of rows will be LastRow-1 due to headers...
Windows("Donation Data.xlsm").Activate
Dim Lastrow, NumPickups As Integer
Lastrow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
NumPickups = Lastrow - 1
'Now identify the Customer ID numbers for each of the Donation Pickups in [Donation_Data] and store in CusArray...max pickups in one day is 35
Dim i As Integer, CusArray(35) As Variant
Erase CusArray
For i = 1 To NumPickups 'note Row 1 is the Headers
CusArray(i) = Cells(i + 1, 1).Value 'identify the Customer ID number starts in row 2, in first colulmn of Donation_Data.xlsx
Next i
Debug.Print "NumPickups is " & NumPickups '=2
Debug.Print "CusArray(1) is " & CusArray(1) '=blank
Debug.Print "CusArray(2) is " & CusArray(2) '=What Date would you like to generate Picku Forms For?
Debug.Print "CusArray(3) is " & CusArray(3) '=blank