I want to execute, if the workbook exists already then re- run it if not exists then create a workbook.
I have uniques values(x) and array(names). I need to compare them if both are equal if not it has to create a workbook with name of array(names) that not had in uniques values(x)
My code:
Sub mac()
Dim c as integer
Dim x as range
Dim s_AgingSCM as string
Dim Array_SCM_Aging as variant
Dim NewBook as workbook
Dim NewBook_SCM as workbook
Dim Master_workbook as workbook
Dim rngCopy_Aging as range
Dim rngFilter_Ws2 as range
For c = LBound(Array_SCM_Aging) To UBound(Array_SCM_Aging)
Set Master_workbook = ThisWorkbook
s_AgingSCM = Array_SCM_Aging(c, 1)
Set x = Master_workbook.Sheets("BASS").Range("AY" & c)
If x = s_AgingSCM Then
With rngFilter_Ws2
.AutoFilter field:=32, Criteria1:="<>(a) 0 - 360", Operator:=xlFilterValues
.AutoFilter field:=37, Criteria1:=s_AgingSCM, Operator:=xlFilterValues
Set rngCopy_Aging = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_Aging.Copy NewBook.Worksheets("Aging Inventory").Cells(1, 1)
Application.DisplayAlerts = False
Else
Dim fso: Set fso = createObject("Scripting.FileSystemObject")
Dim folder: Set folder = fso.GetFolder("C:\")
Dim file, fileNames
Dim rngCopy_SCMAging As Range
For Each file In folder.Files
If Right(file.Name, 4) = "xlsx" Then
fileNames = fileNames & file.Name & ";" ' will give a list of all filenames
End If
Next
If InStr(fileNames, s_AgingSCM) = 0 Then
With NewBook_SCM
Set NewBook_SCM = Workbooks.Add
.Title = s_AgingSCM
NewBook_SCM.Worksheets("sheet1").Name = "Aging Inventory"
With rngFilter_Ws2
.AutoFilter field:=32, Criteria1:="<>(a) 0 - 360", Operator:=xlFilterValues
.AutoFilter field:=37, Criteria1:=s_AgingSCM, Operator:=xlFilterValues
Set rngCopy_SCMAging = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_SCMAging.Copy Destination:=NewBook_SCM.Worksheets("Aging Inventory").Cells(1, 1)
.SaveAs Filename:="KPI" & " " & s_AgingSCM & " " & Format_date & ".xlsx"
Application.DisplayAlerts = False
NewBook_SCM.Close
End With
' Else
End If
End sub
I was stuck here since 2 days. All i want is if the workbook exists then overwrite with the new workbook or else if its not exists create a new workbook.
Can someone please help me out.