VBA - Selecting Data - vba

I'm new to VBA entirely but have spent about a week tinkering with it. I'm in the process of trying to automate a pretty intensive task in excel through a combination of recording and messing about with the code from internet searches and my limited research into VBA. I've actually gotten pretty far into the process, but I've run into a problem that I can't seem to find info on. I assume it's a common issue so there is probably already stuff on it, I'm just not typing in the magic combination of words to search for the right answers.
My problem is this: I have a worksheet with about 10,000 rows of data and from this raw data I need to create 60 or so separate spreadsheets for 60 different companies- so around 160 some row actually pertain to a given client. However, it's not fixed and from one month to the next the actual number of rows changes so I can't just use a simple range. There are two possible ways to mark that the data pertains to a new client. In column 1 if the cell says null, it marks the start of new client data. Or, column 2 contains the name of the client, so if the cell in column b does not equal the cell directly above it also will mark the start of new client data.
The key point is that I need to select and cut all the data for each client and paste it into a newly opened workbook.
I've looked into a couple of ways to do this and am now researching for loops and while loops. can anyone suggest a possible structure to do this or a resource that might help?
Updated Code:
Sub copyStuff()
Dim rowStart As Integer
Dim rowEnd As Integer
Dim rowMax As Integer
Dim colMax As Integer
Dim bookName As String
Dim thisWB As String
thisWB = ThisWorkbook.Name
rowMax = ActiveSheet.UsedRange.Rows.Count + 1
colMax = ActiveSheet.UsedRange.Columns.Count
rowStart = 2
For x = 3 To rowMax
If Cells(x, 2) = Cells(x - 1, 2) Then
'
Else
rowEnd = x - 1
Range(Cells(rowStart, 1), Cells(rowEnd, colMax)).Copy
Set NewBook = Workbooks.Add
Range("A2").PasteSpecial (xlPasteValues)
bookName = Cells(rowStart, 2).Value
NewBook.SaveAs Filename:=bookName
Workbooks(thisWB).Activate
Range(Cells(1, 1), Cells(1, colMax)).Copy
Workbooks(bookName).Activate
Range("A1").PasteSpecial (xlPasteValues)
ActiveSheet.Name = "Daily Summary"
ActiveWorkbook.Save
Workbooks(thisWB).Activate
Worksheets("transaction details").Activate
If Cells(x, 2) = Cells(x - 1, 2) Then
'
Else
rowEnd = x - 1
Range(Cells(rowStart, 1), Cells(rowEnd, colMax)).Copy
NewBook.Activate
Range("A2").PasteSpecial (xlPasteValues)
Sheets.Add.Name = "Transaction Details"
Workbooks(thisWB).Worksheet("Transaction Details").Activate
Range(Cells(1, 1), Cells(1, colMax)).Copy
Workbooks(bookName).Activate
Range("A1").PasteSpecial (xlPasteValues)
End If
Workbooks(bookName).Activate
Worksheets("Daily Summary").Activate
Columns("B").Delete
Range("A1:O1").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A30:O30").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.Font.Bold = True
Range("C2:O29").Select
Range("C29").Activate
Selection.Style = "Currency"
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"
ActiveCell.Replace What:="Null", Replacement:="Total", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("D22").Select
Cells.Select
Cells.EntireColumn.AutoFit
ActiveWorkbook.Sheets.Add.Name = "Summary"
ActiveWorkbook.Worksheets("Summary").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = "Description"
Range("B1").Select
ActiveCell.FormulaR1C1 = "=EOMONTH(TODAY(),-2)+1"
Selection.NumberFormat = "m/d/yyyy"
Range("A1:B1").Select
Range("A1:B1").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("B1").Select
CellContentCanBeInterpretedAsADate = True
Range("A2").Select
ActiveCell.FormulaR1C1 = "Total Amex Charges"
Range("A3").Select
ActiveCell.FormulaR1C1 = "Total Visa Charges"
Range("A4").Select
ActiveCell.FormulaR1C1 = "Total MasterCard Charges"
Range("A5").Select
ActiveCell.FormulaR1C1 = "Total Discover Charges"
Range("A6").Select
ActiveCell.FormulaR1C1 = "Total Credit Card Charges"
Range("A6:B6").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Cells.Select
Cells.EntireColumn.AutoFit
Range("A8").Select
ActiveCell.FormulaR1C1 = "Amex Transaction Fee (.05/per)"
Range("A9").Select
ActiveCell.FormulaR1C1 = "MasterCard Card Fees"
Range("A10").Select
ActiveCell.FormulaR1C1 = "Visa Card Fees"
Range("A11").Select
ActiveCell.FormulaR1C1 = "Discover Fees"
Range("A12").Select
ActiveCell.FormulaR1C1 = "Total Card Fees"
Range("A12:B12").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Cells.Select
Cells.EntireColumn.AutoFit
Range("A14").Select
ActiveCell.FormulaR1C1 = "xx Management Fee"
Range("A15").Select
ActiveCell.FormulaR1C1 = "Total xx Fees"
Range("A15:B15").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A17").Select
ActiveCell.FormulaR1C1 = "Equipment Payment Fee"
Range("A18").Select
ActiveCell.FormulaR1C1 = "Total Equipment Fees"
Range("A18:B18").Select
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A20").Select
ActiveCell.FormulaR1C1 = "Total Visa, MasterCard, Discover Charges"
Range("A21").Select
ActiveCell.FormulaR1C1 = "Less: Total Fees"
Range("A22").Select
ActiveCell.FormulaR1C1 = "Total Amount Owed"
Range("A23").Select
ActiveCell.FormulaR1C1 = "Total ACH Payments"
Range("A24").Select
ActiveCell.FormulaR1C1 = "Overpaid (UnderPaid)"
Range("A24:B24").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A22:B22").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A20:B20").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
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
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A20:B20,A22:B22,A24:B24").Select
Range("A24").Activate
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("B2:B24").Select
Selection.Style = "Currency"
Range("B2").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 2).Value
Range("B3").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 4).Value
Range("B4").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 5).Value
Range("B5").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 3).Value
Range("B6").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 6).Value
Sheets("Daily Summary").Select
Columns("G:G").Select
Selection.Cut
Columns("O:O").Select
Selection.Insert Shift:=xlToRight
Sheets("Summary").Select
Range("B8").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 10).Value
Range("B9").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 7).Value
Range("B10").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 8).Value
Range("B11").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 9).Value
Range("B12").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 11).Value
Range("B14").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 12).Value
Range("B15").Select
ActiveCell.FormulaR1C1 = "=R[-1]C"
Range("B17").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 13).Value
Range("B18").Select
ActiveCell.FormulaR1C1 = "=R[-1]C"
Range("B20").Select
ActiveCell.FormulaR1C1 = "=R[-17]C+R[-16]C+R[-15]C"
Range("B21").Select
ActiveCell.FormulaR1C1 = "=R[-9]C+R[-6]C+R[-3]C"
Range("B22").Select
ActiveCell.FormulaR1C1 = "=R[-2]C-R[-1]C"
Range("B22").Select
ActiveCell.FormulaR1C1 = "=R[-2]C-R[-1]C"
Range("B21").Select
ActiveCell.FormulaR1C1 = "=R[-9]C+R[-6]C+R[-3]C"
Range("B23").Select
ActiveCell.FormulaR1C1 = Sheets("Daily Summary").Cells(rowEnd, 16).Value
Range("B24").Select
ActiveCell.FormulaR1C1 = "=R[-2]C-R[-1]C"
Range("B25").Select
ActiveWorkbook.Close
rowStart = x
Sheets("Data").Activate
End If
Next
End Sub

