D
DavidH56
Hi,
I'm currently using Ron Debruin's code to consolidate workbooks. I'm having
difficulty with trying to elminate extra row headers which are in row 1. I'd
like the final workbook to only have one row header instead of several. All
row headers are alike. Any assistance that anyone can provide would be
greatly appreciated. Please see my code here:
Sub GetData_FromFiles()
'Copy cells from folder and subfolder(s)
Dim Subfolders As Boolean
Dim Fsbj As Object, RootFolder As Object
Dim SubFolderInRoot As Object, file As Object
Dim RootPath, FilesInPath As String
Dim sh As Worksheet, destrange As Range
Dim rnum As Long
Dim MyFiles() As String
Dim Fnum As Long
Dim FileExt As String
Dim myWorkbook As Workbook
Dim rng As Range
Dim Rwcount As Long
Set myWorkbook = Workbooks.Add(1) 'single sheet, same as xlwbatworksheet
'myWorkbook.Worksheets(1).Name = "DeleteMeLater"
'Loop through all files in the Root folder
RootPath = "c:\Temp\Reports" ' <<<< Change"
'Loop through the subfolders True or False
Subfolders = True
'Loop through files with this extension (*.xl* is all Excel files)
FileExt = "*.xl*"
'Add a slash at the end if the user forget it
If Right(RootPath, 1) <> "\" Then
RootPath = RootPath & "\"
End If
Set Fsbj = CreateObject("Scripting.FileSystemObject")
If Not Fsbj.FolderExists(RootPath) Then
MsgBox RootPath & " Not exist"
Exit Sub
End If
Set RootFolder = Fsbj.GetFolder(RootPath)
'Fill the array(myFiles)with the list of Excel files in the folder(s)
Erase MyFiles()
Fnum = 0
'Loop through the files in the RootFolder
For Each file In RootFolder.Files
If LCase(file.Name) Like LCase(FileExt) Then
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = RootPath & file.Name
End If
Next file
'Loop through the files in the Sub Folders if SubFolders = True
If Subfolders Then
Call ListFilesInSubfolders(OfFolder:=RootFolder)
End If
' Now we can loop through the files in the array MyFiles to get the cell
values
'******************************************************************
'Add worksheet to the Activeworkbook and use the Date/Time as name
Set sh = ActiveWorkbook.Worksheets.Add
sh.Name = Format(Now, "dd-mmm-yy hhnn") & "hrs"
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
'Find the last row with data
rnum = LastRow(sh)
'create the destination cell address
Set destrange = sh.Cells(rnum + 1, "A")
' Copy the workbook name in Column E
'sh.Cells(rnum + 1, "E").Value = MyFiles(Fnum)
'Get the cell values and copy it in the destrange
'Change the Sheet name and range as you like
GetData MyFiles(Fnum), "RDU", "A:Q", destrange, False, False
Next
End If
'Sort By Job Order then RDU
Cells.Select
Selection.Sort Key1:=Range("F1"), Order1:=xlAscending, Key2:=Range("L1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'' The following commands assign a line item number to each row of data
where there
' are no blank rows in the data area and each row only contains one
complete data
' record. This also assumes the top row consists of column headings.
Range("B1").Select
Selection.End(xlDown).Select
MyLastRow = ActiveCell.Row
Range("A1").Select
ActiveCell.FormulaR1C1 = "=ROW()-1"
Range("A1").Select
Selection.Copy
Range("A1:A" & MyLastRow).Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
'Format Cell Borders
Application.StatusBar = "Formatting Borders...."
Range("A1").CurrentRegion.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
Namefixer
TrimColF
'Name Ranges
CreateNames
FormatDateCols
AdjustColumnWidths
'Format Header Row
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
'Format Font
Range("A2:R2").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 8
End With
Range("A2:R2").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
FormatPageHeaderFooterA
Range("A1").Select
AddFormula
AutoFillMacro
SumFormula_InM
AlignHeaderRow
FinishHeader
FixColHandI
FormatCellBorders
FormatHeaderBorders
LockFormulaCells
TestReference
CreateEventProcedure
CloseVBEwindow
End Sub
Again thanks in advance.
I'm currently using Ron Debruin's code to consolidate workbooks. I'm having
difficulty with trying to elminate extra row headers which are in row 1. I'd
like the final workbook to only have one row header instead of several. All
row headers are alike. Any assistance that anyone can provide would be
greatly appreciated. Please see my code here:
Sub GetData_FromFiles()
'Copy cells from folder and subfolder(s)
Dim Subfolders As Boolean
Dim Fsbj As Object, RootFolder As Object
Dim SubFolderInRoot As Object, file As Object
Dim RootPath, FilesInPath As String
Dim sh As Worksheet, destrange As Range
Dim rnum As Long
Dim MyFiles() As String
Dim Fnum As Long
Dim FileExt As String
Dim myWorkbook As Workbook
Dim rng As Range
Dim Rwcount As Long
Set myWorkbook = Workbooks.Add(1) 'single sheet, same as xlwbatworksheet
'myWorkbook.Worksheets(1).Name = "DeleteMeLater"
'Loop through all files in the Root folder
RootPath = "c:\Temp\Reports" ' <<<< Change"
'Loop through the subfolders True or False
Subfolders = True
'Loop through files with this extension (*.xl* is all Excel files)
FileExt = "*.xl*"
'Add a slash at the end if the user forget it
If Right(RootPath, 1) <> "\" Then
RootPath = RootPath & "\"
End If
Set Fsbj = CreateObject("Scripting.FileSystemObject")
If Not Fsbj.FolderExists(RootPath) Then
MsgBox RootPath & " Not exist"
Exit Sub
End If
Set RootFolder = Fsbj.GetFolder(RootPath)
'Fill the array(myFiles)with the list of Excel files in the folder(s)
Erase MyFiles()
Fnum = 0
'Loop through the files in the RootFolder
For Each file In RootFolder.Files
If LCase(file.Name) Like LCase(FileExt) Then
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = RootPath & file.Name
End If
Next file
'Loop through the files in the Sub Folders if SubFolders = True
If Subfolders Then
Call ListFilesInSubfolders(OfFolder:=RootFolder)
End If
' Now we can loop through the files in the array MyFiles to get the cell
values
'******************************************************************
'Add worksheet to the Activeworkbook and use the Date/Time as name
Set sh = ActiveWorkbook.Worksheets.Add
sh.Name = Format(Now, "dd-mmm-yy hhnn") & "hrs"
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
'Find the last row with data
rnum = LastRow(sh)
'create the destination cell address
Set destrange = sh.Cells(rnum + 1, "A")
' Copy the workbook name in Column E
'sh.Cells(rnum + 1, "E").Value = MyFiles(Fnum)
'Get the cell values and copy it in the destrange
'Change the Sheet name and range as you like
GetData MyFiles(Fnum), "RDU", "A:Q", destrange, False, False
Next
End If
'Sort By Job Order then RDU
Cells.Select
Selection.Sort Key1:=Range("F1"), Order1:=xlAscending, Key2:=Range("L1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'' The following commands assign a line item number to each row of data
where there
' are no blank rows in the data area and each row only contains one
complete data
' record. This also assumes the top row consists of column headings.
Range("B1").Select
Selection.End(xlDown).Select
MyLastRow = ActiveCell.Row
Range("A1").Select
ActiveCell.FormulaR1C1 = "=ROW()-1"
Range("A1").Select
Selection.Copy
Range("A1:A" & MyLastRow).Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
'Format Cell Borders
Application.StatusBar = "Formatting Borders...."
Range("A1").CurrentRegion.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
Namefixer
TrimColF
'Name Ranges
CreateNames
FormatDateCols
AdjustColumnWidths
'Format Header Row
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
'Format Font
Range("A2:R2").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 8
End With
Range("A2:R2").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
FormatPageHeaderFooterA
Range("A1").Select
AddFormula
AutoFillMacro
SumFormula_InM
AlignHeaderRow
FinishHeader
FixColHandI
FormatCellBorders
FormatHeaderBorders
LockFormulaCells
TestReference
CreateEventProcedure
CloseVBEwindow
End Sub
Again thanks in advance.