macro getting bugged while re run - vba

Not able to re run below macro does anyone know why it breaks on file location ?
Also does someone knows how to loop this macro to pick up more sheets in to the workbook?
trying to pick up the sheets from workbook 2 and format them in below manner.
Sub specalign()
'
' specalign Macro
'
'
Workbooks.Open Filename:= _
"C:\\\\Interface Specifications Master v7 7.xlsx"
Sheets("1.1.1").Select
Sheets("1.1.1").Move After:=Workbooks( _
"Interface Specifications Master v7.8.xlsx").Sheets(2)
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
Columns("N:N").Select
Selection.Delete Shift:=xlToLeft
Range("N1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.ClearContents
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1:N1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("Table1[[#Headers],[Spec ID]]").Select
Selection.AutoFilter
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("Table1[[#Headers],[Spec ID]]").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.Name = "Arial"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A3:B3,G3:M3").Select
Range("Table1[[#Headers],[ Conditionality ]]").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("3:3").RowHeight = 108.75
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("A:A").ColumnWidth = 8#
Columns("B:B").ColumnWidth = 8#
Columns("C:C").ColumnWidth = 34#
Columns("D:D").ColumnWidth = 22#
Columns("E:E").ColumnWidth = 17#
Columns("G:G").ColumnWidth = 8#
Columns("H:H").ColumnWidth = 22
Columns("I:I").ColumnWidth = 8#
Columns("J:J").ColumnWidth = 8#
Columns("K:K").ColumnWidth = 8
Columns("L:L").ColumnWidth = 6#
Columns("M:M").ColumnWidth = 10
End Sub

going off of what #nickslash said your filename looks like it's this issue.
however, there are many things you can do improve this code like getting rid of the scrollcolumns and all the selections.
To get the file path you can right click on the file in your folder and select properties. > On the General Tab select location. Then you'll see the path you'll need.
As you learn vba you'll do better than what I've put here. and select files and make it more dynamic (maybe a prompt to select the file you want as jean demonstrates here).
Update below:
Sub specalign()
Dim ws As Worksheet
Dim wb1 As Workbook
Dim wb2 As Workbook
'this is what mine looks like with the directory
Set wb1 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7.8.xlsx") 'target workbook .8 I kept
Set wb2 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7 7.xlsx") 'source workbook
For Each ws In wb2.Worksheets
ws.Copy after:=wb1.Worksheets(2)
Next

Related

VBA - Simpler Code for Formatting

I was hoping that there is simpler code for my formatted cells below. The goal of this code is to make a box look a bit nicer, nothing overly fancy, just simple and this code doesn't look simple.
Range("C17:C25").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("A17:C25").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("C17:C25").Select
With Selection.Font
.ColorIndex = xlAutomatic
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
I recorded with macros obviously, however, it is very clunky and I am sure that some of it can be taken out, just unsure what can and can't be taken out/modified.
Thanks in advance.
Try this. You can dispense with the selects too. Most of the formatting at the end is probably redundant, unless you have previous settings, and looks as if it was generated by the macro recorder.
Sub x()
With Range("C17:C25")
With .Borders
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Font
.ColorIndex = xlAutomatic
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
With .Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
With Range("A17:C25")
.BorderAround LineStyle:=xlContinuous, ColorIndex:=0, Weight:=xlMedium
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End With
End Sub
I reformatted it once again to something even simpler. I'd love some feed back to ensure I am not doing anything taboo.
With Range("C17:C25")
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
With .Font
.ColorIndex = xlAutomatic
.Name = "Arial"
.Size = 12
End With
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
With .Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
With Range("A17:C25")
.BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End With

Compare Two Excel sheets and find the difference

I want to find out or highlight the differences between two Excel sheets.
from above image I want to compare both sheets based on "Name" and "RuleName", if the number matches it needs to check differences for "Text" and "Rule Text" and it needs to find the differences like highlighted text in second Excel document.
This should do:
Sub HighlightDiffBtwSheets()
'Substitute "TEST1" with the name of the sheet where you have the Name-Text columns
'Substitute "TEST2" with the name of the sheet where you have the RuleName-RuleText columns
'Substitute A in the Range with the column letter of Name/RuleName
For Each Name In Sheets("TEST1").Range("A2:A" & Sheets("TEST1").Cells(Rows.Count, 1).End(xlUp).Row)
For Each RuleName In Sheets("TEST2").Range("A2:A" & Sheets("TEST2").Cells(Rows.Count, 1).End(xlUp).Row)
If InStr(RuleName.Value, Name.Value) <> 0 Then
If Name.Offset(, 1).Value <> RuleName.Offset(, 1).Value Then
RuleName.Offset(, 1).Select
With Selection.Interior
.Color = 65535
End With
End If
End If
Next
Next
End Sub
An easier non VBA way to do this is to use Conditional Formatting. Just Create A New Rule, then select Use Formula option. Use a relative reference (no dollar signs) and copy to where you need it. For example, =A1<>Sheet1!A1
I have created this file to compare two excel workbooks few years back, code is very elemantary but it does work with few limitations.
Limitations:
both file should not have same name
it only compare values in the cell, does not compare any graphics.
It is only comparing first 300 rows and first 200 columns, you can very easily update this in code to fit your need.
Code is divided into two subs. 1. Compareworkbook and 2. CreateNewWorksheet
You can creat browse button macro to populate two excel file names in named cell "file1" and named cell "file2". Then you can use Compareworkbook macro to compare two excel files. Once you run "Compareworkbook" macro, it will create new worksheet to show you the report. it only shows the values which are different.
You can modify this code to compare certain columns or to fit your need. This should give you a good starting point.
Sub CompareWorkbook1()
'this subroutine is created to compare two excel files
'This will only compare first 300 rows and 150 column for all worksheet
'in both workbook assuming both workbook has same number of worksheets
Dim wb As Workbook, wb1 As Workbook, wb2 As Workbook
Dim ws As Worksheet
Dim wBook1 As Variant
Dim wBook2 As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
wBook1 = ActiveWorkbook.Sheets("Sheet1").Range("file1").Value
wBook2 = ActiveWorkbook.Sheets("Sheet1").Range("file2").Value
Answer = MsgBox("This will generate a new report, Do you want to proceed?", vbQuestion + vbYesNo, "Are you sure? This will delete existing reports and generate new reports")
If Answer = vbNo Then
GoTo exit1
Else
If Range("file1").Value = "" Then
Msg = "ERROR: INFORMATION MISSING ..." & vbNewLine & vbNewLine
Msg = Msg & "Make sure you browse the file "
Msg = Msg & "by clicking on Browse button next to Step 1 " & vbNewLine & vbNewLine
Msg = Msg & "REPORT WILL NOT GENERATE"
MsgBox Msg, vbCritical
GoTo exit1
End If
If Range("file2").Value = "" Then
Msg = "ERROR: INFORMATION MISSING ..." & vbNewLine & vbNewLine
Msg = Msg & "Make sure you browse the file "
Msg = Msg & "by clicking on Browse button next to Step 2 " & vbNewLine & vbNewLine
Msg = Msg & "REPORT WILL NOT GENERATE"
MsgBox Msg, vbCritical
GoTo exit1
End If
'generate new worksheet
ReportName = "Comparison Results"
Call CreateNewWorksheet(ReportName)
'set workbooks as variable wb1 and wb2
Set wb1 = Workbooks.Open(wBook1)
Set wb2 = Workbooks.Open(wBook2)
wb.Sheets(2).Cells(4, 2).Value = wb1.Name
wb.Sheets(2).Cells(4, 3).Value = wb2.Name
wb.Sheets(2).Cells(3, 7).Value = wb1.Name
wb.Sheets(2).Cells(3, 10).Value = wb2.Name
'Pull data from browsed workbook for All incident
'MsgBox "WOrkbooks are opened"
ThisWorkbook.Activate
Dim oSheet As Excel.Worksheet
'This will populate all Worksheet name in Combo box
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim wSheetsNo As Integer
Dim wSheetsNo1 As Integer
Dim wSheetsNo2 As Integer
a = 1
b = 1
c = 1
d = 1
wSheetsNo1 = 0
wSheetsNo2 = 0
a = 5
b = 2
For Each oSheet In wb1.Sheets
wb.Sheets(2).Cells(a, b) = oSheet.Name
a = a + 1
wSheetsNo1 = wSheetsNo1 + 1
Next oSheet
a = 5
b = 3
For Each oSheet In wb1.Sheets
wb.Sheets(2).Cells(a, b) = oSheet.Name
a = a + 1
wSheetsNo2 = wSheetsNo2 + 1
Next oSheet
a = 5
b = 7
'populates all worksheet from 1st workbook to compare
For wSheetsNo = 1 To wSheetsNo1
'Compares from row 1 to 300
For c = 1 To 300
'Compares columns 1 to 200
For d = 1 To 200
'Compares each cell value in each worksheets for these two workbook
If wb1.Sheets(wSheetsNo).Cells(c, d).Value <> wb2.Sheets(wSheetsNo).Cells(c, d).Value Then
wb.Sheets(2).Cells(a, b + 1) = "Cells (" & c & ", " & d & ")"
wb.Sheets(2).Cells(a, b + 4) = "Cells (" & c & ", " & d & ")"
wb.Sheets(2).Cells(a, b + 2) = wb1.Sheets(wSheetsNo).Cells(c, d).Value
wb.Sheets(2).Cells(a, b + 5) = wb2.Sheets(wSheetsNo).Cells(c, d).Value
wb.Sheets(2).Cells(a, b) = wb1.Sheets(wSheetsNo).Name
wb.Sheets(2).Cells(a, b + 3) = wb2.Sheets(wSheetsNo).Name
a = a + 1
End If
'looks into next column
Next
'looks into next row
Next
'looks into next worksheet
Next
'closes both workbook
wb1.Close
wb2.Close
End If
'exit if files is now browsed or path is empty
exit1:
End Sub
Sub CreateNewWorksheet(ReportName)
Dim wsSheet As Worksheet
On Error Resume Next
Set wsSheet = Sheets(ReportName)
On Error GoTo 0
If Not wsSheet Is Nothing Then
Application.DisplayAlerts = False
Sheets(ReportName).Delete
Application.DisplayAlerts = True
End If
'Add New sheet at end of worksheet
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = ReportName
Sheets("Comparison Results").Select
Range("B4").Select
Sheets("Comparison Results").Select
Range("B3").Select
ActiveCell.FormulaR1C1 = "Worksheets which are compared"
Range("B4").Select
Columns("B:B").ColumnWidth = 27.57
Columns("B:B").Select
Selection.ColumnWidth = 28
Columns("C:C").Select
Selection.ColumnWidth = 28
Range("B3:C3").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Font.Bold = True
Range("B4").Select
ActiveCell.FormulaR1C1 = "1st Workbook"
Range("C4").Select
ActiveCell.FormulaR1C1 = "2nd Workbook"
Range("B4:C4").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B3:C50").Select
ActiveWindow.SmallScroll Down:=-45
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("B3:C4").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10092543
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("B5:C50").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10092543
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("C13").Select
ActiveWindow.SmallScroll Down:=-15
Range("B3:C3").Select
ActiveCell.FormulaR1C1 = "Worksheets which are compared"
Columns("G:L").Select
Selection.ColumnWidth = 28
Selection.ColumnWidth = 10
Selection.ColumnWidth = 15
Selection.ColumnWidth = 18
Range("G3:I3").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Copy
Range("J3").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("G3:I3").Select
ActiveCell.FormulaR1C1 = ""
Range("G4").Select
ActiveCell.FormulaR1C1 = "Worksheet"
Range("H4").Select
ActiveCell.FormulaR1C1 = "Cell number"
Range("I4").Select
ActiveCell.FormulaR1C1 = "Value in the cell"
Range("G4:I4").Select
Selection.Copy
Range("J4").Select
ActiveSheet.Paste
Range("B4").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("C4").Select
Selection.ClearContents
Range("G3:L10000").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10092543
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("G3:L4").Select
Selection.Font.Bold = True
Range("B3:C4").Select
Selection.Font.Bold = False
Selection.Font.Bold = True
Range("B4:L4").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("G3:L3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Range("G5").Select
ActiveWindow.SmallScroll Down:=-15
Range("G3:I3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range("G3:L3").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("G3:L4").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("G3:L10000").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("B3:C4").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("B3:C50").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("G3:I10000").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns("D:F").Select
Range("F1").Activate
Selection.ColumnWidth = 3
Range("G2:L2").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10092543
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("G2:L2").Select
ActiveCell.FormulaR1C1 = "Comparison Results"
Range("G2:L2").Select
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("G2:L2").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
'Sheets("Sheet1").Select
Range("B2").Select
Range("G3:L4").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("B1").Select
End Sub

Splitting header row elements into separate rows one below the other by modifying VBA code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Current Output1
Expected output2
Whenever I change the code the data below the header gets overwritten. This macro returns 4 excel tabs.
the code is here:
Sub Import_Data()
Dim lastrow As Long
ThisWorkbook.Sheets(4).Select
lastrow = ActiveSheet.Range("A2").End(xlDown).Row
'For i = 2 To lastrow
'ActiveSheet.Select
'Range("C" & i).Value = Range("C" & i).Value / 1000000
'Next i
ActiveSheet.Range("A1:B" & lastrow).Select
Selection.Copy
Sheets("Industry Comparables (1 of 3)").Select
ActiveSheet.Range("A8").Select
ActiveSheet.Paste
Sheets("Industry Comparables (2 of 3)").Select
ActiveSheet.Range("A7").Select
ActiveSheet.Paste
Sheets("Industry Comparables (3 of 3)").Select
ActiveSheet.Range("A7").Select
ActiveSheet.Paste
ThisWorkbook.Sheets(4).Select
ActiveSheet.Select
ActiveSheet.Range("C1:O" & lastrow).Select
Selection.Copy
Sheets("Industry Comparables (1 of 3)").Select
ActiveSheet.Range("C8").Select
ActiveSheet.Paste
Range("B8").Value = "Name"
Range("C8").Value = "Market Cap ($ Mil.) (Most Recent Month End)"
Range("D8").Value = "Assets to Equity (CY)"
Range("E8").Value = "Assets to Equity (PY)"
Range("F8").Value = "Asset Turn- over (CY)"
Range("G8").Value = "Asset Turn- over (PY)"
Range("H8").Value = "Sales /Inven Turn- over (CY)"
Range("I8").Value = "Sales /Inven Turn- over (PY)"
Range("J8").Value = "Receiv- ables Turn- over (CY)"
Range("K8").Value = "Receiv- ables Turn- over (PY)"
Range("L8").Value = "Current Ratio (CY)"
Range("M8").Value = "Current Ratio (PY)"
Range("N8").Value = "Quick Ratio (CY)"
Range("O8").Value = "Quick Ratio (PY)"
Range("B10:B12").Select
Selection.ClearContents
ThisWorkbook.Sheets(4).Select
ActiveSheet.Select
ActiveSheet.Range("P1:Y" & lastrow).Select
Selection.Copy
Sheets("Industry Comparables (2 of 3)").Select
ActiveSheet.Range("C7").Select
ActiveSheet.Paste
Range("B7").Value = "Name"
Range("C7").Value = "Total Debt% Total Assets (CY)"
Range("D7").Value = "Total Debt% Total Assets (PY)"
Range("E7").Value = "Total Debt% Total Equity (CY)"
Range("F7").Value = "Total Debt% Total Equity (PY)"
Range("G7").Value = "L T Debt% Total Capital (CY)"
Range("H7").Value = "L T Debt% Total Capital (PY)"
Range("I7").Value = "S T Debt% Total Debt (CY)"
Range("J7").Value = "S T Debt% Total Debt (PY)"
Range("K7").Value = "Net Cash Fl % Total Debt (CY)"
Range("L7").Value = "Net Cash Fl % Total Debt (PY)"
Range("B9:B11").Select
Selection.ClearContents
ThisWorkbook.Sheets(4).Select
ActiveSheet.Select
ActiveSheet.Range("Z1:AK" & lastrow).Select
Selection.Copy
Sheets("Industry Comparables (3 of 3)").Select
ActiveSheet.Range("C7").Select
ActiveSheet.Paste
Range("B7").Value = "Name"
Range("C7").Value = "Gross Income Margin (CY)"
Range("D7").Value = "Gross Income Margin (PY)"
Range("E7").Value = "Net Income Margin (CY)"
Range("F7").Value = "Net Income Margin (PY)"
Range("G7").Value = "Oper Margin (CY)"
Range("H7").Value = "Oper Margin (PY)"
Range("I7").Value = "Return on Avg Total Equity (CY)"
Range("J7").Value = "Return on Avg Total Equity (PY)"
Range("K7").Value = "Basic EPS Before Extra- ordinary Items (CY)"
Range("L7").Value = "Basic EPS Before Extra- ordinary Items (PY)"
Range("M7").Value = "Diluted EPS Before Extra- Ordinary Items (CY)"
Range("N7").Value = "Diluted EPS Before Extra- Ordinary Items (PY)"
Range("B9:B11").Select
Selection.ClearContents
Application.CutCopyMode = False
'Application.DisplayAlerts = False
'Sheets(4).Delete
'Application.DisplayAlerts = True
End Sub
Sub Comp1Macro()
Dim lastrow As Integer
Sheets("Industry Comparables (1 of 3)").Select
With ActiveWindow
.SplitColumn = 0
.SplitRow = 0
End With
ActiveWindow.FreezePanes = False
Range("A1").Select
ActiveCell.FormulaR1C1 = "GICS Industry-" & Sheets(4).Range("AN2").Value
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.Font.Bold = True
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"The following is an analysis of key ratios/metrics for the issuer compared to other issuers in the same industry."
'Rows("3:7").Select
'Selection.Insert Shift:=xlDown
Range("A3").Select
ActiveCell.FormulaR1C1 = _
"Current Year (CY) ratios are based on each issuer's most recent fiscal year end financials."
Range("A4").Select
ActiveCell.FormulaR1C1 = _
"Prior Year (PY) ratios are based on the year prior to each issuer's most recent fiscal year end financials."
Range("A6").Select
ActiveCell.FormulaR1C1 = "Note 1 - Market Cap is as of most recent month end prior to this issuer profile report date."
Range("A2:A4").Select
With Selection.Font
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
'Rows("8:17").Select
'Selection.Delete Shift:=xlUp
Range("A8:O8").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
Selection.Font.ColorIndex = 2
With Selection.Interior
.ColorIndex = 9
.Pattern = xlSolid
End With
Rows("8:8").EntireRow.AutoFit
Range("A8:O8").Select
Selection.Font.ColorIndex = 2
Range("A8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
'Rows("9:12").Select
'Selection.Insert Shift:=xlDown
Range("A9:O9").Select
Selection.Interior.ColorIndex = 6
Range("A9:B9").Select
Selection.Font.Bold = True
Range("C9:O9").Select
Selection.Font.Bold = True
Range("A10").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A10:O12").Select
Selection.Interior.ColorIndex = 5
Selection.Font.ColorIndex = 2
Range("A10").Select
ActiveCell.FormulaR1C1 = "Upper quartile of Comparables"
Range("A11").Select
ActiveCell.FormulaR1C1 = "Median of Comparables"
Range("A12").Select
ActiveCell.FormulaR1C1 = "Lower quartile of Comparables"
Range("A10:O12").Select
Selection.Font.Bold = True
ActiveSheet.UsedRange
lastrow = ActiveSheet.Range("A13").End(xlDown).Row
Range("A13").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("A13:O" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("B8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("C8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("C8:C" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("D8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("D8:E" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("F8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("F8:G" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("H8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("H8:I" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("J8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("J8:K" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("L8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("L8:M" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("N8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("N8:O" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Range("A8").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("A8:O" & lastrow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Columns("A:A").Select
Selection.ColumnWidth = 8
Range("B:B").Select
Selection.ColumnWidth = 21
Range("A9:B9").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Selection.Merge
Range("A10:B10").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Merge
Range("A11:B11").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Merge
Range("A12:B12").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("A9:O12").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.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 = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range("A1:O" & lastrow).Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$O$" & lastrow
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$12"
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = "$A$1:$O$" & lastrow
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = "&"",Bold""&11Confidential - Not for External Distribution"
.LeftFooter = "&P of &N"
.CenterFooter = ""
.RightFooter = "&"",Bold""&11Comparable 1 of 3&"",Regular""&9" & Chr(10) & ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 70
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
End With
Selection.Replace What:="#N/A", Replacement:="No Data", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.SpecialCells(xlLastCell).Select
Columns("P:P").Select
Selection.Delete Shift:=xlToLeft
Range("C9:O" & lastrow).Select
Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""?_);_(#_)"
Range("A9:O9").Select
With Selection
Selection.Font.ColorIndex = 1
End With
Cells.Select
With Selection.Font
.Name = "Arial"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A8:O" & lastrow).Select
With Selection.Font
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
End Sub
There is a similar code for the next 4 tabs which are generated as output after running this macro. I want the same result for all the tabs. If the answer i get for this code works then I can similarly tweak the other codes.
thank you
The required change is pretty straightforward but to be honest the code is in need of a total re-write, and there's so much of it that it's unlikely anyone is going to do that for you.
Range("B7").Value = "Name"
Range("C7").Value = "Gross Income Margin"
Range("D7").Value = "Gross Income Margin"
Range("E7").Value = "Net Income Margin"
Range("F7").Value = "Net Income Margin"
'etc
Range("B8").Value = ""
Range("C8").Value = "(CY)"
Range("D8").Value = "(PY)"
Range("E8").Value = "(CY)"
Range("F8").Value = "(PY)"
'etc

Code work properly while pressing F8 and doesn't while pressing F5

I have a code, which create sheets with some formats. I have a problem that formatting doesn't work properly (for example it changes color of different range or it doesn't merge cells).
I thought that maybe I did something in wrong order or something like that so I started to press F8 from the beggining to very end. And while doing it, it made exactly a sheet like I wanted.
My code is quite long coz there is many subs inside so I'll try to write how it works and insert important parts. If it won't be enought, I'll put here the rest of my code.
It takes name of the first project from ResourcesProjects sheet. It is stored in sResourcesProjectName variable (it works properly)
Next, it calls 3 subs which create 3 sheets: ResourceSheet, DesignExecutionSheet and RisksSheet.
All of this subs have code to create and format created sheet.
First sub (ResourceSheet) works properly, formatting is exactly like I wanted.
The problem is with subs DesignExecutionSheet and RisksSheet. It is not formatted well when it goes by F5.
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
Sheets(sDesignSheetName).Activate
Cells.EntireColumn.Hidden = False
Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
Columns("C:C").ColumnWidth = 3
Columns("D:D").ColumnWidth = 25
Rows("8:8").RowHeight = 25
Rows("12:12").RowHeight = 25
Rows("17:17").RowHeight = 25
Range("C8:E8,C12:E12,C17:E17").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").Select
Selection.RowHeight = 20
Range("C9:C10,C13:C15,C18:C20").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Range("C9:E10,C13:E15,C18:E20").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A6").Select
Range("D9:D10,D13:D15,D18:D20").Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Selection.Font.Bold = True
Range("D9").Value = "ASSIGNED TO IT&V:"
Range("D10").Value = "COVERED BY IT&V:"
Range("D13").Value = "EXECUTED:"
Range("D14").Value = "PASSED:"
Range("D15").Value = "FAILED:"
Range("D18").Value = "OPEN:"
Range("D19").Value = "CLOSED:"
Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
with RisksSheet() there is similar problem so I don't put here code from that sub.
After calling this subs, there is only this in code:
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
EDIT:
I edited my code like Luboš Suk suggested. Now the problem is different. By pressing F8 it works again properly, but by pressing F5 there is an error "application defined or object defined error" in line
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
What is more, the error isn't appear when I set a breakpoint in main sub in line
Call DesignExecutionSheet
and press F5 twice.
I also figured out that when I don't multiple ranges (for example only "C8:E8" instead of "C8:E8,C12:E12,C17:E17") it also works.
But I have many ranges inside, the code will be very long if I'll divide all of that ranges.
This is my code:
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
'wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
'Sheets(sDesignSheetName).Activate
With Sheets(sDesignSheetName)
.Cells.EntireColumn.Hidden = False
.Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
.Columns("C:C").ColumnWidth = 3
.Columns("D:D").ColumnWidth = 25
.Rows("8:8").RowHeight = 25
.Rows("12:12").RowHeight = 25
.Rows("17:17").RowHeight = 25
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
.Font.Bold = True
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17").Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Sheets(sDesignSheetName).Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Sheets(sDesignSheetName).Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Sheets(sDesignSheetName).Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Sheets(sDesignSheetName).Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").RowHeight = 20
With Sheets(sDesignSheetName).Range("C9:C10,C13:C15,C18:C20").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalDown).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalUp).LineStyle = xlNone
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideVertical).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideHorizontal).LineStyle = xlNone
' .Range("A6").Select
With Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20")
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20").Font.Bold = True
Sheets(sDesignSheetName).Range("D9").Value = "ASSIGNED TO IT&V:"
Sheets(sDesignSheetName).Range("D10").Value = "COVERED BY IT&V:"
Sheets(sDesignSheetName).Range("D13").Value = "EXECUTED:"
Sheets(sDesignSheetName).Range("D14").Value = "PASSED:"
Sheets(sDesignSheetName).Range("D15").Value = "FAILED:"
Sheets(sDesignSheetName).Range("D18").Value = "OPEN:"
Sheets(sDesignSheetName).Range("D19").Value = "CLOSED:"
Sheets(sDesignSheetName).Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
So your main problem will be here, where you activate worksheet and then working with it.
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
I suggest you to use better aproach in your code. Properly declare variables, and work with sheets and cells like relative objects and AVOID .activate and .select
In short therm use something like this. This approach is much more robust for you and readable.
Sub doSomething()
Dim myCuteSheet As Worksheet
Set myCuteSheet = Sheets("pinkRidignSheet")
With myCuteSheet
.Range(.Cells(1,1),.Cells(5,5)) 'then do something with range
.Cells(15,20) 'do something with cell
.Columns ("F") 'do something with column
End With
End Sub

Macro From Excel 2003 Doesn't work in Excel 2007

Recently I've upgraded from Excel 2003 to Excel 2007. Nearly all of the macros work, except for one part of one macro. On this sheet for this file, there are roughly 21 slots that have been sized to have pictures placed in them. Because of the nature of the work, sometimes there are a lot more than 21 pictures to input into the document. Before hand it was just a hassle because you would sometimes forget to copy rows over, and then couldn't resize the images properly.
So, upon inserting the images into the photo sheet and running the macro, if there are 21 or less photos it will simply place all of the photos into the slots and resize them. More or less, this works fine, there's a few things I have to tweak, but generally it's working.
The problem is the case for when there are > 21 photos inserted. The code was to find the last available picture cell and copy and paste the needed rows after it. Excel 2007 is not finding any of those cells. The formatting I copied from a recorded macro, which explains the odd styling choices.
The picture cells look like this:
I figured that perhaps something about how the styles of that box had been changed between 2003 and 2007, so I decided to record another macro to get the "new" formatting. But even with Excel's Find dialog and selecting one of the photo cells for its formatting, it gives me an error of "Excel cannot find the data you are looking for." As expected, there were subtle differences between the two Find Formats retrieved by the macro recorder, but neither of them find the cells like they did in Excel 2003. I'm not particularly sure what to do here; can anyone point me in the right direction of getting this to work like it did previously?
The code is this:
Cells.Find Code, 2007
Dim rng As Range
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "General"
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
With Application.FindFormat.Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.Underline = xlUnderlineStyleNone
.ThemeColor = 2
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Application.FindFormat.Borders(xlLeft)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlRight)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlTop)
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlBottom)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
Application.FindFormat.Borders(xlDiagonalDown).LineStyle = xlNone
Application.FindFormat.Borders(xlDiagonalUp).LineStyle = xlNone
With Application.FindFormat.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Application.FindFormat.Locked = True
Application.FindFormat.FormulaHidden = False
Set rng = Sheet2.Cells.Find(What:="", After:=Sheet2.Range("A6"), SearchDirection:=xlPrevious, SearchFormat:=True)
If rng Is Nothing Then
Debug.Print "Nothing"
End If
Cells.Find Code, 2003
Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
Dim r As Range
Set r = Range(start_cell)
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "General"
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
With Application.FindFormat.Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
With Application.FindFormat.Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
, SearchFormat:=True)
End Function
EDIT
Okay, so I figured out that for some reason, the "choose cell formatting" options was too specific. I went through and just manually chose some of the options who's values I could remember.
The code I currently have is, the error comes at the end of the function, and says Run Time Error '91': Object variable or With block variable not set., and highlights the End Function line.
I have checked to see that find_last_picture_cell is being populated with the correct cell (M102), and it is. But the code still gives me an error and I'm not sure why.
Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
Dim r As Range
Set r = Range(start_cell)
Application.FindFormat.Clear
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.MergeCells = True
End With
With Application.FindFormat.Font
.Subscript = False
.TintAndShade = 0
End With
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.ColorIndex = xlAutomatic
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Application.FindFormat.Locked = True
Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
, SearchFormat:=True)
End Function