如何引用特定工作表与使用活动工作表?

时间:2018-10-28 08:35:25

标签: excel vba

我正在尝试从另一个工作表而不是ActiveSheet列出记录。有没有办法做到这一点?谢谢。

Public Sub CommandButton4_Click()
    ListBox1.ColumnHeads = True
    ListBox1.ColumnCount = 7
    ListBox1.RowSource = "A2:G1000"

    'Count the listbox rows when populated

    With ActiveSheet             
        lastrow = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Rows.Count
        TextBox3 = lastrow
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

赞:

Public Sub CommandButton4_Click()
ListBox1.ColumnHeads = True
ListBox1.ColumnCount = 7
ListBox1.RowSource = "A2:G1000"

Dim ws As Worksheet
Set ws = Sheets("Sheet1")

'Count the listbox rows when populated

With ws             
    lastrow = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Rows.Count
    TextBox3 = lastrow
End With
End Sub