VBA copy and paste corresponding data onto new sheet - excel

I am trying to use VBA to ask the user to enter a date. For that date, copy all appointments and paste them on sheet “Daily Appts”, one-by-one. My reference sheet that contains all the data corresponding to given dates is named "Appts". I attached a picture for reference. Leading up to this, I created worksheet "Daily Sheet" and copy and pasted the headers from "Appts" onto it. I am trying to get every value for the date entered to copy and paste onto the new sheet but I am stuck. For example, if user enters 10/01/2018, it will have multiple sets of data that needs to be copied over. Here is what I have so far. Step 6 is where I need help to complete the task. 1: https://i.stack.imgur.com/vEtUd.png
'Step 1:
Sub Part2()
Dim sheet As Variant
'Step 2: Add code to delete sheet "Daily Appts", if exist.
For Each sheet In ActiveWorkbook.Worksheets
If sheet.Name = "Daily Appts" Then
Application.DisplayAlerts = False
Worksheets("Daily Appts").Delete
Application.DisplayAlerts = True
End If
Next sheet
'Step 3: Add code to add a new sheet, name it "Daily Appts"
Sheets("Main").Select
Sheets.Add After:=ActiveSheet
ActiveSheet.Select
ActiveSheet.Name = "Daily Appts"
Sheets("main").Select
'Step 4: Add code to select the worksheet "Appts". Name the range that holds
'the title (first row), the range that contains the data, and the range
'contains the schedule.
Sheets("Appts").Select
Dim Title As Range, Data As Range, Schedule As Range
Set Title = Range("A1", Range("A1").End(xlToRight))
Title.Name = "Title"
Set Data = Range("A2", Range("A2").End(xlDown).End(xlToRight))
Data.Name = "Data"
Set Schedule = Range("J2", Range("J2").End(xlDown))
Schedule.Name = "Schedule"
'Step 5: Add code to copy and paste the title into the new sheet, "Daily
'Appts".
Sheets("Appts").Range("Title").Copy 'Copy the data
Sheets("Daily Appts").Activate 'Activate the destination worksheet
Range("A1").Select 'Select the target range
ActiveSheet.Paste 'Paste in the target destination
Application.CutCopyMode = False
'Step 6: Ask the user to enter a date. For that date, copy all appointments
'and paste them on sheet "Daily Appts", one-by-one.
Dim result As String, i As Long, mydate As Date
Sheets("Appts").Select
result = InputBox("Enter a date")
For i = 2 To 360
mydate = Cells(i, 10)
If mydate = result Then
Sheets("Appts").Range("J2").End(xlToLeft).Copy
Sheets("Daily Appts").Activate
Range("A2").End(xlDown).Select
ActiveSheet.Paste
End If
Next
End Sub

There is not reason to loop to test whether a single sheet exists a simple test if a cell reference is valid will do it:
If Not IsError(Application.Evaluate("'Daily Appts'!A1")) Then Worksheets("Daily Appts").Delete
Also by declaring a worksheet variable on the addition it make it easier to work with the sheet later:
Dim ws As Worksheet
Set ws = Worksheets.Add(After:=Worksheets("Main"))
ws.Name = "Daily Appts"
Then there is no need for the range.name as you created the ranges as variables just refer to them.
Then in the loop you need to iterate the copy ranges.
I also cleaned up the .Activate and .Select which should be avoided.
Sub Part2()
'Step 2: Add code to delete sheet "Daily Appts", if exist.
If Not IsError(Application.Evaluate("'Daily Appts'!A1")) Then Worksheets("Daily Appts").Delete
'Step 3: Add code to add a new sheet, name it "Daily Appts"
Dim ws As Worksheet
Set ws = Worksheets.Add(After:=Worksheets("Main"))
ws.Name = "Daily Appts"
'Step 4: Add code to select the worksheet "Appts". Name the range that holds
'the title (first row), the range that contains the data, and the range
'contains the schedule.
With Worksheets("Appt")
Dim lCol As Long
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Dim lRow As Long
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim Title As Range
Set Title = .Range(.Cells(1, 1), .Cells(1, lCol))
'Step 5: Add code to copy and paste the title into the new sheet, "Daily
'Appts".
Title.Copy ws.Range("A1") 'Paste in the target destination
'Step 6: Ask the user to enter a date. For that date, copy all appointments
'and paste them on sheet "Daily Appts", one-by-one.
Do
Dim result As String
result = InputBox("Enter a date")
If Not IsDate(result) Then MsgBox ("must be date")
Loop Until IsDate(result)
For i = 2 To lRow
If .Cells(i, 10).Value2 = CDate(result) Then
.Range(.Cells(i, 1), .Cells(i, lCol)).Copy ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1)
End If
Next
End With
End Sub