Here's what I think you're looking for. This will loop through (currently) column A and look for if the cell is the same as the one above it. If it is, it will skip to the next row and continue looking.
When it comes across a change in cells, it will then copy from the start of the range to the end and paste it into a new workbook. It currently names the book whatever the cell value is. So it will, in theory, name it the company name.
Sub copyStuff()
Dim rowStart As Integer
Dim rowEnd As Integer
Dim rowMax As Integer
Dim colMax As Integer
Dim bookName As String
Dim thisWB As String
thisWB = ThisWorkbook.Name
rowMax = ActiveSheet.UsedRange.Rows.Count + 1
colMax = ActiveSheet.UsedRange.Columns.Count
rowStart = 2
For x = 3 To rowMax
If Cells(x, 1) = Cells(x - 1, 1) Then
'
Else
rowEnd = x - 1
bookName = Cells(rowEnd, 1).Value
Range(Cells(rowStart, 1), Cells(rowEnd, colMax)).Copy
Set NewBook = Workbooks.Add
Range("A2").PasteSpecial (xlPasteValues)
NewBook.SaveAs Filename:=bookName
Workbooks(thisWB).Activate
Range(Cells(1, 1), Cells(1, colMax)).Copy
Workbooks(bookName).Activate
Range("A1").PasteSpecial (xlPasteValues)
ActiveWorkbook.Save
ActiveWorkbook.Close
rowStart = x
Sheets("Data").Activate
End If
Next
End Sub

Related

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

Select a range of cells, delete the empty ones, and then add border around remaining cells

I would like to use a macro that will look at a range of cells, delete out the empty rows, and then add a border around the remaining cells that actually has content. Here are two macros I have: One is for removing the empty cells, and the other is to add borders. As I mentioned, the issue is, I do not know how to tell Excel to only add a border around the cells that were left over after the Remove macro was completed. I would appreciate any help.
**Sub Remove()**
'
' Remove Macro
'
'
Range("B80:B95").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-12
Range("B61:B77").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-21
Range("B39:B58").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-27
Range("B10:B28").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-6
End Sub
and
**Sub Border()**
'
' Border Macro
'
'
Range("B7:K19").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
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=18
Range("B21:K74").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
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=54
Range("O76").Select
ActiveWindow.SmallScroll Down:=-81
End Sub
I assume your B to K columns will always remain constant. You basically just want to find the total "used" rows...
Dim cols As Integer, LastRow As Long, TestRow As Long
LastRow = 0
For cols = 2 to 11
TestRow = Cells(Rows.Count, cols).End(xlUp).Row
If TestRow > LastRow Then LastRow = TestRow
Next cols
Range("B7:K" & LastRow).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
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=18
Range("B21:K74").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
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=54
Range("O76").Select
ActiveWindow.SmallScroll Down:=-81
I really hate the use of .Select but I am not rewriting all your other code.

