vba logic to open up several files within a folder and perform 2-3 steps

santa12345

Board Regular
Joined
Dec 2, 2020
Messages
66
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
scan thru all files in given folder in "C2"

Code:
  'put starting folder in cell then run
Public Sub ProcessAllFies()
   ScanFilesIn1Folder Range("C2").Value
End Sub
  


Private Sub ScanFilesIn1Folder(ByVal pvStartDir)
Dim FileSystem As Object
Dim Folder As Object
Dim oFile As Object
Dim vName
Dim wbSrc As Workbook, wbTarg As Workbook
Dim wsSrc
Dim vShtBase

  
Set wbTarg = ActiveWorkbook
Range("A1").Select

Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set Folder = FileSystem.GetFolder(pvStartDir)

For Each oFile In Folder.Files
    
    If InStr(oFile.Name, ".xls") > 0 Then       'If InStr(oFile.Name, ".accdb") > 0 Or InStr(oFile.Name, ".mdb") > 0 Then
        vName = oFile.Name
           
           'do stuff with each file
       ' Workbooks.Open oFile
       
         '  Set wbSrc = ActiveWorkbook
         '  Set wsSrc = wbSrc.Sheets(1)
         '  wsSrc.Activate
           
             'copy sheet src
        'Sheets(1).Select
        'Sheets(1).Copy Before:=wbTarg.Sheets(1)
           'target name
        'Sheets(1).Select
        'Sheets(1).Name = "New Name"
    
    
      'put the name of the file here
        ActiveCell.Value = oFile
        ActiveCell.Offset(1, 0).Select 'next row
        
        'wbSrc.Close False
    
    End If
    
skip1:
Next
'clear mem.
Set wbSrc = Nothing
Set wbTarg = Nothing
Set oFile = Nothing
Set Folder = Nothing
Set FileSystem = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top