I am getting an error on this line 'Set copyRange = wscf.Range("J" & i) & wscf.Cells(i,…
- User
I am getting an error on this line 'Set copyRange = wscf.Range("J" & i) & wscf.Cells(i, "H").Value' in the following code: For i = 5 To lastRow If wscf.Cells(i, "I") = "" Then If copyRange Is Nothing Then Set copyRange = wscf.Range("J" & i) & wscf.Cells(i, "H").Value Else Set copyRange = Union(copyRange, wscf.Range("J" & i) & wscf.Cells(i, "H").Value) End If End If Next i
- Assistant
The error is occurring because you are trying to concatenate a Range object with a String value. In VBA, you cannot directly concatenate a Range object with a String value using the "&" operator.
To fix the error, you can use the following code:
Set copyRange = wscf.Range("J" & i) If wscf.Cells(i, "H").Value <> "" Then copyRange.Value = wscf.Cells(i, "H").Value End IfThis code first sets the copyRange to the range "J" & i. Then, it checks if the value in column H is not empty. If it is not empty, it assigns the value in column H to the copyRange.
Note: This code assumes that you want to copy the value in column H to the corresponding cell in column J if the value in column H is not empty. If you want to copy the value of column J as well, you can modify the code accordingly.
- User
I need to copy the value as J as well
- Assistant
If you want to copy both the value in column J and column H to the copyRange, you can modify the code as follows:
Set copyRange = wscf.Range("J" & i) If wscf.Cells(i, "H").Value <> "" Then copyRange.Value = wscf.Cells(i, "J").Value & wscf.Cells(i, "H").Value End IfThis code sets the copyRange to the range "J" & i. Then, it checks if the value in column H is not empty. If it is not empty, it assigns the value in column J concatenated with the value in column H to the copyRange.