Repeat the same calculation in multiple sheets

Task: Repeat an identical calculation in multiple sheets.
Background:
multiple sheets labelled by calendar date i.e. 01 04, 02 04, 03 04. These are three discrete sheet names meaning 1st April, 2nd April and 3 April. (actual workbook has all the days in the month).
Data has identical column headings, but the number of rows vary. In brief the data is a list of mastercard and visa transactions.
I want to get the total of column G (happens to contain the monetary transaction value) and only take the Visa transactions.
Result:
the code below does this fine and places the results on the same sheet merely offset by a few columns to the right hand side and highlights the value I need in red. (this is a recorded macro I completed)
Limitation and seeking advise:
1) improve code to repeat this for all sheets by a single click of a mouse button.
(as you will note, its about how to cycle through all the sheets within the same workbook rather than (at present) having to manually go into each sheet and run the macro.
thank you in advance
code is:
Sub sum_visa_trans_together()
'
' sum_visa_trans_together Macro
'
' Keyboard Shortcut: Ctrl+r
'
ActiveCell.Rows("1:1").EntireRow.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$M$14").AutoFilter Field:=2, Criteria1:="V"
ActiveCell.Offset(0, 6).Columns("A:A").EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 4).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveCell.Offset(0, 11).Range("A1").Select
ActiveCell.FormulaR1C1 = "max"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=MAX(C[-1])"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=SUM(C[-1])"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "visa trans"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-2]C-R[-3]C"
ActiveCell.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
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
End Sub
this wont repeat in the sheets you want because you are using active cell, you can replace active cell with something like this:
sheetname.cells(1,1).value
in this case you are geting the value of cell A1 wich is row=1,column=1 in the sheet named sheetname
the name of your sheet is not necesary the same in vba so chek your narmes in the vba project explorer.
for example you can try something like this(Im not sure exactly what you are trying to do but this will guide you):
Sub s()
For Each ws In Worksheets 'WS will loop trough all worksheets
Dim TargetCell As Range
Set TargetCell = ws.Cells(1, 2) ' in this case you will run this macro in
' the cell A2 of all your sheets
TargetCell.Rows("1:1").EntireRow.Select
Selection.AutoFilter
ws.Range("$A$1:$M$14").AutoFilter Field:=2, Criteria1:="V"
TargetCell.Offset(0, 6).Columns("A:A").EntireColumn.Select
Selection.Copy
TargetCell.Offset(0, 4).Range("A1").Select
ws.Paste
TargetCell.Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.AutoFilter
TargetCell.Offset(0, 11).Range("A1").Select
TargetCell.FormulaR1C1 = "max"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=MAX(C[-1])"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=SUM(C[-1])"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "visa trans"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=R[-2]C-R[-3]C"
TargetCell.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
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
Next
End Sub
Otherwise:
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
' Insert your code here.
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub
source: http://support.microsoft.com/kb/142126/en

Excel vba count headers and copy active row of same length to new sheet

I'm new to vba and struggling with a macro.
I have recorded a macro and then tried to adapt it.
What I have is a list of drivers as headers currently c1:t1 but as I add or remove drivers I need the selection below to adapt.
B2 is a merged cell (B2:B5) with the date in and the columns across are still individual cells.
The date is repeated all the way down in the same format for each day of the year.
What i'm trying to do is select a date and press ctrl+q and copy the list of drivers names across the headers to a new sheet in column A and the selected date and the number of columns to match the number of drivers in the header.
Sub Macro6()
'
' Macro6 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Selection.Copy
Sheets("Daily").Select
Range("C4:F4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
Application.CutCopyMode = False
With Selection.Font
.Name = "Arial"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
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
Sheets("Weekly").Select
Range("c1", Range("CV1").End(xlToLeft)).Select
Selection.Copy
Sheets("Daily").Select
Range("A5").Select
ActiveWindow.SmallScroll Down:=-27
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
Sheets("Weekly").Select
Application.CutCopyMode = False
Sheets("Daily").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.ClearComments
Sheets("Weekly").Select
Application.CutCopyMode = False
End Sub
Dim lCol As Long, cpycel As Range
Set cpycel = Range(ActiveCell.Address)
lCol = (Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column) - 1
cpycel.Resize(4, lCol).Select
Selection.Copy
Sheets("Daily").Select
Range("C4:F4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
Sheets("Weekly").Select
Range(Cells(1, 2), Cells(1, (lCol + 1))).Select
Selection.Copy
Sheets("Daily").Select
Range("a4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
Range(Cells(5, 1), Cells((lCol + 3), 6)).Select