Macro to past based on columns

karldugan

New Member
Joined
May 10, 2016
Messages
47
Hi all,

Tried looking around for this one but can't find something that works.

I have data in columns A - D that a macro pre-populates, only column B has data in every cell. The macro copies a date from one sheet but what I want it to do is find the first empty cell in column E to start pasting into and then go down as far as the last populated cell in column B.

Thanks in advance :)
Karl.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Can you post some sample data and show us your expected output?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Can you post some sample data and show us your expected output?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
FY21 Template.xlsm
ABCDEFGH
1LookupNominalCost CentreDepartmentPeriodMonthYTD
270007000
370107010
470407040
570417041
671007100
772007200
872017201
972107210
1072207220
1173007300
127301FAC7301FAC
1373017301
147303FAC7303FAC
1573047304
1673077307
1773087308
1873157315
1973187318
2074007400
2174027402
2274107410
2374117411
2474137413
2574207420
2675007500
2776017601
2876027602
298110ORDPRO8110ORDPRO
3084258425
31 
32 
33 
34 
35 
Prior Forecast
Cell Formulas
RangeFormula
A2:A35A2=B2&C2&D2
 
Upvote 0
Can you post some sample data and show us your expected output?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
So I want to post a date into column E, but the macro needs to find the first empty cell in column E and then paste the date down as far as the data goes in column B.
 
Upvote 0
You didn't mention exactly where the date to be used comes from, so I created an InputBox for it, though that can easily be changed.
Try this code:
VBA Code:
Sub MyCopy()

    Dim dte As Date
    Dim firstE As Long
    Dim lastE As Long
    
'   Prompt for new date
    dte = InputBox("What date would you like to populate?")

'   First first row in column E neededing data
    firstE = Cells(Rows.Count, "E").End(xlUp).Row + 1
    
'   Find last row in E needing data (by looking at column B)
    lastE = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Populate column E
    Range(Cells(firstE, "E"), Cells(lastE, "E")).Value = dte
    
End Sub
 
Upvote 0
Solution
You didn't mention exactly where the date to be used comes from, so I created an InputBox for it, though that can easily be changed.
Try this code:
VBA Code:
Sub MyCopy()

    Dim dte As Date
    Dim firstE As Long
    Dim lastE As Long
   
'   Prompt for new date
    dte = InputBox("What date would you like to populate?")

'   First first row in column E neededing data
    firstE = Cells(Rows.Count, "E").End(xlUp).Row + 1
   
'   Find last row in E needing data (by looking at column B)
    lastE = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Populate column E
    Range(Cells(firstE, "E"), Cells(lastE, "E")).Value = dte
   
End Sub
Hi Joe,
Thanks for the super speedy response, the date was picking up from another worksheet and I'd managed to copy it but couldn't figure out the next part.

That said, that prompt is really cool, so I am going to keep that instead!

Thanks for the help and it works a treat!
Karl.
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0
Hi Joe, wondering if you could help with another bit, as I really want to utilise that date prompt you've given me. What I want to do is copy the data from row 6 down based on the date that comes from your input. The data will always start in row 6, but will be various numbers of rows down between different files, all the rows contain data and there is a break line at the bottom, so I was trying to use something like the following code, I just can't figure out how I tell it to find a specific column based on the header.

Range("B6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

FY21 Template.xlsm
HIJKLMNOPQRS
4Oct-21Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22
5ForecastForecastForecastForecastForecastForecastForecastForecastForecastForecastForecastForecast
6£95,000£0£0£0£0£0£0£0£0£0£0£0
7£0£0£0£0£0£0£0£0£0£0£0£0
8£0£0£0£0£0£0£0£0£0£0£0£0
Forecast
Cell Formulas
RangeFormula
I4:S4I4=EOMONTH(H4,0)+1
I6:S6,H7:S8I6=Budget!H6
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H6:H35,H47Expression=IF(H$5="ACTUAL",TRUE,)textNO
G47,G6:G35,I6:S35,I47:S47Expression=IF(G$5="ACTUAL",TRUE,)textNO
 
Upvote 0
Hi Joe, wondering if you could help with another bit, as I really want to utilise that date prompt you've given me. What I want to do is copy the data from row 6 down based on the date that comes from your input. The data will always start in row 6, but will be various numbers of rows down between different files, all the rows contain data and there is a break line at the bottom, so I was trying to use something like the following code, I just can't figure out how I tell it to find a specific column based on the header.
That really is a new and different question, and as such, should be posted in a new thread so it shows up on the "Unanswered threads" listing for all the community to see.

The general rule of thumb is that new questions should be posted to new threads, unless it is a clarification of the original question.
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,686
Members
452,994
Latest member
Janick

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