Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50049

[SOLVED] Variable not defined error

$
0
0
Hi,
I copied code from another sheet into my own and I keep getting a variable not defined on "Listbox1" I'm not sure why. Can someone help me? Here is the code:

Code:

Option Explicit

Dim rg As Range
Private Sub Worksheet_Activate()
  Set rg = Sheets(1).Range("A6:A25")      '<< Update Here : Range with list names
  ' OR
  'Set rg = Sheets(1).Range("A6:A" & Sheets(1).Range("A" & Rows.Count).End(xlUp).Row)    'dynamic range
End Sub

Sub PopulateListBox()
  Dim ar, i As Integer
 
  Application.ScreenUpdating = False
 
  If rg Is Nothing Then Worksheet_Activate
 
  rg.EntireRow.Hidden = False      'By default all series are visible
  ar = rg.Value
  With ListBox1
      .List = ar
      For i = 0 To .ListCount - 1
          .Selected(i) = True
      Next i
  End With

  Application.DisplayAlerts = True
End Sub

Private Sub ListBox_Mouseup(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'When listbox is clicked, refresh series
  Dim i As Integer
  Application.ScreenUpdating = False
  With ListBox1
      For i = 0 To .ListCount - 1
        rg.Offset(i).EntireRow.Hidden = Not .Selected(i)
        Debug.Print i; .Selected(i)
      Next i
  End With
  Application.ScreenUpdating = True
End Sub


Viewing all articles
Browse latest Browse all 50049

Trending Articles