santa12345
Board Regular
- Joined
- Dec 2, 2020
- Messages
- 70
- Office Version
- 365
- Platform
- Windows
Hello.
I am trying to do the following.
Within c:\123 folder. - i have 100 files.
I need vba code to open up file 1, perform a few steps, remove a tab, rename a tab, autofit the columns, save and close, and then loop to the next file until file 100 is completed.
I have the following code which seems to be working on opening up the file, then it bombs when I'm trying to autofit.
Any input would be greatly appreciated.
Thank you!
-------
Sub LoopThroughDirectory()
Dim Fname As String
Dim Pth As String
Dim Wbk As Workbook
Dim Rg As Range (not sure if this is needed)
Pth = "C:\123\"
Fname = Dir(Pth)
Do While Len(Fname) > 0
Set Wbk = Workbooks.Open(Pth & Fname)
Sheets("output").Select
(need to delete output tab)
Sheets("output1").Select
(need to rename output1 tab to output)
(here is where i'm totally lost)
ActiveSheet.Range ("A1:M1000000")
Rg.EntireColumn.AutoFit
With Selection.Font
.Name = "MS Sans Serif"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Cells.EntireColumn.AutoFit
Range("M3").Select
Columns("M:M").ColumnWidth = 21.78
Rows("2:2").RowHeight = 46.8
Wbk.Close True
Fname = Dir
Loop
End Sub
I am trying to do the following.
Within c:\123 folder. - i have 100 files.
I need vba code to open up file 1, perform a few steps, remove a tab, rename a tab, autofit the columns, save and close, and then loop to the next file until file 100 is completed.
I have the following code which seems to be working on opening up the file, then it bombs when I'm trying to autofit.
Any input would be greatly appreciated.
Thank you!
-------
Sub LoopThroughDirectory()
Dim Fname As String
Dim Pth As String
Dim Wbk As Workbook
Dim Rg As Range (not sure if this is needed)
Pth = "C:\123\"
Fname = Dir(Pth)
Do While Len(Fname) > 0
Set Wbk = Workbooks.Open(Pth & Fname)
Sheets("output").Select
(need to delete output tab)
Sheets("output1").Select
(need to rename output1 tab to output)
(here is where i'm totally lost)
ActiveSheet.Range ("A1:M1000000")
Rg.EntireColumn.AutoFit
With Selection.Font
.Name = "MS Sans Serif"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Cells.EntireColumn.AutoFit
Range("M3").Select
Columns("M:M").ColumnWidth = 21.78
Rows("2:2").RowHeight = 46.8
Wbk.Close True
Fname = Dir
Loop
End Sub