运行脚本时循环不在EOF停止

时间:2018-04-02 12:54:43

标签: excel vba excel-vba

我在测试自动化应用程序中编写脚本以循环访问Excel工作表以获取登录信息。我在达到EOF后让循环停止有些困难。

excel工作簿有一个" master"单击命令按钮时使用VBA将登录信息填充到其他工作表的工作表。这曾经是一个手动过程,但是主表已经解决了这个问题。

每当我使用此命令按钮时,循环都不会停止在EOF并且我得到运行时错误。问题是,每当我手动删除应用程序使用的数据下面的任何空白行时,循环就会按预期工作。这让我觉得我用于命令按钮的代码存在问题。这是代码,任何建议将不胜感激:

Sub PopulateAgents()

Dim wb As Workbook
Dim wsMaster As Worksheet
Dim rMasterData As Range
Dim aTransferParams() As Variant
Dim i As Long
Dim lMaxCol As Long
Dim aCell As Range, Rng As Range
Dim SearchString As String

Set wb = ActiveWorkbook
Set wsMaster = wb.Sheets("TESTRUN")

ReDim aTransferParams(1 To 51, 1 To 2)

Set aTransferParams(1, 1) = wb.Sheets("AL"):    aTransferParams(1, 2) = "H"
Set aTransferParams(2, 1) = wb.Sheets("AK"):    aTransferParams(2, 2) = "I"
Set aTransferParams(3, 1) = wb.Sheets("AZ"):    aTransferParams(3, 2) = "J"
Set aTransferParams(4, 1) = wb.Sheets("AR"):    aTransferParams(4, 2) = "K"
Set aTransferParams(5, 1) = wb.Sheets("CA"):    aTransferParams(5, 2) = "L"
Set aTransferParams(6, 1) = wb.Sheets("CO"):    aTransferParams(6, 2) = "M"
Set aTransferParams(7, 1) = wb.Sheets("CT"):    aTransferParams(7, 2) = "N"
Set aTransferParams(8, 1) = wb.Sheets("DC"):    aTransferParams(8, 2) = "O"
Set aTransferParams(9, 1) = wb.Sheets("DE"):    aTransferParams(9, 2) = "P"
Set aTransferParams(10, 1) = wb.Sheets("FL"):    aTransferParams(10, 2) = "Q"
Set aTransferParams(11, 1) = wb.Sheets("GA"):    aTransferParams(11, 2) = "R"
Set aTransferParams(12, 1) = wb.Sheets("HI"):    aTransferParams(12, 2) = "S"
Set aTransferParams(13, 1) = wb.Sheets("ID"):    aTransferParams(13, 2) = "T"
Set aTransferParams(14, 1) = wb.Sheets("IL"):    aTransferParams(14, 2) = "U"
Set aTransferParams(15, 1) = wb.Sheets("IN"):    aTransferParams(15, 2) = "V"
Set aTransferParams(16, 1) = wb.Sheets("IA"):    aTransferParams(16, 2) = "W"
Set aTransferParams(17, 1) = wb.Sheets("KS"):    aTransferParams(17, 2) = "X"
Set aTransferParams(18, 1) = wb.Sheets("KY"):    aTransferParams(18, 2) = "Y"
Set aTransferParams(19, 1) = wb.Sheets("LA"):    aTransferParams(19, 2) = "Z"
Set aTransferParams(20, 1) = wb.Sheets("ME"):    aTransferParams(20, 2) = "AA"
Set aTransferParams(21, 1) = wb.Sheets("MD"):    aTransferParams(21, 2) = "AB"
Set aTransferParams(22, 1) = wb.Sheets("MA"):    aTransferParams(22, 2) = "AC"
Set aTransferParams(23, 1) = wb.Sheets("MI"):    aTransferParams(23, 2) = "AD"
Set aTransferParams(24, 1) = wb.Sheets("MN"):    aTransferParams(24, 2) = "AE"
Set aTransferParams(25, 1) = wb.Sheets("MS"):    aTransferParams(25, 2) = "AF"
Set aTransferParams(26, 1) = wb.Sheets("MO"):    aTransferParams(26, 2) = "AG"
Set aTransferParams(27, 1) = wb.Sheets("MT"):    aTransferParams(27, 2) = "AH"
Set aTransferParams(28, 1) = wb.Sheets("NE"):    aTransferParams(28, 2) = "AI"
Set aTransferParams(29, 1) = wb.Sheets("NV"):    aTransferParams(29, 2) = "AJ"
Set aTransferParams(30, 1) = wb.Sheets("NH"):    aTransferParams(30, 2) = "AK"
Set aTransferParams(31, 1) = wb.Sheets("NJ"):    aTransferParams(31, 2) = "AL"
Set aTransferParams(32, 1) = wb.Sheets("NM"):    aTransferParams(32, 2) = "AM"
Set aTransferParams(33, 1) = wb.Sheets("NY"):    aTransferParams(33, 2) = "AN"
Set aTransferParams(34, 1) = wb.Sheets("NC"):    aTransferParams(34, 2) = "AO"
Set aTransferParams(35, 1) = wb.Sheets("ND"):    aTransferParams(35, 2) = "AP"
Set aTransferParams(36, 1) = wb.Sheets("OH"):    aTransferParams(36, 2) = "AQ"
Set aTransferParams(37, 1) = wb.Sheets("OK"):    aTransferParams(37, 2) = "AR"
Set aTransferParams(38, 1) = wb.Sheets("OR"):    aTransferParams(38, 2) = "AS"
Set aTransferParams(39, 1) = wb.Sheets("PA"):    aTransferParams(39, 2) = "AT"
Set aTransferParams(40, 1) = wb.Sheets("RI"):    aTransferParams(40, 2) = "AU"
Set aTransferParams(41, 1) = wb.Sheets("SC"):    aTransferParams(41, 2) = "AV"
Set aTransferParams(42, 1) = wb.Sheets("SD"):    aTransferParams(42, 2) = "AW"
Set aTransferParams(43, 1) = wb.Sheets("TN"):    aTransferParams(43, 2) = "AX"
Set aTransferParams(44, 1) = wb.Sheets("TX"):    aTransferParams(44, 2) = "AY"
Set aTransferParams(45, 1) = wb.Sheets("UT"):    aTransferParams(45, 2) = "AZ"
Set aTransferParams(46, 1) = wb.Sheets("VT"):    aTransferParams(46, 2) = "BA"
Set aTransferParams(47, 1) = wb.Sheets("VA"):    aTransferParams(47, 2) = "BB"
Set aTransferParams(48, 1) = wb.Sheets("WA"):    aTransferParams(48, 2) = "BC"
Set aTransferParams(49, 1) = wb.Sheets("WV"):    aTransferParams(49, 2) = "BD"
Set aTransferParams(50, 1) = wb.Sheets("WI"):    aTransferParams(50, 2) = "BE"
Set aTransferParams(51, 1) = wb.Sheets("WY"):    aTransferParams(51, 2) = "BF"

For i = LBound(aTransferParams, 1) To UBound(aTransferParams, 1)
    If wsMaster.Columns(aTransferParams(i, 2)).Column > lMaxCol Then lMaxCol = wsMaster.Columns(aTransferParams(i, 2)).Column
Next i

Set rMasterData = wsMaster.Range(wsMaster.Cells(1, "A"), wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp)).Resize(, lMaxCol)

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With

For i = LBound(aTransferParams, 1) To UBound(aTransferParams, 1)

    aTransferParams(i, 1).Range("A1").CurrentRegion.Offset(1).ClearContents

    rMasterData.AutoFilter wsMaster.Columns(aTransferParams(i, 2)).Column, "X"

    rMasterData.Offset(1).Resize(, 6).Copy aTransferParams(i, 1).Range("A2")
    aTransferParams(i, 1).Columns.AutoFit

    rMasterData.AutoFilter
Next i

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub

0 个答案:

没有答案