Copy and Paste Data to VBA created sheets - vba

Working in an Excel document that I didn't design.
I am trying to automate raw data into an report type spreadsheet.
In short. I have code that does everything I need it to as far as formatting, moving columns, calculations, lookups and etc. I even have it creating new sheets based off of data that is in a certain column. The goal is for there to be sheets for every executive that has their data on it and only their data. While maintaining a sheet that has all data on it. So I need to copy and past only their data to their Sheet. I am really close....I think.
Currently the code creates the correct sheets, it even names them correctly. However, it moves the data incorrectly. For example I expect there to 15 records on sheet 2, but there is 10 I expect and 17 random others. Also, you can run the macro twice and get different results on the sheets.
I have exhausted two other people, and several search's today. I have no idea how to fix it. There is a lot of formatting code above this code. I am a basic user of VBA. I can do a good bit of things with it, but this code came from a colleague who has more experience, but then he couldn't figure out why it did what its doing. I'm running out of time. So I really would appreciate any help.
The code is as below.
'create new sheets
On Error GoTo ErrHandle
Dim vl As String
wb = ActiveWorkbook.Name
cnt = Application.WorksheetFunction.CountA(ActiveWorkbook.Sheets("Sheet1").Range("S:S"))
For i = 2 To cnt
vl = Workbooks(wb).Sheets("Sheet1").Cells(i, 19).Value
WS_Count = Workbooks(wb).Worksheets.Count
a = 0
For j = 1 To WS_Count
If vl = Workbooks(wb).Worksheets(j).Name Then
a = 1
Exit For
End If
Next
If a = 0 Then
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = vl
Sheets("Sheet1").Activate
Range("A1:V1").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets(vl).Activate
Range("A1").Select
ActiveSheet.Paste
End If
Next
Sheets("Sheet1").Activate
j = 2
old_val = Cells(2, 19).Value
For i = 3 To cnt
new_val = Cells(i, 19).Value
If old_val <> new_val Then
Range("A" & j & ":V" & i).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets(old_val).Activate
Range("A2").Select
ActiveSheet.Paste
Sheets("Sheet1").Activate
old_val = Cells(i + 1, 19).Value
j = i + 1
End If
Next
On Error GoTo ErrHandle
Worksheets("0").Activate
ActiveSheet.Name = "External Companies"
Worksheets("Sheet1").Activate
ActiveSheet.Name = "All Data"
Worksheets("All Data").Activate
Range("A1").Select
Workbooks("PERSONAL.xlsb").Close SaveChanges:=False
ActiveWorkbook.SaveAs ("Indirect_AVID_Approval")
Exit Sub
ErrHandle:
MsgBox "Row: " & i & " Value =:" & vl
End Sub
My apologies, I know I'm a messy code writer. If you couldn't tell, I'm mostly self taught.
Thanks in advance.

