Attribute VB_Name = "Module1"
'Option Explicit
Option Compare Text
Sub Create_Folders()
Attribute Create_Folders.VB_ProcData.VB_Invoke_Func = " \n14"
'
' Macro to create CG folders and populate CG workbooks
'
Dim CGStart, CGNum, CGReason, strInfile, strOutfile, strPath, strOldFile, strBaseName, strRegion, strRemote As String
Dim CGValue As String, CurrName As String
Dim rng As Range
Dim CGLogged As Date
Dim iCreate As Integer
' Set defaults for macro
strOldFile = "INCIDENT LOG SHEET.xlsx"
strPath = "C:\test\"
strRemote = "\\RemotePath\"
strBaseName = strOldFile
CGSheet = "Sheet1"
CurrName = ThisWorkbook.Name
'Get required start ref
CGStart = Application.InputBox(Prompt:="Enter CG Start Ref", Type:=2)
' CGSheet = Application.InputBox(Prompt:="Sheet Name?", Type:=2)
iCreate = MsgBox("Create Excel Sheets?", vbYesNo)
strRegion = Application.InputBox(Prompt:="(L)ocal or (R)emote path to create", Type:=2)
' Choose which path to use
If Left(strRegion, 1) = "R" Then
strPath = strRemote
End If
Range("A1").Select
Cells.Find(what:=CGStart, after:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
If ActiveCell.Value <> CGStart Then
MsgBox "Value not found", vbExclamation
Exit Sub
End If
' Switch off screen movement
Application.ScreenUpdating = False
' Now open the template file if we are creating sheets
If iCreate = vbYes Then
Workbooks.Open Filename:=strPath & strOldFile
'Now go back to Master Extract workbook
Workbooks(CurrName).Sheets(CGSheet).Activate
End If
Do Until ActiveCell.Value = "" Or ActiveCell.Value = 0
CGNum = ActiveCell.Value
ActiveCell.Offset(0, 1).Activate
CGValue = ActiveCell.Value
ActiveCell.Offset(0, 2).Activate
CGReason = ActiveCell.Value
ActiveCell.Offset(0, 1).Activate
CGLogged = ActiveCell.Value
' MsgBox (CGNum & " " & CGValue & " " & CGReason & " " & CGLogged)
' Now create the folder
If Not FileFolderExists(strPath & CGNum) Then
MkDir (strPath & CGNum)
End If
' Now go to opened template file and relevant sheet if we are creating the files
If iCreate = vbYes Then
' Sometime selecting the sheet works, other times it does not, so remove for now and have only one sheet
Workbooks(strOldFile).Activate
' Now set the values
Range("B1").Value = CGNum
Range("B2").Value = CGReason
Range("E3").Value = CGValue
Range("H1").Value = CGLogged
Range("A6").Value = CGLogged
' Now create the name to save as
strOutfile = strPath & CGNum & "\" & CGNum & " " & strBaseName
' And save the file
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=strOutfile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = True
strOldFile = CGNum & " " & strBaseName
End If
'Now go back to Master Extract workbook
Workbooks(CurrName).Sheets(CGSheet).Activate
' Get back to column A and down a row
ActiveCell.Offset(1, -4).Activate
Loop
If iCreate = vbYes Then
Workbooks(strOldFile).Close
End If
'Switch Screen updating back on
Application.ScreenUpdating = True
If Left(strRegion, 1) <> "R" Then
MsgBox "Macro completed, please now move the folders to required location", vbExclamation
Else
MsgBox "Macro completed, Folders created at remote location", vbExclamation
End If
' Clear variables
End
End Sub
Public Function FileFolderExists(strFullPath As String) As Boolean
' Check if file or folder exists
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then
FileFolderExists = True
End If
EarlyExit:
On Error GoTo 0
End Function