Search code examples
vbaif-statementcopypasteno-data

Copying columns including blanks without skipping rows..leave "blanks" blank VBA


Aplication Defined error Copying a specified column and range including blanks with an embedded button running multiple Macros. I know that all rows will be filled in column A so if I could reference the rest of the Macros to A.end

I've looked Google youtube and here although there is a lot of info on copying and pasting, I cannot find one that works for this running multiple Macros.

Macros 5 & 6 is where I start having problems because these columns have multiple blanks throughout.

Raw data to Copy:

Sheet1

Destination:

Sheet2

Private Sub CommandButton1_Click()

Worksheets("Sheet1").Range("a2", Range("a2").End(xlDown)).Copy _
        Worksheets("Sheet2").Range("a2") 'macro1

Worksheets("Sheet1").Range("d2", Range("d2").End(xlDown)).Copy _
        Worksheets("Sheet2").Range("b2")  'Macro2

Worksheets("Sheet1").Range("c2", Range("c2").End(xlDown)).Copy _
        Worksheets("Sheet2").Range("c2") 'macro3

Worksheets("Sheet1").Range("g2", Range("g2").End(xlDown)).Copy _
        Worksheets("Sheet2").Range("d2") 'macro4


If Worksheets("Sheet1").Range("e2", Range("e2").End(xlDown)).Value = "<0" Then
    Worksheets("Sheet2").Range("i2").Copy  'macro5

If Worksheets("Sheet1").Range("e2", Range("e2").End(xlDown)).Value = ">0" Then
        Worksheets("Sheet2").Range("j2").Copy 'macro6

Worksheets("Sheet2").Activate 'macro7

Solution

  • Range.end(xldown) only gets you a contiguous range (effectively it will stop at the first blank cell).

    Since you want to include blanks, you might want to instead work from the last row of your worksheet back up to the first non-blank cell encountered in that column (which is a way of getting the last row).

    This would mean something like:

    ' If you are new to With statements (below), any objects within the With block that begin with a . relate to "Sheet1". Saves us typing Sheet1 repeatedly, and makes sense to use it since we access a lot of Sheet1's members like range/cells/rows
    
    With Worksheets("Sheet1")
    
    .Range("a2", .cells(.rows.count, "A").End(xlup)).Copy Worksheets("Sheet2").Range("a2") 'macro1
    
    End with
    

    Untested, written on mobile -- but hope it works or gets you closer to a solution. You would need to copy-paste the above and change the A to B, C, D, E, etc. I wasn't too sure what you're trying to achieve with the "<0" condition in macro 5 and 6.

    (It would better if you turned the code into a parameterised Sub and just provide the column letter/number as an argument to the sub, but just depends how new you are to VBA and programming in general -- and for the time being whatever is easier for you to understand/maintain.)

    Edit regarding macro 5 and 6

    With Worksheets("Sheet1")
    
    Dim cell as range
    
    For each cell in .Range("E2", .Cells(.Rows.Count, "E").End(xlUp))
    
    If cell.Value <= 0 Then 'Get rid of the equal sign if you don't want it in your logic/condition'
    
    Cell.Copy Worksheets("Sheet2").cells(cell.row, "I") 'Macro5
    
    ElseIf cell.value > 0 Then
    
    Cell.Copy Worksheets("Sheet2").cells(cell.row, "J") 'Macro6
    End If
    
    Next cell
    
    End With
    
    
    
    Worksheets("Sheet2").Activate 'macro7