airforceone
Board Regular
- Joined
- Feb 14, 2022
- Messages
- 214
- Office Version
- 2019
- 2016
- Platform
- Windows
Good day mate, need some guidance again
I would like to copy sheet1 (First Worksheet) in all workbook inside a folder.
I have a running code that only copies One (1) Worksheet in open workbook
Workbook in the Folder
SalesB000120240123.xlsx
ReturnsB000120240123.xlsx
InventoryB000120240123.xlsx
PreOrderB000120240123.xlsx
ArchiveB000120240123.xlsx
I would like to copy sheet1 (First Worksheet) in all workbook inside a folder.
I have a running code that only copies One (1) Worksheet in open workbook
Workbook in the Folder
SalesB000120240123.xlsx
ReturnsB000120240123.xlsx
InventoryB000120240123.xlsx
PreOrderB000120240123.xlsx
ArchiveB000120240123.xlsx
VBA Code:
Sub ImportMySheet()
Dim sFileNamePath As String, sFileName As String
Dim wbTarget As Workbook, wbSource As Workbook
Dim wsSource As Worksheet
Dim sDirFilePath As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wbTarget = ActiveWorkbook
sFileNamePath = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", Title:="Open Source Workbook")
If sFileNamePath = "False" Then
MsgBox "Select Source Workbook Now!"
Exit Sub
Else
sDirFilePath = Split(sFileNamePath, "\")
sFileName = sDirFilePath(UBound(sDirFilePath))
Application.Workbooks.Open Filename:=sFileNamePath
Set wbSource = Workbooks(sFileName)
With wbSource
If LCase(wbSource.Name) Like "Sales*" Then
Set wsSource = wbSource.Sheets(1)
wsSource.Copy After:=wbTarget.Sheets(wbTarget.Sheets.count)
wbTarget.ActiveSheet.Name = "Sales"
wbSource.Close SaveChanges:=False
Else
wbSource.Close SaveChanges:=False
MsgBox "Workbook Source does NOT EXIST!", vbOKOnly + vbCritical
End If
End With
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
' End of ImportMySheet
End Sub