Home » excel » excel – Is there a reason why VBA code does not work in sequential order?

excel – Is there a reason why VBA code does not work in sequential order?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am not entirely sure how to phrase this question. However, I will be able to explain it better here. Below is my code. The purpose of my code is to copy and paste data from one sheet to another.

Everything seems to work fine up until it gets to the very last line of code (excluding “End Sub”). The last line is supposed to fill down to the last row. The issue I am getting is that the code works fine if I break up the With statement and the final line and run them separately.

I know the last line works but when I run the entire macro, I get a “Run time Error ‘1004” error message. Why does my code not work?

Sub Data_Table()

Dim Data As Worksheet
Dim Sum As Worksheet
Dim lr As Long
Dim lr2 As Long
Dim lr3 As Long
Dim lr4 As Long
Dim lr5 As Long


Set Data = Worksheets("Data-Tracker")
Set Sum = Worksheets("Summary")
lr = Data.Cells(Rows.Count, "E").End(xlUp).Row
lr2 = Data.Cells(Rows.Count, "A").End(xlUp).Row 'for customer type
lr3 = Data.Cells(Rows.Count, "B").End(xlUp).Row ' for Type
lr4 = Data.Cells(Rows.Count, "C").End(xlUp).Row ' for Rate/Budget
lr5 = Data.Cells(Rows.Count, "D").End(xlUp).Row ' for Date


    With Sum

        .Range("B6:B12").Copy Destination:=Data.Range("E" & lr).Offset(1, 0)
        .Range("C6:C12").Copy Destination:=Data.Range("F" & lr).Offset(1, 0)
        .Range("D6:D12").Copy Destination:=Data.Range("G" & lr).Offset(1, 0)
        .Range("C2").Copy Destination:=Data.Range("B" & lr3).Offset(1, 0)
        .Range("B4").Copy Destination:=Data.Range("C" & lr4).Offset(1, 0)
        .Range("B5").Copy Destination:=Data.Range("D" & lr5).Offset(1, 0)


    End With

    Data.Range("B" & lr3, "D" & lr5).AutoFill Destination:=Data.Range("B" & lr3, "D" & lr)


End Sub

Any help would be greatly appreciated.

EDIT:

To help further explain my point, if I first run my code like this:

    With Sum

        .Range("B6:B12").Copy Destination:=Data.Range("E" & lr).Offset(1, 0)
        .Range("C6:C12").Copy Destination:=Data.Range("F" & lr).Offset(1, 0)
        .Range("D6:D12").Copy Destination:=Data.Range("G" & lr).Offset(1, 0)
        .Range("C2").Copy Destination:=Data.Range("B" & lr3).Offset(1, 0)
        .Range("B4").Copy Destination:=Data.Range("C" & lr4).Offset(1, 0)
        .Range("B5").Copy Destination:=Data.Range("D" & lr5).Offset(1, 0)


    End With

'    Data.Range("B" & lr3 & ":D" & lr5).AutoFill Destination:=Data.Range("B" & lr3, "D" & lr)

And then after run it like:

'    With Sum
'
'        .Range("B6:B12").Copy Destination:=Data.Range("E" & lr).Offset(1, 0)
'        .Range("C6:C12").Copy Destination:=Data.Range("F" & lr).Offset(1, 0)
'        .Range("D6:D12").Copy Destination:=Data.Range("G" & lr).Offset(1, 0)
'        .Range("C2").Copy Destination:=Data.Range("B" & lr3).Offset(1, 0)
'        .Range("B4").Copy Destination:=Data.Range("C" & lr4).Offset(1, 0)
'        .Range("B5").Copy Destination:=Data.Range("D" & lr5).Offset(1, 0)
'
'
'    End With

    Data.Range("B" & lr3 & ":D" & lr5).AutoFill Destination:=Data.Range("B" & lr3, "D" & lr)

I have to add the apostrophes in to cancel the code out in order for it to work. Otherwise I get a Autofill Method error.

How to&Answers:

There are two things you need to do.

The first and the obvious one is to fix the syntax in the line in question: a Range Address needs to be indicated in both Source and Destination, not the corners: …lr3 & ":D"… and not this …lr3, "D"

The second one is to make sure that the Destination is always taller than the source, just in case:

If lr > lr5 Then
     Data.Range("B" & lr3 & ":D" & lr5).AutoFill ....
End If

Answer:

I suggest replacing the Range().Copy Destination:= statements with absolute referencing with relative referencing and the direct assignment of multiple values in one statement.

I think your intent is to append to the Summary worksheets with values from Data.

Say you have n cells under E2 in the Data page that you want to be copied under the k-th cell in Summary page under B2. You do that with

Sum.Range("B2").Cells(k,1).Resize(n,1).Value = Data.Range("E2").Resize(n,1).Value

The trick is reference one cell and then use the .Resize() statement to expand the selection into a whole table (multiple rows and columns) if needed. The use the .Value assignment to move all the values in one statement.