Related

Looking to find the last row on my sheet where there is another record of it, then copy and paste data below

I am in the process of trying to create a Macro so that we can press a button and it updates the whole sheet.
Essentially all my data is being collected from another workbook, but it has to be non macro hence all my data is pulling through to my sheet Do Not Delete.
I have got my Macro to cycle through and copy/paste as values onto another sheet and remove all the rows that contain the text '#VALUE!'.
I have tried searching around on how to do this, but to no avail. I am trying to find out how to search each row on the 'Do Not Delete' sheet for the value that is in Column G on each row for anywhere that this exists elsewhere in the workbook, but I am unable to do this. From the point that I find the last record where it exists, I want to then copy down from there onwards.
Sub CopyToSheet()
'
' CopyToSheet Macro
Dim wb As Workbook
Dim ws, wscopy, wsdnd As Worksheet
Dim i, LastRowa, LastRowd As Long
Dim WSheet As String
Dim SheetName As String
Set wsdnd = Sheets("Do Not Delete")
Set wscopy = Sheets("CopyAndClear")
Set wb = ActiveWorkbook
Set ws = ActiveWorkbook.Sheets("Macro - Do not delete")
'Finding Sheet to use
SheetName = Range("L2")
Debug.Print Range("L2")
'Clear Contents
wscopy.Activate
wscopy.Cells.Clear
'Activating Do Not Delete Sheet to copy the data
wsdnd.Activate
LastRowa = wsdnd.Cells(Rows.Count, "A").End(xlUp).Row
wsdnd.Range("A1:IP" & LastRowa).Select
wsdnd.Range("A1:IP" & LastRowa).Copy
'Copy and paste cells onto new sheet
wscopy.Activate
wscopy.Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Apply Filter
Application.DisplayAlerts = False
LastRowc = wscopy.Cells(Rows.Count, "A").End(xlUp).Row
wscopy.Range("A1:IP" & LastRowc).AutoFilter Field:=1, Criteria1:="#VALUE!"
'Delete Rows
wscopy.Range("A1:IP" & LastRowc).SpecialCells(xlCellTypeVisible).Delete
'Clear Filter
On Error Resume Next
wscopy.ShowAllData
On Error GoTo 0
End Sub

Excel VBA remove blank rows from specific range

