0

I am trying list records from another sheet instead of ActiveSheet. Is there a way to achieve this? Thanks.

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
Alex P
  • 12,249
  • 5
  • 51
  • 70
LinuxPingu
  • 89
  • 3
  • 13
  • I tried using the code With Worksheets ("Sheet 2").Select – LinuxPingu Oct 28 '18 at 08:38
  • 1
    Cf. [How to avoid using `Select` in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba/10717999#10717999) and try to fully qualify your range references :-) – T.M. Oct 28 '18 at 09:06

1 Answers1

1

Like this:

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
Alex P
  • 12,249
  • 5
  • 51
  • 70
Lambik
  • 520
  • 1
  • 6
  • 13