将表格从工作表复制到工作表,保留字体颜色

时间:2013-07-26 21:04:16

标签: excel-vba vba excel

我正在尝试从同一个工作簿中的多个其他工作表构建一个主工作表。信息在表格中格式化,需要在主表格中保持这种状态。主和其他选项卡都基于模板表。我可以成功构建主工作表,但表格格式会覆盖复制选项卡中的任何字体颜色。以下是我的代码。有关获取字体颜色以成功复制到主表格的任何想法,同时保留表格吗?

Sub Combine()
    Dim i As Integer
    Dim j As Integer
    Dim ws1 As Worksheet
    'Adding multiple String variables for 5 accounts
    Dim account1 As String
    Dim account2 As String
    Dim account3 As String
    Dim account4 As String
    Dim account5 As String

'Naming which account worksheets need to be copied into the Master worksheet, only for use with Naming Method below
account1 = "x"
account2 = "xx"
account3 = "xxx"
account4 = "xxxx"
account5 = "xxxxx"

'Deleting any previous Master worksheet
For Each SheetExists In Worksheets
    If SheetExists.Name = "Master" Then
        Application.DisplayAlerts = False 'Turn off alerts to avoid annoyance
        SheetExists.Delete
        Application.DisplayAlerts = True 'Turn alerts back on
        Exit For
    End If
Next SheetExists

'Creating a blank Master worksheet based upon the Template worksheet
Set ws1 = ThisWorkbook.Worksheets("Template")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = "Master"
ActiveSheet.Move before:=Worksheets(1) 'Moves Master worksheet to first tab

'Naming Method, selecting specific worksheets based upon names, for each additional worksheet name add another Or statement to the If Then line
For Each SheetExists In Worksheets
    If SheetExists.Name = account1 Or SheetExists.Name = account2 Or SheetExists.Name = account3 Or SheetExists.Name = account4 Or SheetExists.Name = account5 Then 'Matching worksheet names as needed
        SheetExists.Activate 'Make the sheet active
        Range("A1").Select
        Selection.CurrentRegion.Select 'Select all cells in this sheet
        Selection.Offset(2, 0).Resize(Selection.Rows.Count - 3).Select 'Select all rows except top and header
        Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)  'Copy cells selected into the Master sheet on the last line available
        Range("A1").Select 'Deselects entire worksheet so everything isn't highlighted
    End If
Next SheetExists

'Activates Master worksheet
Sheets(1).Activate
Rows("3:4").Delete 'Deletes blank lines from template

End Sub

1 个答案:

答案 0 :(得分:0)

字体颜色是可以通过Font.Color属性访问的属性。也许你可以加入像

这样简单的东西
savedColor = Cells(rowInd, colInd).Font.Color

保存单元格的字体颜色,然后使用该保存的信息在主表格上设置字体的颜色。