Copy Data from One Range to Another in Excel VBA disregarding empty cells

Babynod

Board Regular
Joined
Aug 10, 2022
Messages
56
Office Version
  1. 365
Platform
  1. Windows
Hi All,

i have a very simple copy paste code from an input sheet(DROP) to the main sheet (sheet1)

this works but as i am copying a range when i use this multiple times it inserts the data in the next row after the previous range was pasted in, not the next empty cell.
how do i make it disregard empy cells

VBA Code:
Private Sub CommandButton1_Click()

    Range("K4:V33").Copy
    Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

End Sub


there is the range it copies from (K4:V33). as the size of this list varies i need to copy the whole thing but exclude the empty rows
1677799249348.png




this is where it paste to
1677799285768.png
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
VBA Code:
Private Sub CommandButton1_Click()
Dim r%, nr%, arr, lr&, i%
arr = Worksheets("DROP").Range("K4:V33")
    For r = 1 To UBound(arr)
        If arr(r, 1) <> "" Then
            nr = nr + 1
            For i = 1 To 12
                arr(nr, i) = arr(r, i)
            Next i
        End If
    Next r
    
    With Sheets("Sheet1")
        lr = .Range("B" & Rows.Count).End(xlUp).Row + 1
        .Range("B" & lr & ":" & "M" & lr).Resize(nr) = arr
    End With
End Sub
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim r%, nr%, arr, lr&, i%
arr = Worksheets("DROP").Range("K4:V33")
    For r = 1 To UBound(arr)
        If arr(r, 1) <> "" Then
            nr = nr + 1
            For i = 1 To 12
                arr(nr, i) = arr(r, i)
            Next i
        End If
    Next r
   
    With Sheets("Sheet1")
        lr = .Range("B" & Rows.Count).End(xlUp).Row + 1
        .Range("B" & lr & ":" & "M" & lr).Resize(nr) = arr
    End With
End Sub
Spot on mate thankyou very much.
are you able to break it down so i can understand how this works?
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim r%, nr%, arr, lr&, i%
arr = Worksheets("DROP").Range("K4:V33")
    For r = 1 To UBound(arr)
        If arr(r, 1) <> "" Then
            nr = nr + 1
            For i = 1 To 12
                arr(nr, i) = arr(r, i)
            Next i
        End If
    Next r
   
    With Sheets("Sheet1")
        lr = .Range("B" & Rows.Count).End(xlUp).Row + 1
        .Range("B" & lr & ":" & "M" & lr).Resize(nr) = arr
    End With
End Sub
So ive changed the ranges as ive changed the layout of my sheet, but now im getting an error



1677878433329.png




When i debug it it highlights arr(nr, i) = arr(r, i)

VBA Code:
Private Sub CommandButton1_Click()
Dim r%, nr%, arr, lr&, i%
arr = Worksheets("DROP").Range("L4:T100")
    For r = 1 To UBound(arr)
        If arr(r, 1) <> "" Then
            nr = nr + 1
            For i = 1 To 12
                arr(nr, i) = arr(r, i)
            Next i
        End If
    Next r
    
    With Sheets("MASTER")
        lr = .Range("B" & Rows.Count).End(xlUp).Row + 1
        .Range("B" & lr & ":" & "J" & lr).Resize(nr) = arr
    End With
End Sub

For my layout now, i paste an extract in the left side, then i click my SPLIT macro button, this does TextToColumns on column H as the extract has Qty/Type/Supplier all in 1 column with / between them, so the TextToColumns separates by /.
This lines everything up on the right side (right side is just formulas to equal the corresponding cell on the left side e.g L4 =B4)

Also, where would i add in Range(B4:J100).clear so after the code runs and copys the right side onto my master sheet, it then clears the left side ready for the next extract to be dropped in?
 
Upvote 0
So ive changed the ranges as ive changed the layout of my sheet, but now im getting an error
Respectively, here you need to specify the more specific address of the data you want to copy. L-T columns? (in the post #1 you wrote: " (K4:V33)")
Also, where would i add in Range(B4:J100).clear so after the code runs and copys the right side onto my master sheet, it then clears the left side ready for the next extract to be dropped in?
I suggest that we first solve the first stumbling block in the code, and then consider the other questions. :)
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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