VBA Macro stumbles on workbook name IF command - vba

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.

Related

Copy and paste rows from one to another worksheet VBA

I know already a few people had that problem but their solutions did not help me. I am pretty new to VBA and I want to copy a row if the respective first cell is not empty to another file and iterate as long as the data is.
So far so good. My code runs the first time and actually works (for one line). But then the macro does not open the file again and spits out an error. If I want to manually open the target file it says: "Removed Feature: Data Validation from /xl/worksheets/sheet2.xml part" (and I think this is the reason why it does not iterate further). Do you have any idea what I can do?
Sub transferData()
Dim LastRow As Long, i As Integer, erow As Long
LastRow = ActiveSheet.Range("BC" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If IsEmpty(Cells(i, 63).Value) = False Then
Range(Cells(i, 55), Cells(i, 63)).Select
Selection.Copy
Workbooks.Open Filename:="PATH.xlsx"
Worksheets("NewProjects").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.PasteSpecial
ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.Save
ActiveWorkbook.Close SaveChanges:=False
Application.CutCopyMode = False
End If
Next i
End Sub
Data Validation for the file is corrupt (dropdown lists) - either delete Data Validation, or fix it
Once the file is fixed, the code bellow will copy the data without opening the destination file multiple times. It AutoFilters current sheet for empty values in column BK (63), and copies all visible rows, from columns BC to BK, to the end of the new file (starting at first unused cell in column A)
Option Explicit
Public Sub TransferData()
Const OLD_COL1 = "BC"
Const OLD_COL2 = "BK"
Const NEW_COL1 = "A"
Dim oldWb As Workbook, oldWs As Worksheet, oldLR As Long
Dim newWb As Workbook, newWs As Worksheet, newLR As Long
On Error Resume Next 'Expected errors: new file not found, new sheet name not found
Set oldWb = ThisWorkbook
Set oldWs = ActiveSheet 'Or: Set oldWs = oldWb.Worksheets("Sheet2")
oldLR = oldWs.Cells(oldWs.Rows.Count, OLD_COL1).End(xlUp).Row
Application.ScreenUpdating = False
Set newWb = Workbooks.Open(Filename:="PATH.xlsx")
Set newWs = newWb.Worksheets("NewProjects")
If Not newWs Is Nothing Then
newLR = newWs.Cells(oldWs.Rows.Count, NEW_COL1).End(xlUp).Row
With oldWs.Range(oldWs.Cells(2, OLD_COL2), oldWs.Cells(oldLR, OLD_COL2))
.AutoFilter Field:=1, Criteria1:="<>"
If .SpecialCells(xlCellTypeVisible).Cells.Count > 2 Then
oldWs.Range(oldWs.Cells(3, OLD_COL1), oldWs.Cells(oldLR, OLD_COL2)).Copy
newWs.Cells(newLR + 1, NEW_COL1).PasteSpecial
Application.CutCopyMode = False
newWs.Sort.SortFields.Clear
newWb.Close SaveChanges:=True
Else
newWb.Close SaveChanges:=False
End If
.AutoFilter
End With
End If
Application.ScreenUpdating = True
End Sub

How to save to the next available row instead of replacing it

I had tried to use VBA to help grab values from a Excel form into a another workbook (Which is blank inside for now) here is the VBA code I'm using:
Sub RunMe()
Dim lRow, lCol As Integer
Sheets("Sheet1").Select
lRow = Range("A" & Rows.Count).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
For Each cell In Range(Cells(1, "B"), Cells(1, lCol))
Union(Range("A1:A" & lRow), Range(Cells(1, cell.Column), Cells(lRow, cell.Column))).Copy
Workbooks.Add
Range("A1").PasteSpecial
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\john\Desktop\Testforvba" & cell.Value & ".xls"
ActiveWorkbook.Close
Next cell
Application.CutCopyMode = False
End Sub
But the thing is that when I run this code after inputting a sample value on the form and manually run the macro, it works as it creates a new file and store the sample data I had type on the Form workbook.
But once I tried running the macro again, I realised that it recreates the file AGAIN which means it replace all the previous data. VBA is so dang hard lol. Anyone please give a hand thanks.
P.S The macro I stored in my personal Workbook
Try this one:
Option Explicit
Sub RunMe()
Application.ScreenUpdating = False
Dim lRow As Long, lCol As Long
Dim wb As Workbook, wbNew as Workbook
Dim ws As Worksheet, wsNew as Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
lCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Dim cell As Variant
For Each cell In ws.Range(ws.Cells(1, "B"), ws.Cells(1, lCol))
Union(ws.Range("A1:A" & lRow), ws.Range(ws.Cells(1, cell.Column), ws.Cells(lRow, cell.Column))).Copy
Set wbNew = Workbooks.Add
Set wsNew = wbNew.Sheets("Sheet1")
Dim yourdesktopaddress As String
yourdesktopaddress = CreateObject("WScript.Shell").specialfolders("Desktop") & "\Testforvba"
wsNew.Range("A1").PasteSpecial
If Not Dir(yourdesktopaddress & "\" & cell.Value & ".xls", vbDirectory) = vbNullString Then
'MsgBox "exists"
Application.DisplayAlerts = False
Else
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
yourdesktopaddress & "\" & cell.Value & ".xls"
End If
wbNew.Close
Application.DisplayAlerts = True
Next cell
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
The main issue was, whenever macro runs, Excel was creating the files from zero and this was causing Excel to recreate each file again. I have added that If Not Dir(yourdesktopaddress & "\Testforvba" & cell.Value & ".xls", vbDirectory) = vbNullString part of my code to check whether did this file already created or not, if not it's creating the file now. If you delete the "'" from the beginning of 'MsgBox "exists" it will always tell you that this file is already exist.
Also In the case of variables are not defined well, excel might start to overwrite itself and it may replace the previous data. (Especially Range("A1).PasteSpecial and Activeworkbook.close kind of critical points are most important ones) So I also edited whole code,defined all variables, defined the user's desktop address to ensure code works on every user.

Copy and Paste Data to VBA created sheets

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

Excel VBA copying rows using autofilter

Looking to copy rows from all sheets apart from my active sheet that meet a certain criteria in column J using VBA.
Not experienced in writing code in VBA so I have tried to frankenstein together the necessary parts from looking through other questions and answers;
below is the code I have written so far;
Sub CommandButton1_Click()
Dim lngLastRow As Long
Dim ws As Worksheet
Dim r As Long, c As Long
Dim wsRow As Long
Set Controlled = Sheets("Controlled") ' Set This to the Sheet name you want all Ok's going to
Worksheets("Controlled").Activate
r = ActiveSheet.Cells(Rows.Count, 2).End(x1up).Row
c = ActiveSheet.Cells(1, Columns.Count).End(x1ToLeft).Column
Range("J").AutoFilter
For Each ws In Worksheets
If ws.Name <> "Controlled" Then
ws.Activate
wsRow = ActiveSheet.Cells(Rows.Count, 2).End(x1up).Row + 1
Range("A" & r).AutoFilter Field:=10, Criteria1:="Y"
.Copy Controlled.Range("A3" & wsRow)
End If
Next ws
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Where Controlled is the sheet I want the data to appear in from the other sheets, and all other sheets are searched to see if their column J meets the criteria="Y"
I won't need to copy over formatting as all Sheets will have the formatting exactly the same and if possible I want the rows that are copied over to start at row 3
Try this:
Option Explicit
Sub ConsolidateY()
Dim ws As Worksheet, wsCtrl As Worksheet
Dim lrow As Long, rng As Range
Set wsCtrl = Thisworkbook.Sheets("Controlled")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
For Each ws In Thisworkbook.Worksheets
If ws.Name = "Controlled" Then GoTo nextsheet
With ws
lrow = .Range("J" & .Rows.Count).End(xlUp).Row
.AutoFilterMode = False
Set rng = .Range("J1:J" & lrow).Find(what:="Y", after:=.Range("J" & lrow))
If rng Is Nothing Then GoTo nextsheet
.Range("J1:J" & lrow).AutoFilter Field:=1, Criteria1:="Y"
.Range("J1:J" & lrow).Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Copy
wsCtrl.Range("A" & wsCtrl.Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues
.AutoFilterMode = False
Application.CutCopyMode = False
End With
nextsheet:
Next
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
I think this covers everything or most of your requirement.
Not tested though so I leave it to you.
If you come across with problems, let me know.

Need help merging two macros for Excel VBA

Both of these macros are macros I found online and adapted to my use. I am using this code and it works well to separate specific data into new sheets:
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:Z" & Range("A" & Rows.Count).End(xlUp).Row)
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 5 'I changed this to 3 for column C
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
What I need help with is to add a particular set of formulas to the bottom of each sheet that is created from the above macro. The following macro adds the formulas to all the sheets in the workbook. I need it to add the formulas to only the sheets that are created in the above macro. The number of sheets created change every time they are generated, depending on the source data. I was thinking it might be best to merge bottom macro into the top but I have no idea how to go about doing that.
Sub Insert_Formulas()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
'Start
NxtRw = Cells(Rows.Count, "B").End(xlUp).Row + 1
With Cells(NxtRw, "B")
.Value = "Total Open Cases"
End With
With Cells(NxtRw, "C")
.EntireRow.Insert
.Value = "Total Closed Cases"
End With
'Next Row below
NxtRw = Cells(Rows.Count, "B").End(xlUp).Row + 1
With Cells(NxtRw, "B")
.Formula = "=CountIf(B2:B" & NxtRw - 1 & ", ""Open*"")"
End With
With Cells(NxtRw, "C")
.Formula = "=CountIf(B2:B" & NxtRw - 1 & ", ""Closed*"")"
End With
Next
End Sub
Any help would be greatly appreciated.
Thank you, Ck
I wouldn't combine the 2 macros, simply call the Insert_Formulas macro from the Copy_To_Worksheets macro when it is needed.
To call the macro all you need is this line:
Insert_Formulas
Edit to respond to comment:
Given you don't know how many sheets are being added I have one suggestion you may try.
High level, add text to a cell in each sheet to indicate if it is new or not. When new sheet is created cell should say "new". When not new it should say "existing". Then in the
If you want to give this a try, and let me know what doesn't work I can check back and help update the code.
In the Copy_To_Worksheets macro you'll need to add a line to set all existing sheets to "existing"
In then Copy_To_Worksheets macro add a line so that new sheets get set to "new"
In Insert_Formulas macro, still loop through all sheets, but check to see if the sheet is "new", and if so, run the code to add the formulas.
A cleaner (but slightly more difficult) option would be to define a name on each sheet (use the same name for each and limit the scope to each individual sheet) and use that instead of a cell on each sheet.
If you parametrize the function so that it takes the worksheet which needs the formulas as a parameter
Sub Insert_Formulas_Into_WorkSheet(ws As Worksheet)
ws.Activate
'Start
NxtRw = Cells(Rows.Count, "B").End(xlUp).Row + 1
With Cells(NxtRw, "B")
.Value = "Total Open Cases"
End With
With Cells(NxtRw, "C")
.EntireRow.Insert
.Value = "Total Closed Cases"
End With
'Next Row below
NxtRw = Cells(Rows.Count, "B").End(xlUp).Row + 1
With Cells(NxtRw, "B")
.Formula = "=CountIf(B2:B" & NxtRw - 1 & ", ""Open*"")"
End With
With Cells(NxtRw, "C")
.Formula = "=CountIf(B2:B" & NxtRw - 1 & ", ""Closed*"")"
End With
End Sub
Then you can add the formulas after each new worksheet is created by calling
Insert_Formulas_Into_WorkSheet WSNew