VBA to find column heading and copy data

karldugan

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

I need some help with VBA code that finds the date and then copies the data from row 6 underneath it (the orange cells). The number of rows underneath 6 will be variable but these cells will always contain a forumla or hard code figure, there will always be a break line at the bottom of the data, I was going to use the following code, I just can't figure out how to get it to look at the date at the top and then start from the 6th row down.

The date it needs to find will come from a sheet called P&L and will always be in cell G5 in that sheet.

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

Cell Formulas
RangeFormula
I4I4=EOMONTH(H4,0)+1
I6,H7:I47I6=Budget!H6
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H36:H46Expression=IF(H$5="ACTUAL",TRUE,)textNO
H6:H35,H47Expression=IF(H$5="ACTUAL",TRUE,)textNO
G36:G46,I36:S46Expression=IF(G$5="ACTUAL",TRUE,)textNO
G47,G6:G35,I6:S35,I47:S47Expression=IF(G$5="ACTUAL",TRUE,)textNO
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If I correctly understood you, you just need to find cell with date. You can use Find method of Range object:
VBA Code:
Sub CopyCells()
    Dim rw
    rw = Rows(4).Find(Sheets("P&L").[G5]).Row
    Range(Cells(rw, "H"), Cells(Rows.Count, "H").End(xlUp)).Copy
    '// Do something...
    '// ...
    
    '// Clear clipboard
    Application.CutCopyMode = False
End Sub
 
Upvote 0
If I correctly understood you, you just need to find cell with date. You can use Find method of Range object:
VBA Code:
Sub CopyCells()
    Dim rw
    rw = Rows(4).Find(Sheets("P&L").[G5]).Row
    Range(Cells(rw, "H"), Cells(Rows.Count, "H").End(xlUp)).Copy
    '// Do something...
    '// ...
   
    '// Clear clipboard
    Application.CutCopyMode = False
End Sub
That always selects column H though, what I need it to do is find the column with that date in it, so if the date in P&L G5 = 01/12/21, it knows to go across to column J and then copy everything from row 6 down.

FY21 Template.xlsm
HIJKLMNOPQRS
4Oct-21Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22
5ForecastForecastForecastForecastForecastForecastForecastForecastForecastForecastForecastForecast
6£0£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
H6:S8H6=Budget!G6
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H6Expression=IF(H$5="ACTUAL",TRUE,)textNO
H6:H35,H47Expression=IF(H$5="ACTUAL",TRUE,)textNO
G47,G6:G35,I6:S35,I47:S47Expression=IF(G$5="ACTUAL",TRUE,)textNO
 
Upvote 0
Beg your pardon:
VBA Code:
Sub CopyCells()
    Dim col&
    col = Rows(4).Find(Sheets("P&L").[G5]).Column
    Range(Cells(6, col), Cells(Rows.Count, col).End(xlUp)).Copy
    '// Do something...
    '// ...
   
    '// Clear clipboard
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Apologies Sektor - When I ran it the first time it just kept selecting column H, I've since gone back and this works. God knows what I was doing!

Thank you!
 
Upvote 0
You're welcome!
hi.gif
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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