Copy Offset cell range and move right

Rowdee26

New Member
Joined
Aug 14, 2019
Messages
3
I have a worksheet, "data". Column 'B' searches for cells to last row with data and pastes to the right over writing data, which is ok. Currently this works ok for cell 'B', but I am trying to redo the code to copy cell 'C' also to move cells 'B' and 'C' to the right but this is not working. See following code I am trying.
Appreciate any guidance you can give as I have been trying to complete coding for the last year and this is my first post. May need help on protocols also during posting.

lr = ActiveSheet.UsedRange.Rows.Count
For a = 2 To lr
If Cells(a, 2).Value <> "" Then
Cells(a, 7).Value = Cells(a, 2).Resize(, 2).Value
End If
Next a
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
With your original code you can use
VBA Code:
Sub test()
    Dim lr As Long
    Dim a As Long
    lr = ActiveSheet.UsedRange.Rows.Count
    For a = 2 To lr
        If Cells(a, 2).Value <> "" Then
            Cells(a, 7).Resize(a, 2).Value = Range(Cells(a, 2), Cells(a, 3)).Value
        End If
    Next a
End Sub

Alternatively (and much faster) you can use an array to copy/paste the data
VBA Code:
Sub test2()
    Dim lr As Long
    Dim arr
    lr = ActiveSheet.UsedRange.Rows.Count + 1
    arr = Range(Cells(2, 2), Cells(lr, 3))
    Cells(2, 7).Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub

As a 'protocol' suggestion, please use the code tags when posting code or formulas. You can find the buttons to insert them at the top of the edit window when posting.
 
Upvote 0
Solution
If im understanding you correctly you want to copy the values in B AND C into columns G and H but only if there is a value in B.
if that is the case, you can adjust the middle code to
VBA Code:
Cells(A, 7).Resize(, 2).Value = Cells(A, 2).Resize(, 2).Value
and it should work. If you only want to do this when there is a value in B & C then you will need to consider that in your if statement.
 
Upvote 0
@myall_blues:
test was copying correctly but was pasting xtra cells down. Just tweaked the code - Cells(a, 7).Resize(, 2).Value - to stop doing that and now works great.
test2 array was copying all cells from column B - blank & non blank - with column C to last row. If you have a solution I definitely would like to see that as arrays is absolutely foreign to me.
@dermie_72:
Your code was similar but copying different cell and pasting to the desired location to last row.

Thanks to myall_blues and @dermie_72 for your replies.
I will take it in and continue learning!
 
Upvote 0
Apologies about the skipping blanks bit.
Code below for array method, with comments for reference.

VBA Code:
Sub test2()
    Dim lr As Long, i As Long
    '
    ' Arrays need to be declared as variant
    '
    Dim arr
    '
    lr = ActiveSheet.UsedRange.Rows.Count + 1 ' +1 because data starts in row 2
    '
    ' Read the entire range of interest into the array
    '
    arr = Range(Cells(2, 2), Cells(lr, 3))
    '
    ' LBound(arr,1) is the first row of the array, UBound is the last
    ' Likewise, LBound(arr,2) is the first column of the array
    '
    ' Loop through the array
    '
    For i = LBound(arr, 1) To UBound(arr, 1)
        If arr(i, 1) <> "" Then
            '
            ' Write the first column array value
            '
            Cells(i + 1, 7).Value = arr(i, 1)
            '
            ' Write the second column array value
            '
            Cells(i + 1, 8).Value = arr(i, 2)
        End If
    Next
End Sub
 
Upvote 0
Thanks for the reply Murray.
As this is the 2nd part of my reporting macro to go with the first part i have completed, I have decided to use this array for my macro as it is significantly faster to action with the number of data lines I have.
I look forward to educating myself on these arrays.

I appreciate your time and effort with your above coding.
If I don't hear from you over the next couple of months, all the best for the festive season.

Rod.
 
Upvote 0
Thanks Rod, and the same to you.

If you can use an array, even if you have to loop through it as in this case, it will always be substantially faster than reading and writing to cells.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top