Copy and paste columns between sheets based on an Excel "Map" or table? - Further autoexecute file links? - excel

I have been doing this manually for far too long, and I feel there must be a way to speed up this process. Hopefully you guys can help me.
Currently I have an excel file that has written in VBA macros that copy and paste certain columns from one sheet and paste them into another sheet. There are about 160 of these written, each with about 10 copy/paste commands. (This workbook is called workbook A)
Currently my method involves me opening up Workbook B, copying the data into a sheet in Workbook A, selecting a macro to run from a dropdown in Workbook A, copying the results and pasting them into a "master" Workbook C. The problem for me is that There are frequent instances when maps - that is the column locations of data - change in Workbook B. I maintain a "master map" file that looks something like this:
Contract# | Purchaser | Price | Quantity | Total
------------------------------------------------
A | B | C | D | E
------------------------------------------------
G | D | C | A | B
------------------------------------------------
etc. (Apologies if that's messy)
What I would like to do is have the sheet do the copying and pasting automatically based on A: The column on the map (the pasting column) and B: the letter represented in the row for that particular Contract (this copies the column of which the letter represented).
Is this possible?
Secondly, if that is - The option to have this execute automatically by specifying the file patch of each file would be outstanding (I have a clear taxonomy of file locations and names). Is that also possible?
Added condensed sample of ran macro.
The macro is quite simple, here is a sample...
Sub PA979()
Application.ScreenUpdating = False
'Retail $
Sheets("VSR Input").Select
Range("x1:x5004").Copy
Sheets("Sheet1").Select
Range("q4").Select
ActiveSheet.Paste
'PA $
Sheets("VSR Input").Select
Range("y1:y5004").Copy
Sheets("Sheet1").Select
Range("s4").Select
ActiveSheet.Paste
'Q
Sheets("VSR Input").Select
Range("z1:z5004").Copy
Sheets("Sheet1").Select
Range("t4").Select
ActiveSheet.Paste
'Total $
Sheets("VSR Input").Select
Range("aa1:aa5004").Copy
Sheets("Sheet1").Select
Range("u4").Select
ActiveSheet.Paste
Range("A1").Select
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Dim usedRows As Long
usedRows = ws.Cells(ws.Rows.Count, "U").End(xlUp).Row
Application.ScreenUpdating = False
Sheets("Sheet1").Select
Range("v3").Select
ActiveCell.FormulaR1C1 = "PA#"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell = "979"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range(Cells(1, 1), Cells(usedRows - 3, 1)), Type:= _
xlFillDefault
Range("v4").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveSheet.Range("A1").Select
End Sub `

Assumes the workbook containing the macro has a worksheet "map" with one row for each contract number:
A: contract number
B: file path for the input workbook
C-F: source column letters for each of the columns being copied
Row 2 on the Map sheet has the destination column letters in cols C-F
Compiled but not tested:
Option Explicit
Sub Tester()
CopyData 979
End Sub
Sub CopyData(contractNumber)
Dim wbInput As Workbook
Dim wbDest As Workbook
Dim shtIn As Worksheet, shtDest As Worksheet, shtMap As Worksheet
Dim usedRows As Long
Dim arrDestCols, x As Integer, cFrom, cTo
Dim f As Range, mapRow As Range
'has the column mapping info for each contract number
Set shtMap = ThisWorkbook.Sheets("Map")
'find the row for this contract number
Set f = shtMap.Range("A3:A100").Find(contractNumber, , xlValues, xlWhole)
If f Is Nothing Then
MsgBox "contract number " & contractNumber & " not found!"
Exit Sub
Else
Set mapRow = f.EntireRow
End If
'assumes input file path is in column B
Set wbInput = Workbooks.Open(mapRow.Cells(2).Value)
Set shtIn = wbInput.Sheets("VSR Input")
Set wbDest = ThisWorkbook
Set shtDest = wbDest.Sheets("Sheet1")
Application.ScreenUpdating = False
For x = 1 To 4
' "source" column letters are in columns C-F of the found row
cFrom = mapRow.Cells(2 + x).Value
' "destination" column letters are in C2:F2 of the Map sheet
cTo = shtMap.Rows(2).Cells(2 + x).Value
shtIn.Range(cFrom & "1").Resize(5004, 1).Copy shtDest.Range(cTo & "4")
Next x
With shtDest
usedRows = .Cells(.Rows.Count, "U").End(xlUp).Row
.Range("v3").Value = contractNumber
.Range("v4").Resize(usedRows - 3, 1).Value = contractNumber
End With
wbInput.Close False
End Sub

Related

Excel VBA create new sheet and copy text into cell

I have been using the below code successfully for years but recently it has stopped working. I've since upgraded to Office 365 and still no joy. Essentially the code should copy the Sheet "Response", paste a copy of a cell from "Database" and name the new sheet appropriately. It continues creating new sheets in the workbook until the end of the Database list.
If I run the code as is I get the following: "Run-time error '1004': Microsoft Excel cannot paste the data." When I look at the worksheets, evidentally the code runs and creates a sheet "Response4" (I've only given the database 4 lines to copy). Debug highlights the line ActiveSheet.Paste link:=True. I tested
Frustratingly the code works outside of my company's system (i.e., I sent it to a friend with dummy data and it worked perfectly fine).
Any suggestions very welcome!
Sub CopyCatView()
'NumResp = last row with a responses to the question held within the question 'Themes' database sheet
Dim NumResp As Integer
'x for looping variable
Dim x As Integer
'y for response number variable
Dim y As Integer
Dim ws As Worksheet
Sheets("Database").Activate
NumResp = Range("NumRowsD1").Value + 2
'NumRowsD1 is a named range comprising cell A1 on the Database sheet, which calculates by formula the number of comments in the database
For x = 3 To NumResp
Sheets("Response").Copy before:=Sheets("Response")
y = NumResp - x + 1
ActiveSheet.Name = "Response" & y
ActiveSheet.Range("C2").Value = Sheets("Database").Range("B" & x).Value
ActiveSheet.Range("AA5:CR5").Select
Selection.Copy
Sheets("Database").Select
Cells(x, 3).Select
ActiveSheet.Paste link:=True
Sheets("Response" & y).Activate
ActiveSheet.Range("F4").Select
Selection.Copy
Sheets("database").Select
Cells(x, 70).Select
ActiveSheet.Paste link:=True
'duplicates the Response sheet as many times as there are comments (=X), numbers them Response1 to ResponseX, copies each comment into the white box on a different response sheet from Response1 to ResponseX
'Also links through the check box reporting to the relevant row in the Database sheet
Next x
'at the end hide Sheet "Response"(deleting brings up prompts for every sheet deleted!)
Sheets("Response").Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("Database").Activate
Range("A1").Select
End Sub
Since the "paste with link" requires ranges to be selected before pasting, I'd skip that and create a method to perform that function.
Also - use worksheet variables to reduce the repetition in your code and make for easier maintenance.
Sub CopyCatView()
Dim NumResp As Long, x As Long, y As Long 'prefer Long over Integer
Dim wsDB As Worksheet, wsResp As Worksheet, ws As Worksheet
Set wsDB = ThisWorkbook.Worksheets("Database")
Set wsResp = ThisWorkbook.Worksheets("Response")
NumResp = wsDB.Range("NumRowsD1").Value + 2
For x = 3 To NumResp
wsResp.Copy before:=wsResp
Set ws = ThisWorkbook.Sheets(wsResp.Index - 1) 'get a reference to the copy
y = NumResp - x + 1
ws.Name = "Response" & y
ws.Range("C2").Value = wsDB.Range("B" & x).Value
LinkRanges ws.Range("AA5:CR5"), wsDB.Cells(x, 3)
LinkRanges ws.Range("F4"), wsDB.Cells(x, 70)
Next x
wsResp.Visible = False
wsDB.Activate
wsDB.Range("A1").Select
End Sub
'Link two ranges in the same workbook
' rngFrom = contiguous (single-area) source range
' rngTo = top-left cell of the destination range
Sub LinkRanges(rngFrom As Range, rngTo As Range)
Dim r As Long, c As Long, nm As String
If Not rngFrom.Parent Is rngTo.Parent Then
nm = "'" & rngFrom.Parent.Name & "'!"
End If
For r = 1 To rngFrom.Rows.Count
For c = 1 To rngFrom.Columns.Count
rngTo.Cells(r, c).Formula = "=" & nm & _
rngFrom.Cells(r, c).Address(False, False)
Next c
Next r
End Sub

Paste data in a specific row, depending on the number in column 43 (AQ)

My Problem seems simple but i'm just not able to solve it alone. My VBA Code copies Data, based on a Filter and past it to another Workbook, starting in Row 1. What i want to change now is, that the Code checks the Number in Column 43 (AQ) from every Row he copies and paste the Row into the Row that matches that Number. For Example: If the Number 7 stands in AQ than the Row should be pasted in row 7. If the Number 3 stands there, it should be pasted into Row 3. In between some Numbers are not included so it can be, that some Rows in between are staying empty.
My intially Code to Copy/Paste data, that worked, was this one.
Sub SG7Stundenplan()
SG7Stundenplan
Sheets("Gruppenplanung").Select
Columns.EntireColumn.Hidden = False
Range("A2").Select
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
ActiveSheet.Range("A2").AutoFilter Field:=19, Criteria1:="SG 7", Operator:=xlAnd
Rows("3:100").Select
Selection.Copy
Workbooks.Open "R:\BINplus\Langenthal\Stammgruppen\Stammgruppe 7\Stammgruppe 7_Unterrichtspläne Test.xlsm"
Worksheets("Stammdaten").Range("A1").PasteSpecial Paste:=xlPasteValues
Workbooks("Stammgruppe 7_Unterrichtspläne Test.xlsm").Close SaveChanges:=True
End Sub
I allready tried to create something new but i failed till now. Im pretty sure you need to copy the Rows singulary to put that trick off.
Sub SG7Stundenplan()
Dim Zelle As Range
Workbooks.Open "R:\BINplus\Langenthal\Stammgruppen\Stammgruppe 7\Stammgruppe 7_Unterrichtspläne Test.xlsm"
For Each Zelle In ThisWorkbook.Sheets("Gruppenplanung").UsedRange.Columns(19)
If Zelle.Text = "SG 7" Then
Zelle.EntireRow.Copy
Workbooks("Stammgruppe 7_Unterrichtspläne Test.xlsm").Worksheets("Stammdaten").Cells(Zelle.Offset(43 - Zelle.Column).Value, 1).PasteSpecial xlPasteValues
End If
Next
Workbooks("Stammgruppe 8_Unterrichtspläne Test.xlsm").Close True
End Sub
Thanks a lot for checking this Post and think about it. Your a great Community that allready helped me a lot.
Sincerely,
David from Switzerland
here is my best attempt at your problem.
My Input Sheet:
My Code:
Sub copyData()
Dim src As Worksheet: Set src = ThisWorkbook.Worksheets("Sheet1")
Dim wb As Workbook: Set wb = Workbooks.Open("c:\Desktop\Destination.xlsx")
Dim dest As Worksheet: Set dest = wb.Sheets("Sheet1")
Dim lastRow As Integer: lastRow = src.Cells(src.Rows.Count, 1).End(xlUp).Row
Dim lastCol As Integer: lastCol = src.Cells(1, src.Columns.Count).End(xlToLeft).Column
Dim r As Integer, sendRow As Integer
For r = 2 To lastRow
sendRow = src.Cells(r, 1).Value
src.Range(src.Cells(r, 1), src.Cells(r, lastCol)).Copy Destination:=dest.Range(dest.Cells(sendRow, 1), dest.Cells(sendRow, 1))
Next r
End Sub
My Output:
Good Luck!

Extract same data-points from multiple sheets that have similar names

I have an excel workbook that have a subset of sheets with similar names: Data(US), Data(UK), Data(FR), Data(ES), etc... (in brackets there is the ISO code of the country which data refer to).
I would like to extract two data-points from all these sheets, and print them as a list in a new sheet to create a report.
Each sheet has the same structure inside (see here an image:
so that all the data-points are identified by coordinates for rows ("001","002", ... in row 6) and columns ("001", "002" in column D).
I am trying to write a code that does the following:
Open all sheets that have the name like: Data(**)
Inside the sheet, do a VLOOKUP to get the value corresponding to rows "001" and "002"
Print the data-points extracted in a new sheet, one after the other as a list in column D
Here is the code I wrote so far, which works only for the first sheet (Data(US)) and I included my questions as comments:
Sub ImportDataPoints()
Dim W As Worksheet, C&, F$
Dim D As String
'Take the folder path from cell D1
D = Worksheets("Input").Range("D1").Value
On Error Resume Next
'Target sheet to paste the data
Set W = ThisWorkbook.Worksheets("Data")
C = 3
Application.ScreenUpdating = False
'Open all workbooks in the folder that contain the following
F = Dir(D & "*FINANCIALDATA*" & "*.xlsx")
Do Until F = ""
C = C + 1
'Open the worksheet "Data(US)"
'### QUESTION: How to open all worksheets with similar names like Data(**)? ###
With Workbooks.Open(D & F).Worksheets("Data(US)")
'First datapoint to extract. Initial position: cell AA10.
'Do a VLOOKUP to search the value corresponding to coordinate "001"
.Range("AA10").FormulaR1C1 = "=VLOOKUP(""001"",C[-23]:C[-1],2,FALSE)"
'Move to AB10: if the previous value is empty, then give me zero
.Range("AB10").FormulaR1C1 = "=IF(RC[-1]="""",0,RC[-1])"
'Copy the value
.Range("AB10").Copy
'Paste the value in the Target sheet at row 10, column D
W.Cells(10, C).PasteSpecial xlPasteValues
'Do the same for the second datapoint and paste it in the Target sheet at row 11, column D
.Range("AA10").Offset(1, 0).FormulaR1C1 = "=VLOOKUP(""002"",C[-23]:C[-1],2,FALSE)"
.Range("AB10").Offset(1, 0).FormulaR1C1 = "=IF(RC[-1]="""",0,RC[-1])"
.Range("AB10").Offset(1, 0).Copy
W.Cells(11, C).PasteSpecial xlPasteValues
'### QUESTION: The macro should continue opening all the other sheets (Data(UK), Data(FR), Data(ES), etc...),
'### copying the datapoints 001-002 and pasting them in the same target sheet (column D, continuing from the row 11 onwards...)###
.Parent.Close False
End With
F = Dir
Loop
Set W = Nothing
Application.ScreenUpdating = True
End Sub
Does anybody know how to fix the code? Or if you can think to any other more efficient solution, all proposal are well accepted!
Many thanks in advance!
Something like this:
Sub ImportDataPoints()
Dim W As Worksheet, C&, F$
Dim D As String, wb As Workbook, targetRow As Long, sht As Worksheet
'Take the folder path from cell D1
D = Worksheets("Input").Range("D1").Value
'Target sheet to paste the data
Set W = ThisWorkbook.Worksheets("Data")
C = 3
Application.ScreenUpdating = False
'Open all workbooks in the folder that contain the following
F = Dir(D & "*FINANCIALDATA*.xlsx")
Do Until F = ""
C = C + 1
Set wb = Workbooks.Open(D & F)
targetRow = 10 'summary start row
For each sht in wb.worksheets
If sht.Name Like "Data(*)" Then
'....
'run your code for sheet sht
'....
targetRow = targetRow + 2 'next summary row
End With
Next sht
wb.close True
F = Dir
Loop
Set W = Nothing
Application.ScreenUpdating = True
End Sub

Excel: Macro needed - 2 columns of data to become 1 column "every other"

Hello and first let me say thank you!
I use Excel to capture user requirements and descriptions. I then take that information and clean it up and paste into presentation docs, apply formatting, paste into Powerpoint, etc. It can be 100s of lines in total that this is done for. What I'm looking for is a macro that I can apply to data once it is pasted into Excel. The data will be text, non-numeric
I have a macro that I use to insert a blank row as every other row. I then do everything else manually (macro shown below).
What I'm looking for is a macro that inserts a blank row, then offsets Column 2 by 1 row down. then pastes column 1 into column 2(without copying the blank cells over my already existing data in column 2).
I've pasted a link to an image of what I'm looking for. I've also tried to show below (numbers are column 1, letters are column 2).
2 columns to 1 column - desired result
1 A 2 B3 C
Result I want:
1
A
2
B
3
C
My current "Blank Row" Macro:
Sub insertrow()
' insertrow Macro
Application.ScreenUpdating = True
Dim count As Integer
Dim X As Integer
For count = 1 To 300
If ActiveCell.Value <> "" Then
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(0, 0)).EntireRow.Insert
ActiveCell.Offset(1, 0).Select
For X = 1 To 1
Next X
Else
ActiveCell.Offset(1, 0).Range("a1").Select
End If
Next count
End Sub
This should work, but you'll have to adjust a little for your exact layout and needs.
Sub mergeColumns()
Dim mergedData As Variant
ReDim mergedData(1 To 600)
dataToProcess = Range("A2:B301")
For i = 1 To 300
mergedData(i * 2 - 1) = dataToProcess(i, 1)
mergedData(i * 2) = dataToProcess(i, 2)
Next i
Range("B2:B601") = WorksheetFunction.Transpose(mergedData)
End Sub
The following does what you need without inserting blank rows. It also calculates what the last row is on the sheet that has 2 columns so that you don't need to hard-code when the loop will end.
The comments should help you understand what is happening each step of the way. You can then modify this to work with your particular workbook. There are a lot of ways you could go about this. I chose to put the pivoted result on a second sheet.
Sub PivotTwoColumnsIntoOne()
Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet
Dim rng As Range
Dim cell As Range
Dim lastRow As Long
Dim targetRow As Long
Set wb = ThisWorkbook
' set our source worksheet
Set src = wb.Sheets("Sheet1")
' set our target sheet (where the single column will be)
Set tgt = wb.Sheets("Sheet2")
' get the last row on our target sheet
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
' set the starting point for our target sheet
targetRow = 1
Set rng = src.Range("A1:A" & lastRow)
For Each cell In rng
With tgt.Range("A" & targetRow)
' get the value from the first column
.Value = cell.Value
' get the value from the second column
.Offset(1).Value = cell.Offset(, 1).Value
.HorizontalAlignment = xlLeft
End With
targetRow = targetRow + 2
Next cell
End Sub

Copy/paste a row from one worksheet to another produces type mismatch error

This macro is to move records from a master sheet to other sheets based on criteria from column F.
A type mismatch error occurs in the "Termination" case where it is selecting the cell "B2".
I tried several different options, but each ends up with a different error.
Public Sub moveToSheet()
Sheets("Master").Select
' Find the last row of data
FinalRow = Range("E65000").End(xlUp).Row
'Loop through each row
For x = 2 To FinalRow
' Decide where to copy based on column F
ThisValue = Range("F" & x).Value
Select Case True
Case ThisValue = "Hiring "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Hiring").Select
Sheets("Hiring").Range("B2:W2500").Clear
Sheets("Hiring").Cells("B2").Select
ActiveSheet.Paste
Sheets("Master").Select
Case ThisValue = "Re-Hiring "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Hiring").Select
Sheets("Hiring").Range("B2:W2500").Clear
Sheets("Hiring").Cells("B2").Select
ActiveSheet.Paste
Case ThisValue = "Termination "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Terminations").Select
Sheets("Terminations").Range("B2:W2500").Clear
Sheets("Terminations").Cells("B2").Select
ActiveSheet.Paste
Case ThisValue = "Transfer "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Transfers").Select
Sheets("Transfers").Range("B2:W2500").Clear
Sheets("Transfers").Cells("B2").Select
ActiveSheet.Paste
Case ThisValue = "Name Change "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Name Changes").Select
Sheets("Name Changes").Range("B2:W2500").Clear
Sheets("Name Changes").Cells("B2").Select
ActiveSheet.Paste
Case ThisValue = "Address Change "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Address Changes").Select
Sheets("Address Changes").Range("B2:W2500").Clear
Sheets("Address Changes").Cells("B2").Select
ActiveSheet.Paste
Case Else
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("New Process").Select
Sheets("New Process").Range("B2:W2500").Clear
Sheets("New Process").Cells("B2").Select
ActiveSheet.Paste
End Select
Next x
End Sub
There are a couple problems, first, you need to use the syntax Range("B2").Select to select the cell. BUT, since you selected the entire row from the master sheet, you can't copy the entire row into B2, because the ranges aren't the same size, so you need to select the first cell (A2) instead.
So, the entire case statement should look like this:
Case ThisValue = "Termination "
Sheets("Master").Cells(x, 2).EntireRow.Copy
Sheets("Terminations").Activate
Range("A2").Select
ActiveSheet.Paste
There are a number of issues
No need to Select, use variables instead
Dim all your variables - help with debugging and learning
Some general good practice techniques will help
Here's a (partially) refactored version of your code
Public Sub moveToSheet()
Dim wb As Workbook
Dim shMaster As Worksheet, shHiring As Worksheet
Dim rngMaster As Range
Dim x As Long
Dim rw As Range
Set wb = ActiveWorkbook
Set shMaster = wb.Worksheets("Master")
Set shHiring = wb.Worksheets("Hiring")
' etc
' Find the data
x = shMaster.UsedRange.Count ' trick to reset used range
Set rngMaster = shMaster.UsedRange
'Loop through each row NOTE looping thru cells is SLOW. There are faster ways
For Each rw In rngMaster.Rows
' Decide where to copy based on column F
Select Case Trim$(rw.Cells(1, 6).Value) ' Is there really a space on the end?
Case "Hiring"
shHiring.[B2:W2500].Clear
rw.Copy shHiring.[B2]
' Case ' etc
End Select
Next rw
This is what I basically use to do exactly what you are talking about. I have a "master" sheet that is several thousand rows and a couple hundred columns. This basic version only searches in Column Y and then copies rows. Because other people use this, though, I have several template worksheets that I keep very hidden so you can edit that out if you don't want to use templates. I also can add additional search variables if needed and simply adding in another couple of lines is easy enough. So if you wanted to copy rows that match two variables then you'd define another variable Dim d as Range and Set d = shtMaster.Range("A1") or whatever column you wanted to search the second variable. Then on the If line change it to If c.Value = "XXX" and d.Value = "YYY" Then . Finally make sure you add an offset for the new variable with the c.offset (so it would have a line Set d = d.Offset(1,0) at the bottom with the other). It really has turned out to be pretty flexible for me.
Sub CreateDeptReport(Extras As String)
Dim shtRpt As Excel.Worksheet, shtMaster As Excel.Worksheet
Dim LCopyToRow As Long
Dim LCopyToCol As Long
Dim arrColsToCopy
Dim c As Range, x As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error GoTo Err_Execute
arrColsToCopy = Array(1, 3, 4, 8, 25, 25, 21, 16, 17, 15, 31, 7) 'which columns to copy ?
Set shtMaster = ThisWorkbook.Sheets("MasterSheet")
Set c = shtMaster.Range("Y5") 'Start search in Column Y, Row 5
LCopyToRow = 10 'Start copying data to row 10 in Destination Sheet
While Len(c.Value) > 0
'If value in column Y equals defined value, copy to destination sheet
If c.Value = “XXX” Then
'only create the new sheet if any records are found
If shtRpt Is Nothing Then
'delete any existing sheet
On Error Resume Next
ThisWorkbook.Sheets("Destination").Delete
On Error GoTo 0
ThisWorkbook.Sheets("Template").Visible = xlSheetVisible
ThisWorkbook.Sheets("Template").Copy After:=shtMaster
Set shtRpt = ThisWorkbook.Sheets(shtMaster.Index + 1)
shtRpt.Name = "Destination" 'rename new sheet to Destination
‘Optional Information; can edit the next three lines out -
Range("F1").Value = "Department Name"
Range("F2").Value = "Department Head Name"
Range("B3").Value = Date
ThisWorkbook.Sheets("Template").Visible = xlSheetVeryHidden
End If
LCopyToCol = 1
shtRpt.Cells(LCopyToRow, LCopyToCol).EntireRow.Insert shift:=xlDown
For x = LBound(arrColsToCopy) To UBound(arrColsToCopy)
shtRpt.Cells(LCopyToRow, LCopyToCol).Value = _
c.EntireRow.Cells(arrColsToCopy(x)).Value
LCopyToCol = LCopyToCol + 1
Next x
LCopyToRow = LCopyToRow + 1 'next row
End If
Set c = c.Offset(1, 0)
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Range("A9").Select 'Position on cell A9
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
Also, if you wanted then you could remove the screenupdating lines. As stupid as it sounds some people actually like to see excel working at it. With screenupdating off you don't get to see the destination sheet until the copying is completed, but with updating on the screen flickers like crazy because of it trying to refresh when each row is copied. Some of the older people in my office think that excel is broken when they can't see it happening so I keep screenupdating on most of the time. lol
Also, I like having the templates because all of my reports have quite a few formulas that need to be calculated after the information is broken down so I am able to keep all the formulas where I want them with a template. Then all I have to do is run the macro to pull from the master sheet and the report is ready to go without any further work.

Resources