I have data on Sheet1 which varies in quantity. And I need a macro to loop through all the range if if the cell has a value, then copy it to a specific column on Sheet2. I cannot simply copy the whole range for reasons I won't get into. I need to loop through each cell one by one and paste the value in a specific format on Sheet2.
I have the below code, which gives me
Error '1004' Method 'Range of Object'_Worksheet' failed.
Sub COPY()
Dim i As Long, j As Long, rng As Range, k As Long, ws1 As Worksheet, ws2 As Worksheet
k = 1
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Set rng = Range("B4:D17")
For i = 1 To rng.Rows.Count
If Not IsEmpty(rng.Cells(i, 1)) Then
ws1.Range(rng.Cells(i, 3)).Copy Worksheets("Sheet2").Range("A1")
End If
Next
End Sub
rng
variable is referencing the active worksheet because you did not qualify yourRange
object when you set it. – braXws1.Range(rng.Cells(i, 3))
?rng.Cells()
already returns a range object, so why are you placing this into another function that does essentially the same thing? – K.Dᴀᴠɪs