If you are not filtering the data you don't need to use SpecialCells(xlCellTypeVisible). I use a function getWorkSheet to return a reference to the new worksheet. If the SheetName already exists the function will return that worksheet otherwise it will create a new worksheet rename it SheetName and return the new worksheet.
Sub ProcessWorksheet()
Dim lFirstRow As Long
Dim SheetName As String
Dim ws As Worksheet
With Sheets("Sheet1")
cnt = WorksheetFunction.CountA(.Range("S:S"))
For i = 2 To cnt
If .Cells(i, 19).Value <> SheetName Or i = cnt Then
If lFirstRow > 0 Then
Set ws = getWorkSheet(SheetName)
.Range("A1:V1").Copy ws.Range("A1")
.Range("A" & lFirstRow & ":V" & i - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A2")
End If
SheetName = .Cells(i, 19).Value
lFirstRow = i
End If
Next
End With
Worksheets("0").Activate
ActiveSheet.Name = "External Companies"
Worksheets("Sheet1").Activate
ActiveSheet.Name = "All Data"
Worksheets("All Data").Activate
Range("A1").Select
Workbooks("PERSONAL.xlsb").Close SaveChanges:=False
ActiveWorkbook.SaveAs ("Indirect_AVID_Approval")
End Sub
Function getWorkSheet(SheetName As String) As Worksheet
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(SheetName)
If ws Is Nothing Then
Set ws = Worksheets.Add(after:=ActiveSheet)
ws.Name = SheetName
End If
On Error GoTo 0
Set getWorkSheet = ws
End Function

Related

How to have sum formula VBA for a specific row on each sheet?

Hello so a part of my macro is going through each sheet and putting the sum of that column at the end of it on each of the sheets. But currently it is printing the sum as is, I also want it to show the formula.
Sub GoNext()
Dim i As Integer
For Each ws In ThisWorkbook.Worksheets
Columns("E:E").Select
Selection.NumberFormat = "$#,##0.00"
i = ActiveSheet.Index + 1
If i > Sheets.Count Then i = 1
Sheets(i).Activate
If ActiveSheet.Name <> "CPOA Report Macro" Then
If ActiveSheet.Name <> "Summary" Then
LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row + 1
Range("E" & LastRow).Font.Bold = True
Range("E" & LastRow) = WorksheetFunction.Sum(Range("E2:E" & LastRow - 1))
End If
End If
Next ws
End Sub
I suggest to avoid using .Select and .Activate: See How to avoid using Select in Excel VBA. You can access the sheets and ranges directly.
Also your If statements can be combined by using And.
And you can use .Formula to set the formula for a cell.
Option Explicit
Public Sub GoNext()
Dim LastRow As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Columns("E").NumberFormat = "$#,##0.00"
If ws.Name <> "CPOA Report Macro" And ws.Name <> "Summary" Then
LastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row + 1
ws.Range("E" & LastRow).Font.Bold = True
ws.Range("E" & LastRow).Formula = "=SUM(E2:E" & LastRow - 1 & ")"
End If
Next ws
End Sub
I recommend to use meaningful procedure names like Sub SetFormatAndSumFormula or something like this. That makes your code much easier to read.

excel: run time error 1004 in advanced filter

I have this sheet where I applied a series of subs to get what I want. The last one is an Advanced Filter.
You can see how my main sheet is below:
My criteria is C31:K32 and the results should be pasted from line 38. It gets the information from that sheet called AUX:
The complete code is below:
Sub FiltroAloc()
Dim i As Long
Dim j As Long
Dim Lastrow As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim res
Set ws1 = Sheets("CONSULTA")
Set ws2 = Sheets("BASE_TOTAL_ATUAL")
Set ws3 = Sheets("AUX")
For i = 1 To 100
For j = 1 To 11
If ((ws1.Cells(29, 5).Value < ws2.Cells(i, 7).Value) And (ws1.Cells(29, 6).Value > ws2.Cells(i, 7).Value)) Or ((ws1.Cells(29, 5).Value < ws2.Cells(i, 8).Value) And (ws1.Cells(29, 6).Value > ws2.Cells(i, 8).Value)) Then
ws3.Cells(i, j) = ws2.Cells(i, j).Value
ElseIf (ws1.Cells(29, 5) = "") And (ws1.Cells(29, 6) = "") Then
ws3.Cells(i, j) = ws2.Cells(i, j).Value
End If
Next j
Next i
Call Esvaziar
End Sub
Sub Esvaziar()
Dim r As Range, rows As Long, i As Long
Dim ws As Worksheet
Set ws = Sheets("AUX")
Set r = ws.Range("A1:K450")
rows = r.rows.Count
For i = rows To 1 Step (-1)
If WorksheetFunction.CountA(r.rows(i)) = 0 Then
r.rows(i).Delete
End If
Next
Call AutoFilter
End Sub
All my code works fine! After that, I started to record my Advanced Filter as a Macro with AutoFilter name.
When I finished to record it worked fine and load all information, because I recorded it with nothing in my criteria.
The problem is when I assing to my "Filtrar" button. It gave me
Run-time error '1004' - Method 'Range' of object'_Global' failed
And that is the code:
Sub AutoFilter()
'
' AutoFilter Macro
'
'
Sheets("AUX").Range("A1:K176").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:="CONSULTA!Criteria", CopyToRange:=Range("CONSULTA!Extract") _
, Unique:=False
ActiveWindow.SmallScroll Down:=-21
Range("G3").Select
End Sub
And the highlighted part:
Sheets("AUX").Range("A1:K176").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:="CONSULTA!Criteria", CopyToRange:=Range("CONSULTA!Extract") _
, Unique:=False
I tried a lot of things but I guess I'm missing something. I don't know where to find my range problem... Any suggestions will be appreciated.
Try it with CriteriaRange:=worksheets("CONSULTA").Range("Criteria") and CopyToRange:=worksheets("CONSULTA").Range("Extract")
Sheets("AUX").Range("A1:K176").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=worksheets("CONSULTA").Range("Criteria"), _
CopyToRange:=worksheets("CONSULTA").Range("Extract"), Unique:=False

Copying dynamic rows from one worksheet to another in VBA

I am trying to copy a number of rows from each worksheet to the worksheet called "renew" in the same workbook.
The rows are defined as between the key words of "Service Requests" and "Renewals".
so step 1: define those row numbers, and step 2: copy them to the Renew sheet.
I run into the problem with step 2, somehow, i couldn't work out how to use the rownumber1 and rownumber2 in the copy command.
Any help would be appreciated. Thanks!
Sub test()
Dim ws As Worksheet
Application.ScreenUpdating = False
Sheets("Renew").Activate
For Each ws In Worksheets
If ws.Name <> "Renew" Then
For i = 1 To 100
Dim rownumber1 As Integer
Dim rownumber2 As Integer
If Range("A" & i).Text = "Service Requests" Then
rownumber1 = i
ElseIf Range("A" & i).Text = "Renewals" Then
rownumber2 = i
End If
Next i
'copy rows between rownumber1 and rownumber2 to the renew sheet
ws.Rows("rownumber1:rownumber2").EntireRow.Copy
ActiveSheet.Paste Range("A65536").End(xlUp).Offset(1, 0)
End If
Next ws
End Sub
update:
Sub test2()
Dim ws As Worksheet
Dim rownumber1 As Integer
Dim rownumber2 As Integer
Dim FoundCell As Excel.Range
Application.ScreenUpdating = False
Sheets("Renew").Activate
For Each ws In Worksheets
If ws.Name <> "Renew" Then
Set FoundCell = ws.Range("A:A").Find(what:="Service Requests", lookat:=xlWhole)
If Not FoundCell Is Nothing Then
rownumber1 = FoundCell.Row
End If
Set FoundCell = ws.Range("A:A").Find(what:="Renewals", lookat:=xlWhole)
If Not FoundCell Is Nothing Then
rownumber2 = FoundCell.Row
End If
'copy renewals to the renewalsummary
ws.Rows(rownumber1 & ":" & rownumber2).EntireRow.Copy
ActiveSheet.Paste Range("A65536").End(xlUp).Offset(1, 0)
End If
Next ws
End Sub
What you're looking for is:
ws.Rows(rownumber1 & ":" & rownumber2).EntireRow.Copy
Although, there are some other things to consider with your code. It may be a work in progress so I only answered your question, but:
Your loop is going to return row 100 every time, so I'm curious what the point of your loop is.
You should never DIM in a loop, since you can only declare a variable once and the loop will attempt to do it every time and should throw an error (Dim your rownumber variables with your ws variable).
Why loop to 100? You should loop to the end of the list of values.
Reply to Edits
It looks pretty good. the main thing is that it works. Although I would change this:
ActiveSheet.Paste Range("A65536").End(xlUp).Offset(1, 0)
to this:
ActiveSheet.Paste Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Hard-coded values aren't very future-proof, this looks at the last row of the sheet (whatever that might be). If any of your sheets start to reach the row max you need to do this:
If Cells(Rows.Count, 1) <> "" Then
ActiveSheet.Paste Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Else
MsgBox("Sheet " & ws.Name & " is full, row cannot be copied")
End If
But that's probably way down the road and at that point you might be outgrowing Excel.

VBA Macro stumbles on workbook name IF command

This code I have cobbled together for collating content from a named worksheet from all open workbooks seems to run fine on my computer, but not on the clients.
Whats going wrong here? I believe we are running the same version of excel, and using identical workbooks to test with.
It gets stuck on line 22:
wkb.Worksheets(sWksName).Copy _
Before:=ThisWorkbook.Sheets(1)
Sorry I don't have the error message!
Sub CopyandCollateQuery1()
With Application ' Scrubs settings that slow process
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With
Dim wkb As Workbook ' Dim Variables
Dim sWksName As String
Dim Title1 As Range
Dim Title1end As Range
Dim NewRng As Range
Dim check As String
sWksName = "Query1" ' Sets Worksheet to be collated
For Each wkb In Workbooks ' Pulls said worksheet title from each open workbook and copies into macro workbook
If wkb.Name <> ThisWorkbook.Name Then
wkb.Worksheets(sWksName).Copy _
Before:=ThisWorkbook.Sheets(1)
End If
Next
Set wkb = Nothing
For Each ws In ThisWorkbook.Worksheets
With ws
If .Name <> "Collated" Then
rowscount = .Cells(ws.Rows.Count, 2).End(xlUp).Row
.Range("B3" & ":" & "B" & rowscount).Copy
Worksheets("Collated").Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End With
Next ws
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
If ActiveSheet.Cells(1, 1).Value = "" Then
Rows(1).Delete
ActiveSheet.Cells(1, 2).Value = "Total Combined Count"
End If
ActiveSheet.Cells(1, 1).Activate
For Each ws In ThisWorkbook.Worksheets
With ws
Set lol = ws.Name
If .Name <> "Collated" Then
i = 4
Do While i < rowscount + 1
check = .Range("B" & i).Value
checknum = .Range("B" & i).Offset(0, -1).Value
Sheets("Collated").Activate
Worksheets("Collated").Range("A:A").Find(check, LookAt:=xlWhole).Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = ActiveCell.Value + checknum
checknum = 0
i = i + 1
Loop
End If
End With
Next ws
With Application ' undoes initial processes scrub
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
End With
End Sub
It's also having trouble finding the correct last row when performing the collating action, so I will need to adjust that. But that's beside the point.
As mentioned in your code the For loop For Each wkb In Workbooks is used to Pull said worksheet title from each open workbook and and copy into macro workbook. That means it looks for the sheet Query1 in all the open workbooks and when any of the workbook do not have a sheet named Query1 it throws Subscript out of range error.
You can tackle this error in two ways:
1. Make sure all your workbooks has sheet Query1 (don't think this will always happen)
2. Use error handling in your code
For Each wkb In Workbooks
If wkb.Name <> ThisWorkbook.Name Then
On Error Resume Next '<--- add this line in your code
wkb.Worksheets(sWksName).Copy _
Before:=ThisWorkbook.Sheets(1)
End If
Next
On Error Resume Next resumes execution ignoring any error thrown on the next line of code. Please note that On Error Resume Next does not in any way "fix" the error. It simply instructs VBA to continue as if no error occurred. For details see this link.

Throwing 1004 Error Only On PC

VBA macro throwing Run-time error '1004':
PasteSpecial method of Range class failed
This error is only being thrown when the macro runs on PC. On a Mac, the macro runs seamlessly. Is there any reason the below macro would throw an error?
Option Explicit
Sub DCR()
Dim J As Integer
Dim K As Integer
Dim L As Range
Dim sDay As String
Dim sMonth As String
Dim sTemp As String
Dim iTarget As Integer
Dim dBasis As Date
Dim Wb As Workbook
Dim Wb2 As Workbook
Set Wb = ThisWorkbook
Set L = Sheets("Sheet1").Range("A1:G7")
L.Copy
For Each Wb2 In Application.Workbooks
Wb2.Activate
Next
iTarget = 13
While (iTarget < 1) Or (iTarget > 12)
iTarget = Val(InputBox("Numeric month?"))
If iTarget = 0 Then Exit Sub
Wend
Set Wb2 = Workbooks.Add
Application.ScreenUpdating = False
Application.DisplayAlerts = False
sTemp = Str(iTarget) & "/1/" & Year(Now())
dBasis = CDate(sTemp)
For J = 1 To 31
sDay = Format((dBasis + J - 1), "dddd mm-dd-yyyy")
sMonth = Format((dBasis), "yyyy-mm")
If Month(dBasis + J - 1) = iTarget Then
If J > Sheets.Count Then
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = sDay
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
Else
If Left(Sheets(J).Name, 5) = "Sheet" Then
Sheets(J).Name = sDay
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = sDay
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
End If
End If
End If
Next J
For J = 1 To (Sheets.Count - 1)
For K = J + 1 To Sheets.Count
If Right(Sheets(J).Name, 10) > _
Right(Sheets(K).Name, 10) Then
Sheets(K).Move Before:=Sheets(J)
End If
Next K
Next J
Sheets(1).Activate
Application.ScreenUpdating = True
Wb2.SaveAs Filename:="DCR_" + sMonth + ".xlsx"
'
End Sub
The reason for the error is that you are prematurely copying the source range to the clipboard, and somehow by the time when you try to paste the source range to the corresponding worksheet the clipboard is empty thus giving the error 1004. As to why the Mac does not give an error I have no idea, probably none of the actions performed between the L.Copy and the .PasteSpecial clears the clipboard or whatever the Mac uses. Nevertheless, it’s a bad practice to keep the items to be copied that long in the clipboard.
I have also done a review of your code and highlighted some points for improvement (see comments below)
Set Wb = ThisWorkbook 'Here you set the Wb variable but is not used at all in the entire procedure
Set L = Sheets("Sheet1").Range("A1:G7") 'Here was an opportunity to use the `Wb` variable instead this line points to whatever workbook is active
'This is the cause of the error: here you copy `A1:G7` to the clipboard (1\2)
L.Copy
'This Loop Through All Open Workbooks Seems To Have No Purpose!
For Each Wb2 In Application.Workbooks
Wb2.Activate
Next
'This is not efficient, if the user does not enter neither a valid number nor a zero it will go endlessly
'Also suggest to use Do...Loop for the reasons mentioned in the Tip of the page While...Wend Statement (see suggested pages)
iTarget = 13
While (iTarget < 1) Or (iTarget > 12)
iTarget = Val(InputBox("Numeric month?"))
If iTarget = 0 Then Exit Sub
Wend
'This way of setting the date is not efficient as it depends on knowing the date format used by the user machine
'Sugest to use instead the DateSerial Function (see suggested pages)
sTemp = Str(iTarget) & "/1/" & Year(Now())
dBasis = CDate(sTemp)
If J > Sheets.Count Then
Sheets.Add.Move after:=Sheets(Sheets.Count)
'These lines are repeated for each "situation" of the sheets (three times)
ActiveSheet.Name = sDay
'This is the cause of the error(2\2): here you try to paste from an empty clipboard
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
Else
If Left(Sheets(J).Name, 5) = "Sheet" Then
Sheets(J).Name = sDay
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
Else
Sheets.Add.Move after:=Sheets(Sheets.Count)
ActiveSheet.Name = sDay
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteValues
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteFormats
Wb2.Sheets(J).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Value = sDay
End If
End If
End If
Next J
'This sort is redundant, instead have a more efficient process to add the required worksheets
For J = 1 To (Sheets.Count - 1)
For K = J + 1 To Sheets.Count
If Right(Sheets(J).Name, 10) > _
Right(Sheets(K).Name, 10) Then
Sheets(K).Move Before:=Sheets(J)
End If
Next K
Next J
Sheets(1).Activate
Application.ScreenUpdating = True
'Missed to restate the `Application.DisplayAlerts = True`
'This is very dangerous as the system will not advise when closing a workbook without saving it first.
'And it will result in losing all work done on that workbook!
'This will give an error if by any chance a workbook with same name is open
Wb2.SaveAs Filename:="DCR_" + sMonth + ".xlsx"
This is the revised code.
For a deeper understanding of the resources used suggest to visit these pages:
Application Members (Excel),
On Error Statement,
DateSerial Function
While...Wend Statement,
Do...Loop Statement,
With Statement
Option Explicit
Sub DCR()
Dim rSrc As Range 'Source Range to be copied
Dim WbkTrg As Workbook 'Target Workbook to act upon
Dim sWbkTrg As String 'Target Workbook name
Dim WshTrg As Worksheet 'Target Worksheet to act upon
Dim sWshTrg As String 'Target Worksheet name
Dim bMonth As Byte
Dim dDate As Date
Dim bDay As Byte
Dim b As Byte
Rem Application Settings OFF
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Rem Get User Input
Do
On Error Resume Next
bMonth = InputBox("Enter month number (1 to 12) or 0 to cancel.")
On Error GoTo 0
b = 1 + b
If bMonth = 0 Then GoTo ExitTkn
If b = 3 Then GoTo ExitTkn
Loop Until bMonth >= 1 And bMonth <= 12
Rem Set Target Range To Be Copied Into New Workbook
Set rSrc = ThisWorkbook.Sheets("Sheet1").Range("A1:G7")
Rem Add Target Workbook
Set WbkTrg = Workbooks.Add
sWbkTrg = "DCR_" & Format(DateSerial(Year(Now), bMonth, 1), "yyyy-mm") & ".xlsx"
Rem Delete All Worksheets Minus One In Target Workbook
Do
With WbkTrg
If .Sheets.Count = 1 Then Exit Do
.Sheets(1).Delete
End With
Loop
Rem Add Worksheet for each day of the month
For bDay = 1 To 31
Rem Set Date & Month
dDate = DateSerial(Year(Now), bMonth, bDay)
sWshTrg = Format(dDate, "dddd mm-dd-yyyy")
If Month(dDate) = bMonth Then
Rem Process Worksheets - Days
With WbkTrg
If bDay = 1 Then
Rem Process 1st Day
Set WshTrg = .Sheets(bDay)
Else
Rem Add Remaining Days
Set WshTrg = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End If: End With
Rem Update Day Standard Data
WshTrg.Name = sWshTrg
With WshTrg.Range("A1")
rSrc.Copy
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.Value = sWshTrg
Application.CutCopyMode = False
End With
End If: Next
Rem Save Target Workbook
Application.Goto WbkTrg.Sheets(1).Cells(1), 1
On Error Resume Next
Workbooks(sWbkTrg).Close 'Close Workbook If Open
On Error GoTo 0
WbkTrg.SaveAs Filename:=sWbkTrg
ExitTkn:
Rem Application Settings ON
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub