通过修改VBA代码将标题行元素拆分为一个在另一个下面的单独行

时间:2016-06-19 07:00:14

标签: excel vba excel-vba

当前输出1 预期产出2 每当我更改代码时,标题下面的数据都会被覆盖。该宏返回4个excel选项卡。 代码在这里:

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

在运行此宏之后,接下来的4个选项卡有一个类似的代码作为输出生成。我希望所有标签都有相同的结果。如果我得到这个代码的答案,那么我可以类似地调整其他代码。 谢谢

1 个答案:

答案 0 :(得分:0)

所需的更改非常简单,但说实话,代码需要完全重写,并且有很多内容,以至于任何人都不可能为你做这些。

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