N
news.microsoft.com
I'm really confused. I thought the following code would work but it isn't
doing what I expected. I have several data files that are exactly the same
and I need to reformat all of them... Instead of opening each one and doing
the formatting needed, I thought it would be nice to have a macro open the
all the xls files in a given directory and format them, and then save &
close them. Does anyone know what is wrong with my code below?
Public Sub ImportData()
'Define variables
Dim sPath As String
Dim sFile As String
Dim oExcel As New Excel.Application
Dim oWB As New Workbook
'Loop through the xls files in the directory...
sPath = ActiveWorkbook.Path
sFile = Dir(sPath & "\*.xls")
Do While sFile <> ""
'Open xls data file that needs to be formatted...
If sFile <> "Master Import File.xls" Then
Debug.Print sFile
Set oWB = oExcel.Workbooks.Open(sPath & "\" & sFile)
With oWB
.Activate
Call Formatting
.Close
End With
Set oWB = Nothing
End If
sFile = Dir
Loop
Set oExcel = Nothing
End Sub
Sub Formatting()
ActiveSheet.Rows("1:7").Select
Selection.Delete Shift:=xlUp
ActiveSheet.Columns("A:A").ColumnWidth = 83.14
ActiveSheet.Cells.Select
With Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveSheet.Columns("B").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("D").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("H:I").Select
ActiveSheet.Range("I1").Activate
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("B:H").Select
Selection.ColumnWidth = 15
ActiveSheet.Range("A1").Select
ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
Selection.Interior.ColorIndex = 6
ActiveWorkbook.Save
End Sub
Thanks!
Rob
doing what I expected. I have several data files that are exactly the same
and I need to reformat all of them... Instead of opening each one and doing
the formatting needed, I thought it would be nice to have a macro open the
all the xls files in a given directory and format them, and then save &
close them. Does anyone know what is wrong with my code below?
Public Sub ImportData()
'Define variables
Dim sPath As String
Dim sFile As String
Dim oExcel As New Excel.Application
Dim oWB As New Workbook
'Loop through the xls files in the directory...
sPath = ActiveWorkbook.Path
sFile = Dir(sPath & "\*.xls")
Do While sFile <> ""
'Open xls data file that needs to be formatted...
If sFile <> "Master Import File.xls" Then
Debug.Print sFile
Set oWB = oExcel.Workbooks.Open(sPath & "\" & sFile)
With oWB
.Activate
Call Formatting
.Close
End With
Set oWB = Nothing
End If
sFile = Dir
Loop
Set oExcel = Nothing
End Sub
Sub Formatting()
ActiveSheet.Rows("1:7").Select
Selection.Delete Shift:=xlUp
ActiveSheet.Columns("A:A").ColumnWidth = 83.14
ActiveSheet.Cells.Select
With Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveSheet.Columns("B").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("D").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("H:I").Select
ActiveSheet.Range("I1").Activate
Selection.Delete Shift:=xlToLeft
ActiveSheet.Columns("B:H").Select
Selection.ColumnWidth = 15
ActiveSheet.Range("A1").Select
ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
Selection.Interior.ColorIndex = 6
ActiveWorkbook.Save
End Sub
Thanks!
Rob