I would like to change the VBA code below to do the following; If Cells(i, "I") = ""…
- User
I would like to change the VBA code below to do the following;
If Cells(i, "I") = "" Then Set copyRange = wscf.Range("J" & i) with the value of Cells(i, "H") added Values in "H" are either empty or contain a date value
For i = 5 To lastRow If wscf.Cells(i, "I") = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Range("J" & i) Application.ScreenUpdating = True Application.EnableEvents = True Else Set copyRange = Union(copyRange, wscf.Range("J" & i)) Application.ScreenUpdating = True Application.EnableEvents = True End If End If Next i
- Assistant
To modify the VBA code to set
copyRangewith the value ofCells(i, "H")added ifCells(i, "I")is blank or empty, you can make the following changes:For i = 5 To lastRow If wscf.Cells(i, "I") = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Range("J" & i) If wscf.Cells(i, "H") <> "" Then copyRange.Value = copyRange.Value + wscf.Cells(i, "H").Value End If Else Set copyRange = Union(copyRange, wscf.Range("J" & i)) If wscf.Cells(i, "H") <> "" Then copyRange.Value = copyRange.Value + wscf.Cells(i, "H").Value End If End If End If Next i Application.ScreenUpdating = True Application.EnableEvents = TrueIn the above code, we added an
Ifcondition inside the firstIfblock to check ifCells(i, "H")is not empty before adding it tocopyRange.Value.Remember to adjust the
lastRowvalue to match your specific range.Finally, the lines
Application.ScreenUpdating = TrueandApplication.EnableEvents = Truecan be placed outside the loop (after ) to enable screen updating and events back, to avoid possible performance issues. - User
rror on this line: copyRange.Value = copyRange.Value + wscf.Cells(i, "H").Value
- Assistant
Apologies for the incorrect code. The
+operator is not applicable for concatenating values in VBA. To concatenate the values, we can use the&operator. Here's the corrected code:For i = 5 To lastRow If wscf.Cells(i, "I") = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Range("J" & i) If wscf.Cells(i, "H") <> "" Then copyRange.Value = copyRange.Value & wscf.Cells(i, "H").Value End If Else Set copyRange = Union(copyRange, wscf.Range("J" & i)) If wscf.Cells(i, "H") <> "" Then copyRange.Value = copyRange.Value & wscf.Cells(i, "H").Value End If End If End If Next i Application.ScreenUpdating = True Application.EnableEvents = TrueNow the values from column H will be concatenated to the existing values in
copyRange.Valueusing the&operator.