获取循环以停止在VBA中打印

时间:2017-01-09 18:13:42

标签: excel-vba vba excel

我对VBA很陌生,而且几年前java和C ++都很生气,所以任何帮助都会受到赞赏。在概述中,我试图让这个程序找到一个数字并将月份解释为外部工作簿上的工作表编号,然后根据第一个数字返回一个值。我有它这样做,但我不能让循环结束在第一个空单元格。它可能是lookFor中的范围或循环语句的问题;我不确定。有什么想法吗?这是代码

Sub Go()

  Range("B6").Select
  Do Until IsEmpty(ActiveCell)

    Dim lookFor As Range
    Dim srchRange As Range
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim book2Name As String
    book2Name = "Cash_Office_Long_Short_Log_FYE18.xlsx"
    Dim book2NamePath As String
    book2NamePath = ThisWorkbook.Path & "\" & book2Name
    Dim refMonth As Integer
    refMonth = Month(Cells(6, 5)) + 1

    Debug.Print "refMonth="; refMonth

    Set book1 = ThisWorkbook

    If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)
    Set book2 = Workbooks(book2Name)

    Set lookFor = book1.Sheets(1).Range("B6:B800")
    Set srchRange = book2.Sheets(refMonth).Range("A1:B800")

    lookFor.Offset(0, -1).Value = Application.VLookup(lookFor, srchRange, 2, False)

    ActiveCell.Offset(1, 0).Select

  Loop

End Sub

Function IsOpen(strWkbNm As String) As Boolean

  On Error Resume Next
  Dim wBook As Workbook
  Set wBook = Workbooks(strWkbNm)
  If wBook Is Nothing Then
    IsOpen = False
    Set wBook = Nothing
    On Error GoTo 0
  Else
    IsOpen = True
    Set wBook = Nothing
    On Error GoTo 0
  End If

End Function

1 个答案:

答案 0 :(得分:0)

您的循环问题是由您使用ActiveCell引起的。一旦打开新工作簿,活动单元格就会成为该工作簿中的单元格,这会导致循环变得非常混乱。

有关如何避免这些问题的建议,请参阅How to avoid using Select in Excel VBA macros

与此同时,我已经重构了你的代码(希望)做你想做的事情:

'Change name of sub - "Go" isn't valid
Sub RunMe()
    Dim controlRow As Long
    Dim lookFor As Range
    Dim srchRange As Range
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim book2Name As String
    Dim book2NamePath As String
    Dim refMonth As Integer

    Set book1 = ThisWorkbook
    With book1.Worksheets(1) ' I have assumed that the active sheet was worksheets(1)
        'Set row pointer to start at row 6
        controlRow = 6
        Do Until IsEmpty(.Cells(controlRow, "B"))

            book2Name = "Cash_Office_Long_Short_Log_FYE18.xlsx"
            book2NamePath = ThisWorkbook.Path & "\" & book2Name
            refMonth = Month(.Cells(6, 5)) + 1  'Should this be controlRow rather than 6?

            Debug.Print "refMonth="; refMonth

            If Not IsOpen(book2Name) Then Workbooks.Open book2NamePath
            Set book2 = Workbooks(book2Name)

            Set lookFor = .Cells(controlRow, "B") ' Assumed that you are searching for value on this row
            Set srchRange = book2.Worksheets(refMonth).Range("A1:B800")

            lookFor.Offset(0, -1).Value = Application.VLookup(lookFor.Value, srchRange, 2, False)

            'Point to next row
            controlRow = controlRow + 1
        Loop
    End With

End Sub

Function IsOpen(strWkbNm As String) As Boolean
    On Error Resume Next
    Dim wBook As Workbook
    Set wBook = Workbooks(strWkbNm)
    IsOpen = Not (wBook Is Nothing)
    Set wBook = Nothing
    On Error GoTo 0
End Function