Ashley91
04-07-2022, 11:58 PM
Hello,
So I am trying to work on this again now. My code works if I do it per sheet, but the number of sheets differ monthly so I can't do the per sheet code. How do I loop through all sheets and add the worksheet_change event to each sheet?
What am I doing wrong in the below code?
With wTarget
For Each sh In Worksheets
Set xPro = .VBProject
Set xCom5 = xPro.VBComponents(sh)
Set xMod = xCom5.CodeModule
With xMod
xLine = .CreateEventProc("Change", "Worksheet")
xLine = xLine + 1
.InsertLines xLine, " End If"
.InsertLines xLine, " Next cll"
.InsertLines xLine, " Cells(cll.Row, ""Q"") = Join(Array(Cells(cll.Row, ""D"").Text, Cells(cll.Row, ""H"").Text, Cells(cll.Row, ""M"").Text, Cells(Target.Row, ""N"").Text, Cells(cll.Row, ""O"").Text, Cells(cll.Row, ""P"").Text), vbLf)"
.InsertLines xLine, " For Each cll In myRng"
.InsertLines xLine, " If Not myRng Is Nothing Then"
.InsertLines xLine, " Set myRng = Intersect(Target, Range(""D:D,H:H,M:P""))"
End With
Next sh
End With
Please help thank you so much
So I am trying to work on this again now. My code works if I do it per sheet, but the number of sheets differ monthly so I can't do the per sheet code. How do I loop through all sheets and add the worksheet_change event to each sheet?
What am I doing wrong in the below code?
With wTarget
For Each sh In Worksheets
Set xPro = .VBProject
Set xCom5 = xPro.VBComponents(sh)
Set xMod = xCom5.CodeModule
With xMod
xLine = .CreateEventProc("Change", "Worksheet")
xLine = xLine + 1
.InsertLines xLine, " End If"
.InsertLines xLine, " Next cll"
.InsertLines xLine, " Cells(cll.Row, ""Q"") = Join(Array(Cells(cll.Row, ""D"").Text, Cells(cll.Row, ""H"").Text, Cells(cll.Row, ""M"").Text, Cells(Target.Row, ""N"").Text, Cells(cll.Row, ""O"").Text, Cells(cll.Row, ""P"").Text), vbLf)"
.InsertLines xLine, " For Each cll In myRng"
.InsertLines xLine, " If Not myRng Is Nothing Then"
.InsertLines xLine, " Set myRng = Intersect(Target, Range(""D:D,H:H,M:P""))"
End With
Next sh
End With
Please help thank you so much