VBA Code to Find a Value on a sheet and copy and paste the values below it

LindenWolf

New Member
Joined
Feb 28, 2012
Messages
30
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am very new at VBA and really need some help with a piece of VBA code.

I receive a file from someone that has a Heading (maybe in row 2, row 1, row 3, who knows) The heading labels are at least consistent.
I have a master template that I need to copy the data beneath each heading of their file to my file.

Received File: the value I am looking for is called Employee Name, could be in column G, H, I just depends on how inconsistent they are
Master Template File: My value is always in column G and I would need to post their data starting in cell G2

So I need to do a search on the Employee Name title so that I know where it is today, then copy the data underneath that all the way to the end and paste it in my Master Template File starting in Column G2.

Any help would be greatly appreciated.
 
I have included the macro with explanatory comments:
Code:
Sub CopyData()
    Application.ScreenUpdating = False 'prevents screen flickering and sppeds up the macro
    Dim LastRow As Long
    LastRow = Workbooks("Master_Data.xlsx").Sheets("Master_Data").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'finds the lasted used row in the Master workbook, Master_Data worksheet
    Dim foundVal As Range
    Set foundVal = Workbooks("Master_Data.xlsx").Sheets("Master_Data").UsedRange.Cells.Find("Employee Name", LookIn:=xlValues, lookat:=xlWhole) 'searches for "Emplyee Name" in the Master workbook and sheet
    If Not foundVal Is Nothing Then 'if it finds "Employee Name, the next line of code is executed
        Workbooks("Master_Data.xlsx").Sheets("Master_Data").Range(Cells(foundVal.Row + 1, foundVal.Column), Cells(LastRow, foundVal.Column)).Copy 'copies the range starting from 1 row below "Employee Name" down to the lastrow in that column
            Workbooks("Template.xlsm").Sheets("Sheet1").Range("G2").PasteSpecial xlPasteValues 'pastes the copied range to cell G2 of the Template
    End If
    Application.CutCopyMode = False 'de-selects the copied range
    Application.ScreenUpdating = True 'turns ScreenUpdating back on
End Sub
I still don't understand why it works properly for me and not for you. The only explanation I can think of is that your actual data is organized differently than the files you posted.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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