Excel VB-Copy formula down until adjacent cell (left) is blank?

gittymoe

Board Regular
Joined
Apr 23, 2005
Messages
79
Guys, I have daily data pull fills Colum D Row count is always different. I am modifying the data pull through VB, and I also need to find the last cells in columns A:C and copy the data down to the last row in column D. I guess I want it to be dynamic so that as rows decrease/increase the data is only copied down to the final row/record. Below works, however, it starts at A2:C2 every time. Somedays I may have data that starts at A2:C2 while other days It will start at A25:C25 so I need this to be dynamic and pick up the last records in columns A:C.

Sub test()
Dim LastRow As Long
With Worksheets("Sheet1")
LastRow = .Cells(Rows.Count, "D").End(xlUp).Row
.Range("a2:C2").AutoFill Destination:=.Range("A2:C2" & LastRows & LastRow) _
, Type:=xlFillCopy
End With

End With
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
And what can we analyze to determine where the data starts from?
For example: Are the rows above the first used row empty? Is there a heading that can be searched ad located? Is that the first row of data in column D? Or what?

Bye?
 
Upvote 0
Yes, there are headings in Columns A, B and C..InvoiceNumber, InvoiceDate and PONumber respectively. So the first available for each would be A2, B2 and C2 and my thought would be to copy down using column D with the header SKUNumber that with data starting in D2 and may go down some days to D2:D33 or the next day D2:D20. In the below example I would look to have the data in columns A:C copy down based on column D, in this case D4

InvoiceNumberInvoiceDatePONumberSKUNumber
N123454/26/20201123456
1234567
234567
 
Upvote 0
I'm confused.

Are you wanting to copy data in A2:C2 down the same number of rows as there are in Col D that have data already ?
The SKU Numbers are already in the Col from D2:D25 as an example ?

If the above question answer is true ... are you wanting the Invoice Number to increase one number at a time as it is copied in the different rows ?
What about the Invoice Date ? Is that to be increased one day at a time as well ?
And the PONumber is to be increased one number at a time too ?
 
Upvote 0
There would be no increase in PONumber, InvoiceDate, or InvoiceNumber. Below is what the end results should be. Think of it like this, Ultimately, I am trying to pull in invoice data from one invoice and I need the InvoiceNumber, InvoiceDate, and PONumber (they are only provided once on the invoice) to be copied down in the columns adjacent to the SKUNumbers

InvoiceNumberInvoiceDatePONumberSKUNumber
N123454/26/20201123456
N123454/26/202011234567
N123454/26/20201234567
 
Upvote 0
This is one of many methods :

VBA Code:
Option Explicit

Sub test()
Dim C As Range
Application.ScreenUpdating = False

    For Each C In Range("D2:D100") '<-- adjust range here
        If C.Value <> "" Then
            Sheet1.Range("A2:C2").Copy C.Offset(0, -3)
        End If
    Next
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
[...] and I also need to find the last cells in columns A:C and copy the data down to the last row in column D. I guess I want it to be dynamic so that as rows decrease/increase the data is only copied down to the final row/record. [...] . Somedays I may have data that starts at A2:C2 while other days It will start at A25:C25
Given the initial information plus those in messages #3 and #5 I guess we are talking about a summary list, populated day after day by appending the new information to those already set in the sheet. So my proposal is this macro:
VBA Code:
Sub gittyfill()
Dim LastA As Long, LastD As Long
'
LastA = Cells(Rows.Count, "A").End(xlUp).Row
LastD = Cells(Rows.Count, "D").End(xlUp).Row
'
If LastD > LastA Then
    Cells(LastA + 1, "A").Resize(LastD - LastA, 3).Value = Cells(LastA, "A").Resize(1, 3).Value
End If
End Sub
This will copy the values in the "last available" Ax:Cx near the data in col D

Bye
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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