Automating open Excel file/Run Script/Then Save Process with a VBA Script

Growler

Member
Joined
Sep 23, 2010
Messages
13
Programming Experience
Beginner
Hello,

I'm trying to build a database in Access by importing and appending hundreds of Excel documents in a certain folder together. Each imported excel spreadsheet needs to be basically uniform if it is to be appended correctly to the last excel spreadsheet in Access. In addition, blank spaces in the cells cause problems in access...
Since there are hundreds of excel files to be added to Access, I wished to use VBA to automate the process... so here's what I'd like to accomplish:


1st) The macro first scans through the folder with all Excel spreadsheets I wish to import... and automatically opens a single excel file at a time.
2nd) Checks that excel file to see that all blank spaces are filled with " - "
3rd) When it is, save that updated excel copy to a folder I name "New Project"
4th) repeat process on the next spreadsheet

Here's the code I've written so far.. but haven't been able to have it Automatically open each file I need from a particular folder, run the rest of the script, then save it...

VB.NET:
Sub Formatting()

Dim counter As Integer
Dim TotalFiles As Integer
TotalFiles = 1
[COLOR="SeaGreen"]
'Loop through each xl file in a folder[/COLOR]
For counter = 1 To TotalFiles


[COLOR="SeaGreen"]'Open multiple Files----------------------------------------------------------------------------------------------[/COLOR]
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim xlFile As Variant

Filter = "Excel Files (*.xls), *.xls," & "Text Files (*.txt), *.txt," & "All files (*.*), *.*"
[COLOR="SeaGreen"]
'Default filter = *.*[/COLOR]
FilterIndex = 3
'Set dialog caption

Title = "Select File(s) to Open"
[COLOR="SeaGreen"]'Select Start and Drive path[/COLOR]
ChDrive ("C")
ChDir ("C:\Users\DTurcotte\Desktop\Test_Origin")

With Application
[COLOR="SeaGreen"]    'Set file name array to selected files (allow multiple)[/COLOR]
    xlFile = .GetOpenFilename(Filter, FilterIndex, Title, , True)
 [COLOR="SeaGreen"]   'Reset Start Drive/Path[/COLOR]
    ChDrive (Left(.DefaultFilePath, 1))
    ChDir (.DefaultFilePath)
End With

[COLOR="SeaGreen"]'Exit on Cancel[/COLOR]
If Not IsArray(xlFile) Then
    MsgBox "No file was selected."
    Exit Sub
End If
[COLOR="SeaGreen"]'Open Files[/COLOR]
For i = LBound(xlFile) To UBound(xlFile)
    msg = msg & xlFile(i) & vbCrLf
    Workbooks.Open xlFile(i)
Next i
MsgBox msg, vbInformation, "Files Opened"



[COLOR="SeaGreen"]'Format Column Headings----------------------------------------------------------------------------------------------[/COLOR]
ActiveWorkbook.Sheets.Select

Dim RowIndex As Integer
Dim ColIndex As Integer
Dim totalRows As Integer
Dim totalCols As Integer

Dim LastRow As Long
Dim range As range


totalRows = Application.WorksheetFunction.CountA(Columns(1))

If Cells(1, 1).Value <> "ROOM #" Then Cells(1, 1).Value = "ROOM #"
If Cells(1, 2).Value <> "ROOM NAME" Then Cells(1, 2).Value = "ROOM NAME"
If Cells(1, 3).Value <> "HOMOGENEOUS AREA" Then Cells(1, 3).Value = "HOMOGENEOUS AREA"
If Cells(1, 4).Value <> "SUSPECT MATERIAL DESCRIPTION" Then Cells(1, 4).Value = "SUSPECT MATERIAL DESCRIPTION"

If Cells(1, 5).Value <> "ASBESTOS CONTENT (%)" Then Cells(1, 5).Value = "ASBESTOS CONTENT (%)"
If Cells(1, 6).Value <> "CONDITION" Then Cells(1, 6).Value = "CONDITION"
If Cells(1, 7).Value <> "FLOORING (SF)" Then Cells(1, 7).Value = "FLOORING (SF)"
If Cells(1, 8).Value <> "CEILING (SF)" Then Cells(1, 8).Value = "CEILING (SF)"

If Cells(1, 9).Value <> "WALLS (SF)" Then Cells(1, 9).Value = "WALLS (SF)"
If Cells(1, 10).Value <> "PIPE INSULATION (LF)" Then Cells(1, 10).Value = "PIPE INSULATION (LF)"
If Cells(1, 11).Value <> "PIPE FITTING INSULATION (EA)" Then Cells(1, 11).Value = "PIPE FITTING INSULATION (EA)"
If Cells(1, 12).Value <> "DUCT INSULATION (SF)" Then Cells(1, 12).Value = "DUCT INSULATION (SF)"

If Cells(1, 13).Value <> "EQUIPMENT INSULATION (SF)" Then Cells(1, 13).Value = "EQUIPMENT INSULATION (SF)"
If Cells(1, 14).Value <> "MISC. (SF)" Then Cells(1, 14).Value = "MISC. (SF)"
If Cells(1, 15).Value <> "MISC. (LF)" Then Cells(1, 15).Value = "MISC. (LF)"

[COLOR="SeaGreen"]'Fills in blank spaces with "-"[/COLOR]
For RowIndex = 1 To totalRows
    For ColIndex = 1 To 15
        If Cells(RowIndex, ColIndex).Value = "" Then Cells(RowIndex, ColIndex).Value = "test"
        Next ColIndex
        Next RowIndex
        
[COLOR="SeaGreen"]'Clears content from "Totals" Row[/COLOR]
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
    End With
    Rows(LastRow).ClearContents
[COLOR="SeaGreen"]    
'Saves file to a new folder[/COLOR]
'Need to have the code run through that excel doc, set that updated copy to a variable, and then have the following code save it to a new folder

[COLOR="SeaGreen"]'newSaveName = updated excel file
'ActiveWorkbook.SaveAs ("C:\Users\DTurcotte\Desktop\TestExcelFiles" & Test1_Success & ".xls")[/COLOR]

Next counter
    

End Sub

----------------------------
Can anyone provide any help?
Thank you!!!
 
This is a VB.Net forum, VBA questions you have to take to a VBA forum. Btw, VB.Net also has tools to work with Office files, like interop to automation libraries and ADO.Net.
 
Back
Top