Find the End of Data File in VBA

dkauf

Board Regular
Joined
Sep 23, 2008
Messages
68
I have a Macro I've developed that takes a raw database export file and runs some formulas/cleans up the data so that it can be imported seamlessly into a different database. KEY - the raw data file can contain any number of rows (values).

To work around the unknown number of rows, I wrote my formulas in VBA to check the first column for a non-blank and then copied that same formula down to Row 5000 (as it is unlikely there would be that many rows with data in the raw file). NOTE - the formula returned "" if the contents in the first column was blank.

This works for the most part, but the database that is then receiving this file is interpreting the Blank "" cells as actually containing values and is rejecting the data input. As a very novice VBA writer, I am hoping to find a way to write a formula in my macro that works like the double-click on the bottom right of a cell you wish to copy down to all adjacent cells (i.e. I would modify my existing VBA code to only copy down until the last row with "real" data as opposed to copying all the way down to Row 5000.

A very basic example of my current work is below:

Uploaded with ImageShack.us
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If your formula column is Col D:
Rich (BB code):
With ActiveSheet
.Range(.Cells(1, 4), .Cells(.UsedRange.Rows.Count, 4)).FormulaR1C1 = "=YOURFORMULA IN R1C1 FORMAT"
End With
 
Last edited:
Upvote 0
If your formula column is Col D:
Rich (BB code):
With ActiveSheet
.Range(.Cells(1, 4), .Cells(.UsedRange.Rows.Count, 4)).FormulaR1C1 = "=YOURFORMULA IN R1C1 FORMAT"
End With


Thank you. How would I modify the following code to follow the suggested syntax above?
Code:
    Range("X2:AA2").Select
    Selection.AutoFill Destination:=Range("X2:AA5000"), Type:=xlFillDefault
 
Upvote 0
This should do what you want
Code:
Sub AddFormula()
Dim LR As Long
LR = Cells(Rows.Count, "B").End(xlUp).Row
Cells(2, "D").Resize(LR - 1, 1).Formula = "=YourFormula" 'For D2
End Sub


lenze
 
Upvote 0
This should do what you want
Code:
Sub AddFormula()
Dim LR As Long
LR = Cells(Rows.Count, "B").End(xlUp).Row
Cells(2, "D").Resize(LR - 1, 1).Formula = "=YourFormula" 'For D2
End Sub


lenze

This is perfect - thanks Lenze!
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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