如何引用code_click到特定的工作表

时间:2019-04-16 12:06:42

标签: excel vba

如何在不激活以下代码中的工作表的情况下引用工作表(“客户”)?

Application.ScreenUpdating无法完成工作,因为仍然有令人讨厌的闪烁。

当我取消注释'Worksheets(“ Customers”)。Activate

时,代码运行良好。

我想在Worksheets(“ Dashboard”)打开时执行所有步骤。

我尝试了“ With ... End With”,但是没有运气。还引用了Worksheets(“ Customers”)。Cells .....等无法正常工作。就像代码跳过代码一样,直接转到 “

 If SearchRow = 2 Then
    MsgBox "Customer Not Found", vbExclamation
    Exit Sub
End If
"

我还有另一个类似代码的问题,由于某种原因,它不能正常工作。

只想提及我正在将这段代码与用户表单一起使用,然后单击按钮。

任何帮助将不胜感激。

Private Sub srCus_Click()

Application.ScreenUpdating = False
Dim RowNum As Long
Dim SearchRow As Long

RowNum = 2
SearchRow = 2

Worksheets("SearchCus").Range("A2:I100").ClearContents

'Worksheets("Customers").Activate

Do Until Worksheets("Customers").Cells(RowNum, 1).Value = ""
    If InStr(1, Cells(RowNum, 3).Value, CusDB.Value, vbTextCompare) > 0 Then
        Worksheets("SearchCus").Cells(SearchRow, 1).Value = Cells(RowNum, 1).Value
        Worksheets("SearchCus").Cells(SearchRow, 2).Value = Cells(RowNum, 2).Value
        Worksheets("SearchCus").Cells(SearchRow, 3).Value = Cells(RowNum, 3).Value
        Worksheets("SearchCus").Cells(SearchRow, 4).Value = Cells(RowNum, 4).Value
        Worksheets("SearchCus").Cells(SearchRow, 5).Value = Cells(RowNum, 5).Value
        Worksheets("SearchCus").Cells(SearchRow, 6).Value = Cells(RowNum, 6).Value
        Worksheets("SearchCus").Cells(SearchRow, 7).Value = Cells(RowNum, 7).Value
        Worksheets("SearchCus").Cells(SearchRow, 8).Value = Cells(RowNum, 8).Value
        Worksheets("SearchCus").Cells(SearchRow, 9).Value = Cells(RowNum, 9).Value
        SearchRow = SearchRow + 1
    End If
    RowNum = RowNum + 1
Loop

If SearchRow = 2 Then
    MsgBox "Customer Not Found", vbExclamation
    Exit Sub
End If
ResultsDB.RowSource = "SearchResults"

'ThisWorkbook.Worksheets("Dashboard").Activate

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

感谢SJR,您对所有这些Cells(RowNum,1)的引用是正确的,我总是在InStr行中跳过一个。感谢您的帮助和所有建议。在下面查看了代码。

Private Sub srCus_Click()

Application.ScreenUpdating = False
Dim RowNum As Long
Dim SearchRow As Long

RowNum = 2
SearchRow = 2

Worksheets("SearchCus").Range("A2:I100").ClearContents

'Worksheets("Customers").Activate

Do Until Worksheets("Customers").Cells(RowNum, 1).Value = ""
    If InStr(1, Worksheets("Customers").Cells(RowNum, 3).Value, CusDB.Value, vbTextCompare) > 0 Then
        Worksheets("SearchCus").Cells(SearchRow, 1).Value = Worksheets("Customers").Cells(RowNum, 1).Value
        Worksheets("SearchCus").Cells(SearchRow, 2).Value = Worksheets("Customers").Cells(RowNum, 2).Value
        Worksheets("SearchCus").Cells(SearchRow, 3).Value = Worksheets("Customers").Cells(RowNum, 3).Value
        Worksheets("SearchCus").Cells(SearchRow, 4).Value = Worksheets("Customers").Cells(RowNum, 4).Value
        Worksheets("SearchCus").Cells(SearchRow, 5).Value = Worksheets("Customers").Cells(RowNum, 5).Value
        Worksheets("SearchCus").Cells(SearchRow, 6).Value = Worksheets("Customers").Cells(RowNum, 6).Value
        Worksheets("SearchCus").Cells(SearchRow, 7).Value = Worksheets("Customers").Cells(RowNum, 7).Value
        Worksheets("SearchCus").Cells(SearchRow, 8).Value = Worksheets("Customers").Cells(RowNum, 8).Value
        Worksheets("SearchCus").Cells(SearchRow, 9).Value = Worksheets("Customers").Cells(RowNum, 9).Value
        SearchRow = SearchRow + 1
    End If
    RowNum = RowNum + 1
Loop

If SearchRow = 2 Then
    MsgBox "Customer Not Found", vbExclamation
    Exit Sub
End If
ResultsDB.RowSource = "SearchResults"

'ThisWorkbook.Worksheets("Dashboard").Activate

Application.ScreenUpdating = True

End Sub