I have an excel macro that creates a new sheet called "Compiled", copies over the contents of every sheet in the workbook from A2 onward (so the header isn't copied). This works great, except I often get tons of completely blank rows all over the place.
My objective is to have a macro to find the last row in the Compiled sheet, and delete any fully blank rows.
Here's my current script:
Sub CombineData()
' Delete unneeded sheets
Application.DisplayAlerts = False
Sheets("Instructions").Select
ActiveWindow.SelectedSheets.Delete
Sheets("TM Contacts").Select
ActiveWindow.SelectedSheets.Delete
' Add new sheet called Compiled
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Compiled"
Sheets("Lastname, First Name").Select
Range("Table_1[#Headers]").Select
Selection.Copy
Sheets("Compiled").Select
Range("A1").Select
ActiveSheet.Paste
Range("A2").Select
' Copy all sheet contents onto one
Dim lastRowSource As Long, lastRowDest As Long, i As Long
For i = 1 To Sheets.Count
If Not Sheets(i).Name = "Compiled" Then
lastRowSource = Sheets(i).Cells(Sheets(i).Rows.Count, "A").End(xlUp).Row
lastRowDest = Sheets("Compiled").Cells(Sheets("Compiled").Rows.Count, "A").End(xlUp).Row
With Sheets(i)
.Range(.Cells(2, "A"), .Cells(lastRowSource, "AB")).Copy Sheets("Compiled").Range(Sheets("Compiled").Cells(lastRowDest + 1, "A"), Sheets("Compiled").Cells(lastRowDest + 1 + lastRowSource, "AB"))
End With
End If
Next i
' delete blank rows
End Sub
I tried this code from an older question to delete the blank rows, which gave me an "out of range" error:
Dim myWs As Worksheet
Set myWs = ThisWorkbook.Worksheets("Compiled") 'set your sheet name
Dim lastRow As Long
lastRow = myWs.Range("A" & myWs.Rows.Count).End(xlUp).Row 'find last used row
With myWs.Range(myWs.Cells(2, "A"), myWs.Cells(lastRow, "A"))
.Value = .Value 'convert formulas to values whithin the range from with block (column A only)
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'delete rows where column A is blank
End With
The error with this code appears to be at "Dim myWs As Worksheet". This is where I get the "out of range" error. I'm trying to point to the compiled worksheet.
If I am not wrong, you want to combine data from different worksheets into one master sheet. But your code is producing lots of empty rows in the "Compiled" sheet. That's why you want to "remove blank rows from specific range".
What I understand from your code:
you want to:
delete sheets named "Instructions" and "TM Contacts"
add a new sheet "Compiled"
copy header from the table "Table_1" in sheet "<Last Name, First Name>" and paste it as header for sheet "Compiled"
copy data "A2" to "AB & last row" from all sheets to sheet "Compiled", starting from "A2"
Please check if this works:
Here I have tried to avoid .select
Option Explicit
Sub CombineData()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim lastRowDest As Long
Dim lastRowSource As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'delete sheets named "Instructions" and "TM Contacts". also delete "Compiled", if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Instructions").Delete
ActiveWorkbook.Worksheets("TM Contacts").Delete
ActiveWorkbook.Worksheets("Compiled").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'add a new sheet "Compiled"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Compiled"
'copy header from the table "Table_1" in sheet "Last Name, First name" and paste it as header for sheet "Compiled"
'from your code I assume you have a data formatted as a table, "Table_1"
ActiveWorkbook.Worksheets("Last Name, First Name").ListObjects("Table_1").HeaderRowRange.Copy
DestSh.Range("A1").PasteSpecial xlPasteValues
'copy data "A2" to "AB & last row" from all sheets to sheet "Compiled",starting from "A2"
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
With DestSh
lastRowDest = .Range("A" & .Rows.Count).End(xlUp).Row
End With
With sh
lastRowSource = .Range("A" & .Rows.Count).End(xlUp).Row
End With
'if you want to change copy range, change here
Set CopyRng = sh.Range("A2:AB" & lastRowSource)
With CopyRng
DestSh.Cells(lastRowDest + 1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Create sheets based on unique values in rows based on Primary Key

I am trying to populate multiple sheets based on the given data (attached Samplesheet SampleSheet.xlsx) as per the below rules:
Customer Code is the primary key, there should be each sheet for each unique customer code.
The new sheets should be named as "CustomerCode_Leads"
Every worksheet should have same headers.
I have started up with a logic and build a code behind but am lacking the knowledge on how to read the customer code data line by line, copy the rows with the same customer code and paste it in the sheet based on unique customer code.
Code written so far:
Sub Test()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Data")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
Selection.AutoFilter
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
Application.Goto Reference:="R2C2"
ActiveCell.EntireColumn.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$5000").RemoveDuplicates Columns:=1, Header:=xlYes
Dim CurSheet As Worksheet
Dim Source As Range
Dim c As Range
Set CurSheet = ActiveSheet
Set Source = Selection.Cells
Application.ScreenUpdating = False
For Each c In Source
sName = Trim(c.Text)
If Len(sName) > 0 Then
Worksheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = sName + "_Leads"
End If
Next c
End Sub
Output Desired:
Can someone advise how to read the data row by row and paste it in a new worksheet named "CustomerCode_Lead" where "CustomerCode" is a variable with some values in the Data sheet.
The algorithm that I am following is:
Copy the datasheet and paste it into a new worksheet
Sort the data in ascending order based on Customer Code (it'll bring all the similar customer code together and ease the row by row reading)
Read the data row by row and copy the entire row and paste into a new sheet until the customer code stays the same, once different code arrives in the next row, it creates a new sheet named "CustomerCode_Leads"
Do the reading of data until the end of the data in the "Data" sheet.
I would absolutely thank you in advance for the help I'll get here from the community. :)
This is all you need:
Get all unique values of customer ID column
Filter data and copy to another sheet
It could look like below:
Option Explicit
Public Sub SplitDataByCustomerIntoSheets()
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Data")
Dim LastRow As Long
LastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
'creat unique list of customer codes (https://stackoverflow.com/questions/36044556/quicker-way-to-get-all-unique-values-of-a-column-in-vba)
Dim UniqueValues() As Variant
UniqueValues = wsData.Range("A2:A" & LastRow).Value
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim iRow As Long
For iRow = 1 To UBound(UniqueValues)
dict(UniqueValues(iRow, 1)) = Empty
Next
UniqueValues = WorksheetFunction.Transpose(dict.Keys())
'check if filter was already set
If wsData.FilterMode = False Then
wsData.Range("A1").AutoFilter
Else
wsData.ShowAllData
End If
Dim CustomerID As Variant
For Each CustomerID In UniqueValues 'loop through all customer IDs
With wsData.Range("A1:B" & LastRow) 'make sure to adjust B to the last column of your data
.AutoFilter Field:=1, Criteria1:=CustomerID 'filter by customer ID
'create a new sheet as last sheet and name it by customer ID
Dim NewSheet As Worksheet
Set NewSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
NewSheet.Name = CustomerID & "_Leads"
'copy visible cells of filtered data to new sheet
.SpecialCells(xlCellTypeVisible).Copy NewSheet.Range("A1")
End With
Next CustomerID
End Sub
Data sheet:
It will create a sheet for each customer ID like this:

Macro to copies value of a cell to another sheet but retain destination Format

I have a Sheet named "Daily Data" and One Sheet named "JPY Dly". I created a button and wrote a Macro to import data from another file and place into cells A1:D1 in "Daily Data".
I then need to Copy and Paste those VALUES into the next available cells in specific columns of Sheet "JPY Dly".
I used Offset in the Paste portion of the code but when the Paste occurs, the data does not keep the destination cells formatting. All I want is the VALUES of the cells in "Daily Data" to be copied over and for them to assume the pre-determined formatting of cells in "JPY Dly".
Here is the code that I am using.
Sub Import_DailyData()
Workbooks.Open "C:\Users\dbrown1\Downloads\exchange.csv"
'Opens the dowloaded file from the web
Workbooks("exchange.csv").Worksheets("exchange").Range("A8:AN9").Copy _
Workbooks("FOREX TEST.xlsm").Worksheets("Daily Data").Range("A1")
'Copies the daily data into FOREX Workbook
Workbooks("exchange.csv").Close SaveChanges:=False
'Closes the downloaded sheet without saving
Kill ("C:\Users\dbrown1\Downloads\exchange.csv")
'Insert the "Write to sheets" portion of the Sub in here
Worksheets("Daily Data").Range("A2").Copy Sheets("JPY Dly").Range("C2000").End(xlUp).Offset(1, 0)
Worksheets("Daily Data").Range("F2").Copy Sheets("JPY Dly").Range("E2000").End(xlUp).Offset(1, 0)
Worksheets("Daily Data").Range("G2").Copy Sheets("JPY Dly").Range("F2000").End(xlUp).Offset(1, 0)
Worksheets("Daily Data").Range("E2").Copy Sheets("JPY Dly").Range("G2000").End(xlUp).Offset(1, 0)
'Below this you will see the ClearContents portion of the code
Worksheets("Daily Data").Range("A1:AN2").ClearContents
End Sub
Can you please tell me how to paste the Values only from "Daily Data" and them assume the formatting of the cells in "JPY Dly"?
UPDATE
Here is updated code recommended by chrisnielsen and the screenshot from the downloaded "exchange"file.
Sub Import_DailyData()
Dim wbCSV As Workbook
Dim wsCSV As Worksheet
Dim wsDestination As Worksheet
Dim DestRow As Long
Set wbCSV = Workbooks.Open("C:\Users\dbrown1\Downloads\exchange.csv")
Set wsCSV = wbCSV.Worksheets("exchange")
'Opens the dowloaded file from the web
Set wsDestination = ThisWorkbook.Worksheets("JPY Dly")
'Copies the daily data into FOREX Workbook
'Closes the downloaded sheet without saving
'Insert the "Write to sheets" portion of the Sub in here
With wsDestination
DestRow = .Cells(.Rows.Count, 3).End(xlUp) + 1
' Copy data
.Cells(DestRow, 3).Value = wsCSV.Cells(2, 1).Value
.Cells(DestRow, 5).Value = wsCSV.Cells(2, 3).Value
.Cells(DestRow, 6).Value = wsCSV.Cells(2, 4).Value
.Cells(DestRow, 7).Value = wsCSV.Cells(2, 2).Value
End With
'Below this you will see the ClearContents portion of the code
'Worksheets("Daily Data").Range("A1:AN2").ClearContents
Workbooks("exchange.csv").Close SaveChanges:=False
'Kill ("C:\Users\dbrown1\Downloads\exchange.csv")
End Sub
While Copy/Paste Values will work, it's cleaner to use the values properties of the source and destination cells. This will retain destination cell formats.
Also, there are a number of other opertunities for improvement
Use Workbook and Workssheet references
No need for the intermediate Daily Data Sheet.
No need to repeat the .End(xlUp) bits
No need for the ( ) on the Kill line (in fact this has side effects that, while not a problem here, will eventually bite you)
Sub Import_DailyData()
Dim wbCSV As Workbook
Dim wsCSV As Worksheet
Dim wsDestination As Worksheet
Dim DestRow As Long
'Open the dowloaded file from the web, and get references
Set wbCSV = Workbooks.Open("C:\Users\dbrown1\Downloads\exchange.csv")
Set wsCSV = wbCSV.Worksheets("exchange")
' Reference the destination
Set wsDestination = ThisWorkbook.Worksheets("JPY Dly") ' Assuming FOREX TEST.xlsm contains this code
' If FOREX TEST.xlsm does not contains this code, use this instead of the previous line
'Set wsDestination = Application.Workbooks("FOREX TEST.xlsm").Worksheets("JPY Dly")
' get destination row
With wsDestination
DestRow = .Cells(.Rows.Count, 3).End(xlUp).Row + 1
' Copy data
.Cells(DestRow, 3).Value = wsCSV.Cells(2, 1).Value
.Cells(DestRow, 5).Value = wsCSV.Cells(2, 6).Value
.Cells(DestRow, 6).Value = wsCSV.Cells(2, 7).Value
.Cells(DestRow, 7).Value = wsCSV.Cells(2, 5).Value
End With
'Close and delete the downloaded workbook without saving
wbCSV.Close SaveChanges:=False
Kill "C:\Users\dbrown1\Downloads\exchange.csv"
End Sub
Building on ACCitonMan's comment to use paste special. The following code takes the text from cell A1 and pastes it into cell A2 while keeping whatever formatting is in cell A2.
Sub pasteSpec()
Dim ws As Excel.Worksheet
Dim cRng As Excel.Range
Dim pRng As Excel.Range
Set ws = ThisWorkbook.Worksheets(1)
Set cRng = ws.Range("A1")
Set pRng = ws.Range("A2")
cRng.Copy
pRng.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'stops marching ants when using .copy
End Sub
Additional paste types can be found in the documentation here.

Excel VBA - Copy from multiple sheets with condition and placing in certain cell in different sheets

I'm new to VBA Excel and I have some code that will go through multiple sheets and copy values in certain range of cells if the criteria are met.
So basically I would like to copy certain data from multiple sheets and paste it in certain cells (it must be placed based on the variable in the cells)
I would like to copy from sheet 1, 2, 3, etc., cell E to L and place it in another sheet, based on the value of cell L5:
And paste it to this sheet, in cell F to M, if the value of cell C in sheet Template 1 are the same with cell L5 in sheet 1,2,3,etc:
Here are the code that I have:
Option Explicit
'Note: This example use the function LastRow
'This example copy the range A2:G2 from each worksheet.
'
'Change the range here
'
''Fill in the range that you want to copy
'Set CopyRng = sh.Range("A2:G2")
'When you run one of the examples it will first delete the summary worksheet
'named RDBMergeSheet if it exists and then adds a new one to the workbook.
'This ensures that the data is always up to date after you run the code.
'*****READ THE TIPS on the website****
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
Dim i As Integer
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
'Application.DisplayAlerts = False
'On Error Resume Next
'ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
'On Error GoTo 0
'Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets("Template 1")
'DestSh.Name = "RDBMergeSheet"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
'Loop through all worksheets except the RDBMerge worksheet and the
'Information worksheet, you can ad more sheets to the array if you want.
If IsError(Application.Match(sh.Name, _
Array("Information", "Template 1", "Template 2", "Template 3"), 0)) Then
'Find the last row with data on the DestSh
Last = LastRow(DestSh)
'Fill in the range that you want to copy
Set CopyRng = sh.Range("E10:L10")
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats, if you only want to copy the
'values or want to copy everything look at the example below this macro
'For i = 2 To LastRow(DestSh)
CopyRng.Copy
With DestSh.Cells(Last + 1, "E")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
'Optional: This will copy the sheet name in the H column
'DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name
End If
Next
ExitTheSub:
Application.GoTo DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
The code above succeeded to copy from sheet 1,2,3,etc in cell range but place it in the last row and not yet based on the criteria.
I would like to know how to incorporate the criteria that I need to the code above? Thanks
This code will do for loops which find the sheet where the L5 value matches the C1 value in each of the Templates. Inside that if statement is where you can put the code you have that copies and pastes the values. Good luck!
Sub matchTemplateWithSheet()
'
' matchTemplateWithSheet Macro
'
'
Dim x As Integer
Dim y As Integer
Dim a As Integer
Dim b As Integer
Dim numberOfTemplates As Integer
Dim numberOfSheets As Integer
numberOfTemplates = 3 'you can set the number of templates you're trying to fill
numberOfSheets = 5 ' you can set the number of sheets you're looking through. _
this can also easily be automated in the code
For x = 1 To numberOfTemplates
a = ActiveWorkbook.Worksheets("Template " & x).Cells(1, 3)
For y = 1 To numberOfSheets
b = ActiveWorkbook.Worksheets("Sheet" & y).Cells(5, 12)
If a = b Then
'''''This is where you can put the copy/paste code that you already have'''''
End If
Next y
Next x
End Sub

Resources