0

我已经尝试解决这个问题好几个星期了。我有一个包含用户表单的模板。此用户表单选择当月报告和上月报告并在当月报告中运行 vlookup。它在 B 列中运行 vlookup。问题是,代码运行 vlookup 并且不会停止,我需要它在遇到 C 列中的空白单元格时立即停止。这是代码。我在这里急需帮助。谢谢!

Private Sub CommandButton1_Click()
    Dim str1 As String
    Dim str2 As String
    Dim i As Integer

    Application.ScreenUpdating = False

    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
            str1 = ListBox1.List(i)
        End If
    Next i

    For i = 0 To ListBox2.ListCount - 1
        If ListBox2.Selected(i) = True Then
            str2 = ListBox2.List(i)
        End If
    Next i

    Workbooks(str1).Activate
    Sheets(1).Activate
    Range("B1").Select
    i = 0
    Do Until ActiveCell.Offset(i, 0).Value = "Category"
        i = i + 1
    Loop
    Range("B1").Offset(i, 0).Select
    With Range(ActiveCell.Offset(1), Cells(ActiveSheet.UsedRange.Rows.Count, 2))
        .Formula = "=VLOOKUP(C" & ActiveCell.Offset(1).Row & ",'[" & str2 & "]Sheet1'!$A$4:$B$200,2,FALSE)"
    End With

    Application.ScreenUpdating = True
End Sub
4

1 回答 1

0

也许...

Private Sub CommandButton1_Click()

    Dim lMacroSec As MsoAutomationSecurity
    Dim lCalc As XlCalculation
    Dim i As Long
    Dim j As Long

    With Application
        lMacroSec = .AutomationSecurity
        lCalc = .Calculation
        .AutomationSecurity = lMacroSec
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
            For j = 0 To ListBox2.ListCount - 1
                If ListBox2.Selected(j) = True Then
                    With Workbooks(ListBox1.List(i)).Sheets(1)
                        With .Columns("B").Find("Category").Offset(1)
                            With .Parent.Range(.Cells, .Offset(-1, 1).End(xlDown).Offset(, -1))
                                .Formula = "=VLOOKUP(C" & .Row & ",'[" & ListBox2.List(j) & "]Sheet1'!$A$4:$B$200,2,FALSE)"
                                .Calculate
                            End With
                        End With
                    End With
                End If
            Next j
        End If
    Next i

    With Application
        .AutomationSecurity = lMacroSec
        .Calculation = lCalc
        .DisplayAlerts = True
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub
于 2013-09-11T19:44:22.567 回答