CREATE FOLDER USING VBA
Sub Create_folder()
Dim fos As Scripting.FileSystemObject
Dim F As String
Set fso = New Scripting.FileSystemObject
F = ThisWorkbook.Path & "\" & Application.WorksheetFunction.Text(Now(), "DD MMM YY")
Fi = F & "\Day 1.xlsb"
If Not FSO.FolderExists(F) Then
FSO.CreateFolder (F)
Else
FSO.DeleteFolder (F)
FSO.CreateFolder (F)
end if
end sub
-
Go to References and add Microsoft Scripting Runtime
-
Paste Coding to you visual
CREATE FILE USING VBA
Sub Create_File()
Dim fos As Scripting.FileSystemObject
Dim F As String
Dim Fi As String
Set fso = New Scripting.FileSystemObject
F = ThisWorkbook.Path & "\"
Fi = F & "\Day 1.xlsb"
If Not FSO.FileExists(F) Then
FSO.CreateFile (F)
Else
FSO.DeleteFile (F)
FSO.CreateFile (F)
end if
end sub
Copy file one folder to other folder
Miscellaneous Code of File and Folder
Sub Copy_File()
Dim fos As Scripting.FileSystemObject
Dim DFolder As String
Dim Fi As String
Dim SFolder As String
Set fso = New Scripting.FileSystemObject
SFolder = ThisWorkbook.Path
DFolder = "C:\Users\hp\Desktop\App\"
Fi = "Loops.xlsb"
'Checking you file do exist as Source folder or not
If Not FSO.FileExists(SFolder & "\" & Fi) Then
A = MsgBox(Fi & " Not exists in folder " & SFolder, vbCritical, "Alert")
ElseIf FSO.FileExists(DFolder & "\" & Fi) Then
A = MsgBox(Fi & " Already in folder " & DFolder, vbInformation, "Alert")
Else
FSO.CopyFile (SFolder & "\" & Fi), DFolder, True
A = MsgBox(Fi & " Copy from " & SFolder & " to " & DFolder)
End If
End Sub
Excel File Formats
-
xlAddIn ----- .xla
-
xlCSV ------ .CSV
-
xlExcel12 ----- .xlsb
-
xlHtml ---- .htm
-
xlOpenDocumentSpreadsheet --- .ods
-
xlOpenXMLAddIn ---- .xlam
-
xlOpenXMLWorkbook --- xlsx
-
xlOpenXMLWorkbookMacroEnabled -- .xlsm
-
xlTextMac --- .txt
-
we are assuming that you have dim FSO as object and set FSO = CreateObject("Scripting.filesystemobject")
-
Create Folder = FSO.CreateFolder (Folder Path)
-
Create File = FSO.CreateFile (File Path and Name with Extension)
-
Delete Folder = FSO.DeleteFolder (Folder Path)
-
Delete File = FSO.DeleteFolder (File path and Name with Extension)
-
Check Existence of Folder = FSO.FolderExists( Folder Path)
-
Check Existence of File = FSO.FileExists ( File Path and Name with Extension)
-
Copy File from one folder to other folder = FSO.CopyFIle ( Source folder path and file name with extension ), destination folder, True
-
Open Excel File :- Workbooks.Open(File Path with Name and Extension
-
Close Special workbook = We assumed you have dim Wb as workbook Wb.close , SaveChange:= true , to save and False to not save
-
Close all Excel files = Workbooks.close
-
Add New workbook = workbooks.add
-
Save Workbook = Eg wb.SaveAs Filename:= Workbook Path with name ", FileFormat:= .xlCSV, Password:= "Afzal" ( Check file formats in left side) Example ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\File.xlsx", FileFormat:=xlCSV, Password:="Afzal"
-
Open Folder = Shell "Explorer.exe & Folder Path .vbNormalFocus Example ( Shell "Explorer.exe C:\Users\afzal_102599\Desktop\MY Drive\ICICI Bank\", vbNormalFocus)/
-
ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Columns( "A:A")
Create HTML File
Sub Create_HTML()
Dim wb As Workbook
Dim ws As Worksheet
Dim Fso As Scripting.FileSystemObject
Dim Hfile As String
Set wb = ThisWorkbook
Set ws = wb.Sheets("MIS")
Set Fso = New Scripting.FileSystemObject
Hfile = "C:\Users\hp\Downloads\HTML FIle\Pivot.HTML"
Fso.CreateTextFile (Hfile)
wb.PublishObjects.Add(xlSourceRange, Hfile, "Sheet1", ws.UsedRange.Address, xlHtmlStatic).Publish (True)
Set myfile = Fso.OpenTextFile(Hfile)
newwb.Close
End Sub
Get File Name from a Folder
Sub GetFileName()
Dim F As String
Dim df As String
F = "C:\Users\hp\***\***\"
df = Dir(F)
Sheets("Name").Activate
Range("A1").Value = "Name"
Range("A2").Select
Do While df <> ""
Selection.Value = df
Selection.Offset(1, 0).Select
df = Dir
Loop
End Sub
SharePoint File and Folders
Sub Compile_SharePoint_File()
Dim wb As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim sh As Worksheet
Dim rng As Range
Dim f As String
Set wb = ThisWorkbook
Set ws = wb.Sheets("Data")
Set sh = wb.Sheets("Home")
Set rng = sh.Range("A2", sh.Range("A2").End(xlDown))
f = "https:/YourSharePointSite.sharepoint.com/Shared%20Documents/Excel%20File%20For%20Practice/"
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.AskToUpdateLinks = False
.EnableEvents = False
End With
For Each rng In rng
Workbooks.Open (f & Application.WorksheetFunction.Substitute(rng, " ", "%20") & ".xlsx")
Set wb2 = ActiveWorkbook
Range("A2:O2").Select
Range(Selection, Selection.End(xlDown)).Copy
wb.Activate
ws.Activate
If Range("A2") = "" Then
Range("A2").PasteSpecial xlPasteValues
Else
Range("A2").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
End If
wb2.Close
Next rng
End Sub
SharePoint Site do not Accept space that is why we have to replace all space with %20.
Eg. you want to open a file from your SharePoint which name is Laptop, and location is Documents/Excel file for Practice
below is code
workbooks.open("https:/YourSharePointSite.sharepoint.com/Shared%20Documents/Excel%20File%20For%20Practice/Laptop.xlsx"
and file file name is Laptop India then code will be
workbooks.open("https:/YourSharePointSite.sharepoint.com/Shared%20Documents/Excel%20File%20For%20Practice/Laptop%20India.xlsx"