In the VBA event below, I am getting an Object required error on the line 'If Not…
- User
In the VBA event below, I am getting an Object required error on the line 'If Not Intersect(Target, activeSheet.Range("E6:E305")) Is Nothing Then' : Sub BudgetChange()
codeDim sectorSheet As Worksheet Dim sectorRange As Range Dim subSectorRanges As Variant Dim subSectorRange As Range Dim sectorValue As String Dim subSectorValue As String Dim costValue As Double ' Set the worksheets Set sectorSheet = ThisWorkbook.Sheets("Sector Budget") Application.EnableEvents = False activeSheet.Unprotect Password:="edit" sectorSheet.Unprotect Password:="edit"' Set the sector range Set sectorRange = sectorSheet.Range("A2:A22")
code' Set the subsector ranges subSectorRanges = Array(sectorSheet.Range("C2:C22"), sectorSheet.Range("E2:E22"), sectorSheet.Range("G2:G22"), _ sectorSheet.Range("I2:I22"), sectorSheet.Range("K2:K22"), sectorSheet.Range("M2:M22"), _ sectorSheet.Range("O2:O22"), sectorSheet.Range("Q2:Q22"), sectorSheet.Range("S2:S22"), _ sectorSheet.Range("U2:U22"), sectorSheet.Range("W2:W22")) ' Check if the changed range is in E6:E305 and the target range is within the specified range If Not Intersect(Target, activeSheet.Range("E6:E305")) Is Nothing Then If Not Intersect(Target, activeSheet.Range("E6:E305").Resize(, 1)) Is Nothing Then ' Get the sector value from D1 sectorValue = activeSheet.Range("D1").Value ' Get the cost value from the adjacent cell costValue = Target.Value ' Loop through the subsector ranges For Each subSectorRange In subSectorRanges ' Check if the subsector value exists in the range If WorksheetFunction.CountIf(subSectorRange, activeSheet.Range("D" & Target.Row).Value) > 0 Then ' Get the subsector value subSectorValue = activeSheet.Range("D" & Target.Row).Value ' Find the matching sector row sectorRow = WorksheetFunction.Match(sectorValue, sectorRange, 0) ' Find the matching subsector column subSectorColumn = WorksheetFunction.Match(subSectorValue, subSectorRange, 0) ' Add the cost value to the corresponding cell in the sector budget sheet sectorSheet.Cells(sectorRow, subSectorColumn).Value = sectorSheet.Cells(sectorRow, subSectorColumn).Value + costValue Exit For End If Next subSectorRange End If End If sectorSheet.Protect Password:="edit" activeSheet.Protect Password:="edit" Application.EnableEvents = True - Assistant
- User
When I run the code, I get a Run-time error '5': Invalid procedure call or argument 'If Not Intersect(Target, activeSheet.Range("E6:E305")) Is Nothing Then'
- Assistant
The error "Run-time error '5': Invalid procedure call or argument" is occurring because the 'Target' variable is not being assigned to when the event is triggered. Make sure you are calling the 'BudgetChange' subroutine from the appropriate event that passes the changed cell(s) as the target.
For example, if you want to trigger the code when a value is changed in the worksheet, you can use the Worksheet Change event. In the Worksheet module, add the following code:
vbaPrivate Sub Worksheet_Change(ByVal Target As Range) BudgetChange ' Call the BudgetChange subroutine End SubThis ensures that the Target variable is properly assigned to the changed range.
- User
- Assistant
- User
When I run the code, I get a Run-time error '424': 'Object required' on the line 'For Each subSectorRange In subSectorRanges'
- Assistant
The error "Run-time error '424': Object required" occurs because the variable
subSectorRangeis not declared correctly in the code. To fix the error, add theDim subSectorRange As Rangedeclaration before the loop.Here's the updated code: