Loop & Search for Matching Worksheets in Separate Wkbks; Add Wksht if No Match Found - vba

I have a series of workbooks by District, Territory, and Period that have monthly sales data for each combination of District, Territory, and Period. There is a master workbook for each District containing separate worksheets for each Territory. The monthly data appears in columns B:M.
I need to open each of the monthly District, Territory, and Period files, open the corresponding District's master workbook, search for a corresponding Territory, and paste that month's data in the column associated with that month (e.g., Feb. data is pasted in column C). This should be followed by closing the monthly file and loop to the next monthly file.
However, I need to have code for the possibility that a NEW Territory is added to a District in midyear--sometime after that District's master workbook was initially created.
The loop as written wants to jump from the open monthly file to the next part of the loop code that would create a new worksheet, but that is not what is needed.
Any suggestions for fixing this? Here is what I have so far:
Sub DSMReportsP02()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim DistrictDSM As Range, DistrictsDSMList As Range
Dim Period As String, Path As String, DistPeriodFile As String, Territory As String
Dim YYYY As Variant
Dim WBMaster As Workbook, DistMaster As Workbook, CurDstTerrFile As Workbook
Dim wsCount As Integer, x As Integer
Dim wsExists As Boolean
Set DistrictsDSMList = Range("E11:E" & Cells(Rows.Count, "E").End(xlUp).Row)
Set WBMaster = ActiveWorkbook
Period = Range("C6").Value
YYYY = Range("C8").Value
wsExists = False
For Each DistrictDSM In DistrictsDSMList.Cells
Workbooks.Open Filename:="H:\Accounting\Monthend " & YYYY & "\DSM Files\DSM Master Reports\" & DistrictDSM & ".xlsx"
Set DistMaster = ActiveWorkbook
wsCount = Application.Sheets.Count
Path = "H:\Accounting\Monthend " & YYYY & "\DSM Files\" & DistrictDSM & "\P02"
DistPeriodFile = Dir(Path & "\*.xlsx")
Do While DistPeriodFile <> ""
Workbooks.Open Filename:=Path & "\" & DistPeriodFile, UpdateLinks:=False
DistPeriodFile = Dir
Set CurDstTerrFile = ActiveWorkbook
Territory = CurDstTerrFile.Sheets("Index").Range("A3").Value
For x = 1 To wsCount
If DistMaster.Worksheets(x).name = Territory Then
CurDstTerrFile.Sheets("Index").Range("F20").Copy 'PM
DistMaster.Sheets(Territory).Activate
Range("C3").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("J20").Copy 'XRA
DistMaster.Sheets(Territory).Activate
Range("C5").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("N20").Copy 'CO-OP
DistMaster.Sheets(Territory).Activate
Range("C7").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("S20").Copy 'VR
DistMaster.Sheets(Territory).Activate
Range("C9").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("W20").Copy 'OVER & ABOVE
DistMaster.Sheets(Territory).Activate
Range("C11").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("AA20").Copy 'SS
DistMaster.Sheets(Territory).Activate
Range("C13").PasteSpecial Paste:=xlPasteValues
CurDstTerrFile.Sheets("Index").Range("A3:D19").Copy 'COPY BTs by DISTRICT
WBMaster.Sheets("BTs by District").Activate
Range("A1000000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Exit For
End If
Next x
If wsExists = False Then '***********FIX THIS SECTION!!!*************************
Worksheets.Add after:=DistMaster.Worksheets(Worksheets.Count)
CurDstTerrFile.Sheets("Index").Range("A3").Copy 'COPY TERRITORY
ActiveSheet.name = "New Territory"
DistMaster.Sheets(Territory).Activate
Range("A1").PasteSpecial Paste:=xlPasteValues
End If
Dim WS As Worksheet, SheetXXX As Worksheet
Set WS = WBMaster.Sheets("ReptTemplate")
WS.Copy after:=Sheets(WBMaster.Sheets.Count)
Set SheetXXX = ActiveWorkbook.ActiveSheet
SheetXXX.name = Worksheets("ReptTemplate").Range("A1").Value
CurDstTerrFile.Close
Loop
Dim DistWS As Worksheet
Dim DistName As String
Dim wbNew As Workbook
DistName = Left(DistrictDSM, 6) & "*"
Set wbNew = Application.Workbooks.Add
For Each DistWS In WBMaster.Sheets
If DistWS.name Like DistName Then DistWS.Move after:=Sheets(wbNew.Sheets.Count)
Next DistWS
With wbNew
.SaveAs "H:\Accounting\Monthend " & YYYY & "\DSM Files\DSM Master Reports\" & DistrictDSM & ".xlsx"
.Close
End With
Next DistrictDSM
Application.EnableEvents = True
End Sub

I am sorry but I cannot comment (not enough reputation to comment on questions) or otherwise I would have asked a few question before posting this.
From what I understood. What you need is a logic/algorithm that checks whether a new sheet (new territory) is required be added to the master file before starting the copying process to the master file. If a sheet needs to be added, it should be added at the end of the master file.
The code below is a general code, but you should be able to tweak it easily to fit your purpose. The code below compares the worksheets in wb2 with wb1. if a sheet name in wb2 does not exist in wb1 it will be added to a new sheet at the end of wb1 with a name similar to the one in wb2.
This code should be placed immediately after both files are opened.
Sub Comapre_Sheets()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wks1 As Worksheet
Dim wks2 As Worksheet
Dim bWorkSheet_Found As Boolean
Set wb1 = Workbooks("Book1") ''' Change this to the master file
Set wb2 = Workbooks("Book2") ''' Change this to the file that might have the new sheet/territory
For Each wks2 In wb2.Worksheets
bWorkSheet_Found = False
For Each wks1 In wb1.Worksheets
If wks1.name = wks2.name Then
bWorkSheet_Found = True
End If
Next wks1
If Not bWorkSheet_Found Then
wb1.Worksheets.Add(After:=Worksheets(wb1.Sheets.Count)).name = wks2.name
End If
Next wks2
End Sub
Hope this helps you

Related

How to run VBA in excel, when Excel filename got changed

I have a Workbook (1) contains 2 sheets. In my program, I would like to generate a workbook (2) which Create 2 sheets. Then the program will filter the table and copy values from workbook 1 to workbook 2.
But my problem is my workbook (1) name will get changed every time. I tried to use ActiveWorkbook.Name. But when the program is running, it will create a new workbook then suddenly it became an active workbook.
I named my main Workbook (1) as Filevalue. But not working. How to solve this problem. I need to run this program eventhough when the name get changed. Help me
Sub createlandDE()
Filepath = ActiveWorkbook.path
FileValue = ActiveWorkbook.Name 'Problem With Activeworkbook
NameValue = Format(Date, "yymmdd") & "-DE"
Dim wb As Workbook
Set wb = Workbooks.add
Dim path As String
Dim FSO As Object
path = Filepath & "\" & NameValue & ".xlsx"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(path) Then
On Error Resume Next
Workbooks(NameValue & ".xlsx").Close False
Kill path
wb.SaveAs path
Sheets(3).Delete
Else
wb.SaveAs path
Sheets(3).Delete
End If
Application.ScreenUpdating = False
Dim ws, ws1, ws2 As Worksheet
Dim table1, table2 As ListObject
Dim rng1 As Range
Sheets(1).Name = "Main view"
Sheets(2).Name = "Overall view"
Set ws1 = Workbooks(NameValue & ".xlsx").Worksheets("Main view")
ws1.ListObjects.add(xlSrcRange, ws1.Range("A$1:$J$1"), , xlYes).Name = "MainTable"
Set table1 = ws1.ListObjects(1)
Set ws = Workbooks(FileValue).Worksheets("Main") 'Problem With Activeworkbook
ws.PivotTables("MainTable").PivotFields("Dealer Country Code").CurrentPage = "DE"
With ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng1 = Range(.Range("A4"), .Range("J" & LastRow))
End With
rng1.Copy
ws1.Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Set ws2 = Sheets("Overall view")
ws2.ListObjects.add(xlSrcRange, ws2.Range("A$1:$Q$1"), , xlYes).Name = "OverallTable"
Set table2 = ws2.ListObjects(1)
Worksheets("Overall view").ListObjects("OverallTable").TableStyle = "Table Style 1"
Workbooks(FileValue).Activate 'Problem With Activeworkbook
Sheet2.ListObjects("DataTable").Range.AutoFilter Field:=1
Sheet2.ListObjects("DataTable").Range.AutoFilter Field:=12
ActiveWorkbook.Save
Application.ScreenUpdating = True
End Sub

Splitting an identified range of spreadsheets to a new workbook with a new name

I've been trying to come up with a way to split a workbook into separate workbooks based on identified worksheets in the workbook.
For example:
Say I had a worksheet for every letter in the alphabet.
I would want to split worksheets A through C into a new workbook named "A through C."
D through I will go into a new workbook named "D through I."
etc...
My idea would be to first insert a worksheet that in column A names the new workbook it will become and Columns b through as many columns as there are will the names of the worksheets to be copied into the new workbook.
Does anyone have an idea of how to make a macro for this? I've tried myself but have been unsuccessful.
Thank you!
I found this Macro out there. Does anyone think it can be modified to work?
Sub Test()
Dim Sh As Worksheet
Dim Rng As Range
Dim c As Range
Dim List As New Collection
Dim Item As Variant
Dim WB As Workbook
Application.ScreenUpdating = False
Set Sh = Worksheets("Sheet1")
Set Rng = Sh.Range("A2:A" & Sh.Range("A65536").End(xlUp).Row)
On Error Resume Next
For Each c In Rng
List.Add c.Value, CStr(c.Value)
Next c
On Error GoTo 0
Set Rng = Sh.Range("A1:H" & Sh.Range("A65536").End(xlUp).Row)
For Each Item In List
Set WB = Workbooks.Add
Rng.AutoFilter Field:=1, Criteria1:=Item
Rng.SpecialCells(xlCellTypeVisible).Copy WB.Worksheets(1).Range("A1")
Rng.AutoFilter
With WB
.SaveAs ThisWorkbook.Path & "\" & Item & ".xls"
.Close
End With
Next Item
Sh.Activate
Application.ScreenUpdating = True
End Sub
The following code assumes you have your control sheet (named "Split Parameters") in the workbook containing the macro, and it is set out with the desired filenames in column A, and the sheets that you wish to copy into that file (from the ActiveWorkbook, which might, or might not, be the one containing the macro) listed in columns B, C, etc. Row 1 is assumed to be headings, and is therefore ignored.
Sub SplitBook()
Dim lastRow As Long
Dim LastColumn As Long
Dim srcWB As Workbook
Dim newWB As Workbook
Dim i As Long
Dim c As Long
Dim XPath As String
Dim newName As String
Dim sheetName As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set srcWB = ActiveWorkbook
XPath = srcWB.Path
With ThisWorkbook.Worksheets("Split Parameters")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
'Take the first worksheet and create a new workbook
sheetName = .Cells(i, "B").Value
srcWB.Sheets(sheetName).Copy
Set newWB = ActiveWorkbook
'Now process all the other sheets that need to go into this workbook
LastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
For c = 3 To LastColumn
sheetName = .Cells(i, c).Value
srcWB.Sheets(sheetname).Copy After:=newWB.Sheets(newWb.Sheets.Count)
Next
'Save the new workbook
newName = .Cells(i, "A").Value
newWB.SaveAs Filename:=xPath & "\" & newName & ".xls", FileFormat:=xlExcel8
newWB.Close False
Next
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Excel Macro: Replace formulas and save sheet as own file without changing original document

I have a workbook with a lot of worksheets. I only work on sheets marked with a !. I want to replace all formulas by values and store the sheets as own .xls files. My script is exactly doing that. My problem is that the original document is affected as well. Is there a way to replace the values only on the copied sheet which will be stored so the original document stays the same?
Dim wbk As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim cl As Object
Dim xPath As String
Dim isReadable As Boolean
Dim sName As String
xPath = Application.ActiveWorkbook.Path
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
On Error Resume Next
Set wbk = ActiveWorkbook
For Each ws In wbk.Sheets
isReadable = (InStr(ws.Name, "!")) > 0
If isReadable Then
Debug.Print ws.Name
Set rng = ws.Range("A1").SpecialCells(xlCellTypeFormulas, 23)
If Not (rng Is Nothing) Then
For Each cl In rng
cl.Value = cl.Value
Next cl
End If
sName = Replace(ws.Name, "!", "")
sName = LCase(Replace(sName, "+", ""))
ws.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & sName & ".xlsx", CreateBackup:=False
Application.ActiveWorkbook.Close False
Debug.Print sName
End If
Next ws
Application.Calculate
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
MsgBox "Done, do not save the changes!"
Make a copy of the sheet and work on that:
Sub DoStuff()
Dim wsOrig As Worksheet
Dim wsNew As Worksheet
Dim wbOrig As Workbook
Dim wbNew As Workbook
Set wsOrig = ActiveSheet ' or whatever
Set wbOrig = ActiveWorkbook ' or whatever
For s = 1 To wbOrig.Sheets.Count
If wbOrig.Sheets(s) ' meets my conditions then....
Application.SetWarnings False ' don't question if we want to delete thinsg
Set wbNew = Workbooks.Add
wsOrig.Copy After:=wbNew.Sheets(1)
wbNew.Sheets(1).Delete ' delete the default Sheet1 of the new workbook
Set wsNew = wbNew.Sheets(1)
With wsNew
' do all the stuff I want to do
End With
wbNew.SaveAs ' whatever
Application.SetWarnings True
End If
Next s
End Sub
Here is another way to make the changes only on the copied sheet:
Dim ws As Worksheet, ws2 As Worksheet
For Each ws In Worksheets
If ws.Name Like "*!*" Then
ws.Copy
Set ws2 = Workbooks(Workbooks.Count).Sheets(1) ' newest workbook
ws2.Name = LCase(Replace(Replace(ws.Name, "!", ""), "+", ""))
ws2.UsedRange.Value = ws2.UsedRange.Value
ws2.Parent.SaveAs ws.Parent.Path & "\" & sName & ".xlsx", CreateBackup:=False ' add checks if the file already exist and if path contains illegal characters that are not allowed
ws2.Parent.Close False
End If
Next

Excel Sheet Name Error

I'm using a VBA code to cycle through excel files in a directory and pull information from one worksheet and paste into a newly created worksheet. I'm also naming my new worksheets (in my destination file) by the name in one of the cells in the source file.
My code works for the first loop but fails/stops in the second loop (VBA points to an error in the Dest.Sheets.Add(After:=Dest.Sheets(Dest.Sheets.Count)).Name = Sheetname line. I need to loop through 75 of these files and I'm unsure of what's going on because it works correctly for the first file.
Thanks so much for the help!
Sub AddSummaryTables()
Dim Spath, Filename, Sheetname As String
Dim Source, Dest As Workbook
Dim WS As Worksheet
Set Dest = ThisWorkbook
Spath = InputBox("Enter File Source Path") & "\"
Filename = Dir(Spath & "*.xls*")
Do While Filename <> ""
Set Source = Workbooks.Open(Spath & Filename)
Sheetname = Source.Sheets("Summary").Range("B2").Text
MsgBox Sheetname
Dest.Sheets.Add(After:=Dest.Sheets(Dest.Sheets.Count)).Name = Sheetname
Source.Sheets("Summary").Range("A1:R150").Copy
Dest.Worksheets(Sheetname).Range("A1").PasteSpecial xlPasteValues
Dest.Worksheets(Sheetname).Range("A1").PasteSpecial xlPasteFormats
Dest.Worksheets(Sheetname).Range("A1:R150").WrapText = False
Dest.Worksheets(Sheetname).Rows.AutoFit
Dest.Worksheets(Sheetname).Columns.AutoFit
Source.Close SaveChanges:=False
Dest.Save
Filename = Dir()
Loop
End Sub
following Comintern's and Wyatt's suggestion you could try like follows
Option Explicit
Sub AddSummaryTables()
Dim sPath As String, fileName As String
Dim sourceWb As Workbook, destWb As Workbook
Dim sourceWs As Worksheet, destWs As Worksheet
Set destWb = ThisWorkbook
sPath = InputBox("Enter File Source Path") & "\"
fileName = Dir(sPath & "*.xls*")
Do While fileName <> ""
Set sourceWb = Workbooks.Open(sPath & fileName)
Set sourceWs = GetWorksheet(sourceWb, "Summary")
If Not sourceWs Is Nothing Then
Set destWs = SetWorksheet(destWb, sourceWs.Range("B2").Text)
sourceWs.Range("A1:R150").Copy
With destWs
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.UsedRange.WrapText = False
.Rows.AutoFit
.Columns.AutoFit
End With
sourceWb.Close SaveChanges:=False
destWb.Save
End If
fileName = Dir()
Loop
End Sub
Function GetWorksheet(wb As Workbook, sheetName As String) As Worksheet
On Error Resume Next
Set GetWorksheet = wb.Worksheets(sheetName)
On Error GoTo 0
End Function
Function SetWorksheet(wb As Workbook, sheetName As String) As Worksheet
Dim i As Integer
Do While Not GetWorksheet(wb, sheetName & IIf(i = 0, "", "-" & i)) Is Nothing
i = i + 1
Loop
With wb
.Worksheets.Add(After:=.Worksheets(.Worksheets.Count)).Name = sheetName & IIf(i = 0, "", "-" & Format(i, "000"))
Set SetWorksheet = .ActiveSheet
End With
End Function
where you make sure that
any opened workbook has a "Summary" worksheet
you name worksheets in your destination workbook such as not to have duplicates: if you happen to deal with say three worksheets named "Sheet5" then your destination workbook will have added worksheets "Sheet5", "Sheet5-001" and "Sheet5-002".
You're issue may be that when you are adding the sheet from the second workbook, it has the same name as the sheet from the first workbook. You could check if the sheet exists and add a number to it. The post below might help.
Test or check if sheet exists

Copy value from active sheet to new excel file

I have a work sheet named Final_Sheet, I want to create a button on that sheet and execute the following operation
Select Cell Range A1:D30 and pickup the values from cell only and create a new Excel file and paste the copied cell values into Sheet1 of created excel file. I am able to o this much, further I can't understand what to do can anybody please help me out?
Private Sub Button1_Click()
Dim rownum As Integer
Dim selection As Range
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
rownum = InputBox("Row No to Copy :", "OK")
selection = ActiveSheet.Range(Cells(1, 1), Cells(rownum, 10)).Select
selection.Copy
Flname = InputBox("Enter File Name :", "Creating New File...")
MsgBox ("Output File Created Successfully")
If Flname <> "" Then
Set NewWkbk = Workbooks.Add
ThisWorkbook.Sheets(1).Range("D1:D30").Copy Before:=NewWkbk.Sheets(1)
NewWkbk.Sheet(1).Select
Cells(1, 1).Activate
selection.PasteSpecial xlPasteValues
NewWkbk.SaveAs ThisWorkbook.Path & "\" & Flname
ActiveWorkbook.Close
End If
End Sub
The code below should do as you've asked.
EDIT: Pastes values only
Private Sub Button1_Click()
'Dim Variables
Dim ws As Worksheet
Dim rng As Range
Dim wbNew As Workbook, wbCurrent As Workbook
Dim strFileName As String
'Assign object variables
Set wbCurrent = ActiveWorkbook
Set ws = wbCurrent.ActiveSheet
Set rng = ws.Range("A1:D30") 'Amend range if needed
'Get desired file path from user
strFileName = InputBox("Enter File Name: ", "Creating New File...")
If strFileName <> "" Then
'Create new Workbook and paste rng into it
Set wbNew = Workbooks.Add
rng.Copy
wbNew.Sheets(1).Range("A1:D30").PasteSpecial xlValues
'Save new workbook using desired filepath
wbNew.SaveAs wbCurrent.Path & "\" & strFileName
End If
End Sub
It wasn't clear to me from the question which workbook you wished to close with ActiveWorkbook.Close but you could easily add wbNew.Close or wbCurrent.Close below wbNew.SaveAs ... as required.