Option Explicit
'http://www.mrexcel.com/forum/excel-questions/808685-autofit-all-cells-customer-sort-comany-name-then-insert-line-break-between-each-different-company-name.html
Const BOMHeaderStartRow As Integer = 4
Const MinBOMDetailStartRow As Integer = 9
Const DefaultBOMDetailColCount As Integer = 17
Sub directorylisting()
Dim Coll_Docs As New Collection
Dim Search_path, Search_Filter, Search_Fullname As String
Dim DocName As String
Dim messagebox As Integer
Dim i As Long
Dim lX As Long 'new
Search_path = "C:\RSMBOMCheck\Files" ' where ?
'Search_Filter = "*.xls" ' what ?
Set Coll_Docs = Nothing
DocName = Dir(Search_path & "\")
Do Until DocName = "" ' build the collection
Coll_Docs.Add Item:=DocName
DocName = Dir
Loop
For i = Coll_Docs.Count To 1 Step -1 '
Workbooks.OpenText filename:="C:\RSMBOMCheck\CheckBomTemplate.xlsx", DataType:=xlDelimited, Comma:=True
x = 1
Search_Fullname = Search_path & "\" & Coll_Docs(i)
Application.DisplayAlerts = False
Workbooks.OpenText filename:=Search_Fullname, DataType:=xlDelimited, Comma:=True
FormatReport
bomchecks (Coll_Docs(i))
ActiveWorkbook.SaveAs "C:\RSMBOMCheck\Done\CheckSheet_" & Range("a2") & ".xlsx"
ActiveWorkbook.Close
Next
MsgBox "Done"
End Sub
Sub FormatReport()
On Error Resume Next '[A] 'Should be limited to lines causing error, then
'work at getting rid of error
Dim iBOMRowCount As Integer
Dim iBOMColCount As Integer
Dim iBOMStartRow As Integer
'Dim iEngRowCount As Integer
'Dim iEngColCount As Integer
'Dim iEngStartRow As Integer
Dim iBOMHdrRowCount As Integer '[A]
iBOMRowCount = 0 '[A]
iBOMColCount = 0 '[A]
iBOMStartRow = 0 '[A]
iBOMRowCount = Sheets("Counts").Cells(1, 1).Value
iBOMColCount = Sheets("Counts").Cells(2, 1).Value '14
iBOMHdrRowCount = Sheets("Counts").Cells(1, 2).Value '[A]
'iBOMStartRow = Sheets("Counts").Cells(3, 1).Value '13 '[D]
iBOMStartRow = BOMHeaderStartRow + iBOMHdrRowCount + 1 '[A]
'iEngRowCount = Sheets("Counts").Cells(4, 1).Value '1
'iEngColCount = Sheets("Counts").Cells(5, 1).Value '3
'iEngStartRow = Sheets("Counts").Cells(6, 1).Value 'iBOMStartRow + iBOMRowCount + 5
If (iBOMStartRow < MinBOMDetailStartRow) Then iBOMStartRow = MinBOMDetailStartRow '[A]
'If # of columns in BOMDetail is not provided, use 17 (the max)
If (iBOMColCount < 1) Then iBOMColCount = DefaultBOMDetailColCount '[A]
Sheets("BOM Report").Select
With Range(Cells(BOMHeaderStartRow, 1), Cells(MinBOMDetailStartRow - 1, 3))
.UnMerge
.Borders.LineStyle = xlNone
.Font.FontStyle = xlNormal
.Font.Bold = False
.Font.Name = "Arial"
.Font.Size = 8
.Font.ColorIndex = xlColorIndexAutomatic
End With
With Range(Cells(MinBOMDetailStartRow, 1), Cells(200, 25))
.UnMerge
.Borders.LineStyle = xlNone
.Font.FontStyle = xlNormal
.Font.Bold = False
.Font.Name = "Arial"
.Font.Size = 8
.Font.ColorIndex = xlColorIndexAutomatic
End With
Call SetupHeaderFormatting(iBOMHdrRowCount)
'Setup BOM Detail Formatting
' Range(Cells(iBOMStartRow, 1), Cells(iBOMStartRow + iBOMRowCount, iBOMColCount)).Select
'
' With Selection.Borders(xlEdgeLeft)
' .LineStyle = xlContinuous
' .Weight = xlThin
' .ColorIndex = xlAutomatic
' End With
' 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
For lX = 7 To 12
On Error Resume Next 'In case only 1 row or column selected
' otherwise error on xlInsideHorizontal & xlInsideVertical
With Range(Cells(iBOMStartRow, 1), Cells(iBOMStartRow + iBOMRowCount, iBOMColCount)).Borders(lX)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
On Error GoTo 0
Next
With Range(Cells(iBOMStartRow, 1), Cells(iBOMStartRow + iBOMRowCount, iBOMColCount))
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
With .Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
End With
With Range(Cells(iBOMStartRow, 1), Cells(iBOMStartRow, iBOMColCount))
.Font.Bold = True
.EntireRow.AutoFit
End With
'Bug 1412 Fix - BEGIN {
If (iBOMRowCount > 0) Then
With Range(Cells(iBOMStartRow + 1, 1), Cells(iBOMStartRow + iBOMRowCount, iBOMColCount))
.Font.Bold = False
.Rows.AutoFit
End With
End If
'Bug 1412 Fix - END }
' EngSpec - Comment out 07/05/00 - waiting for BE provide Generic Eng data
'Range(Cells(iEngStartRow, 1), Cells(iEngStartRow + iEngRowCount, iEngColCount)).Select
'With Selection.Borders(xlEdgeLeft)
' .LineStyle = xlContinuous
' .Weight = xlThin
' .ColorIndex = xlAutomatic
'End With
'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
'With Selection.Font
' .Name = "Arial"
' .FontStyle = "Regular"
' .Size = 8
' .Strikethrough = False
' .Superscript = False
' .Subscript = False
' .OutlineFont = False
' .Shadow = False
' .Underline = xlUnderlineStyleNone
' .ColorIndex = xlAutomatic
'End With
'With Selection
' .HorizontalAlignment = xlGeneral
' .VerticalAlignment = xlBottom
' .WrapText = True
' .Orientation = 0
' .AddIndent = False
' .ShrinkToFit = False
' .MergeCells = False
'End With
'Range(Cells(iEngStartRow, 1), Cells(iEngStartRow, iEngColCount)).Select
'Selection.Font.Bold = True
Range(Cells(1, 1), Cells(1, 1)).Select
End Sub
'*********************************************************************
'* Name: SetupHeaderFormatting
'* Purpose: Setup the merging, borders and font of cells which display
'* the BOM Header information
'*
'* Parameters:
'* iRowCount Integer # of rows used for displaying the BOM Header
'*
'* Returns: Nothing
'*
'* Logic/PseudoCode:
'* Create a Range going from Row#4.Col1 to Row #4+iRowCount-1.Col#3
'* Set the font properties of this range (Arial, 8Pt, Normal, AutoColor)
'* For iRowCount # of Rows starting at Row #4
'* Make a range of the first 2 cells in the row
'* Merge the range
'* Setup borders around it (thin, continuous)
'* Make its font Bold
'*
'* Setup Borders around Cell#3 in iRow (col #3)
'* End For
'*
'* Change History:
'* 02/21/03 v-vikask Initial Version
'*********************************************************************
Private Sub SetupHeaderFormatting(iRowCount As Integer)
On Error GoTo ErrHandler
Dim iStartRow As Integer
Dim iRow As Integer
Dim sh As Worksheet
Dim rRange As Range
iStartRow = BOMHeaderStartRow
Set sh = ActiveSheet
If (iRowCount = 0) Then Exit Sub
Set rRange = sh.Range(sh.Cells(iStartRow, 1), sh.Cells(iStartRow + iRowCount - 1, 3))
With rRange.Font
.FontStyle = xlNormal
.Bold = False
.Name = "Arial"
.Size = 8
.ColorIndex = xlColorIndexAutomatic
End With
For iRow = iStartRow To iStartRow + iRowCount - 1 Step 1
Set rRange = sh.Range(sh.Cells(iRow, 1), sh.Cells(iRow, 2))
With rRange
.Merge
Call .BorderAround(xlContinuous, xlThin, xlColorIndexAutomatic)
.Font.Bold = True
End With
With sh.Cells(iRow, 3)
Call .BorderAround(xlContinuous, xlThin, xlColorIndexAutomatic)
End With
Next iRow
Set rRange = sh.Range(sh.Cells(iStartRow, 1), sh.Cells(iStartRow + iRowCount - 1, 3))
rRange.EntireRow.AutoFit
Exit Sub
ErrHandler:
Exit Sub 'Ignore Errors
End Sub
Sub bomchecks(filename)
'
' bomchecks Macro
'
Rows("25:25").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C15").Copy Destination:=Range("B25")
Range("A15:B15").FormulaR1C1 = "UPC"
Range("C25").FormulaR1C1 = "UPC"
Rows("1:22").Delete Shift:=xlUp
Columns("A:A").Delete Shift:=xlToLeft
Columns("A:A").Cut
Windows("CheckBomTemplate.xlsx").Activate
Columns("A:A").Paste
Windows("3pt-00018-Oct 6 2014 05.09.27.XLS").Activate
Columns("B:B").Cut
Windows("CheckBomTemplate.xlsx").Activate
Columns("B:B").Paste
Windows("3pt-00018-Oct 6 2014 05.09.27.XLS").Activate
Columns("G:G").Cut
Windows("CheckBomTemplate.xlsx").Activate
Columns("D:D").Paste
End Sub