Hey
I am new to VBA and have mainly built this thanks to ChatGPT, forums and gluing code together, I am having a problem with this code in that when I first open my document and this is activated I get a 'Run-time error '9': Subscript out of range' BUT if I go into the VBA editor in Excel and click reset this code works exactly how it is meant to, this is the code
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Dim code As String
code = "Private Sub Worksheet_Change(ByVal Target As Range)" & vbCrLf & _
" If Not Intersect(Target, Me.Range(""D2"")) Is Nothing Then" & vbCrLf & _
" HideRowsBasedOnCriteria" & vbCrLf & _
" End If" & vbCrLf & _
"End Sub"
With ThisWorkbook.VBProject.VBComponents(Sh.CodeName).codeModule
.InsertLines .CountOfLines + 1, code
End With
End Sub
the purpose of this is to paste this code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("D2")) Is Nothing Then
HideRowsBasedOnCriteria
End If
End Sub
into a new sheet on creation that works as a filter using values from a hidden column
I am new to VBA and have mainly built this thanks to ChatGPT, forums and gluing code together, I am having a problem with this code in that when I first open my document and this is activated I get a 'Run-time error '9': Subscript out of range' BUT if I go into the VBA editor in Excel and click reset this code works exactly how it is meant to, this is the code
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Dim code As String
code = "Private Sub Worksheet_Change(ByVal Target As Range)" & vbCrLf & _
" If Not Intersect(Target, Me.Range(""D2"")) Is Nothing Then" & vbCrLf & _
" HideRowsBasedOnCriteria" & vbCrLf & _
" End If" & vbCrLf & _
"End Sub"
With ThisWorkbook.VBProject.VBComponents(Sh.CodeName).codeModule
.InsertLines .CountOfLines + 1, code
End With
End Sub
the purpose of this is to paste this code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("D2")) Is Nothing Then
HideRowsBasedOnCriteria
End If
End Sub
into a new sheet on creation that works as a filter using values from a hidden column