如何在一个范围内复制excel文件中的单元格格式?

如何在一个范围内复制excel文件中的单元格格式?,excel,vba,Excel,Vba,我有这张excel表格(上面链接的图片),我需要每月更新一次,正如你们所看到的,每个月都有一个新的列和新的数字。在花了一段时间试图掌握vba并四处询问之后,我感激地得到了以下代码: Sub Increment_Month() Dim lngLastCol As Long, lngRow As Long lngRow = ActiveCell.Row lngLastCol = Cells(lngRow, Columns.Count).End(xlToLeft).Col

我有这张excel表格(上面链接的图片),我需要每月更新一次,正如你们所看到的,每个月都有一个新的列和新的数字。在花了一段时间试图掌握vba并四处询问之后,我感激地得到了以下代码:

Sub Increment_Month()


    Dim lngLastCol As Long, lngRow As Long

    lngRow = ActiveCell.Row
    lngLastCol = Cells(lngRow, Columns.Count).End(xlToLeft).Column

    If IsDate(Cells(lngRow, lngLastCol)) Then
        With Union(Cells(3, lngLastCol + 1), Cells(17, lngLastCol + 1), Cells(32, lngLastCol + 1))
            .Value = DateAdd("M", 1, CDate(Cells(lngRow, lngLastCol)))
            .NumberFormat = Cells(lngRow, lngLastCol).NumberFormat
        End With
    End If

End Sub
这会增加我的月份数,这样我可以点击一个按钮,它将下一个月作为标题。现在,我一直在研究如何将前几个月的格式粘贴到下个月,即使用一个宏更新格式,而不必复制每个月的格式(边框、颜色标题等),但我得到的宏只覆盖了上个月的数据,而没有执行一个月

如果这不合理,很抱歉(请让我澄清一下)


谢谢你的帮助

VBA允许您使用以下语法更改单元格的背景色。 下面的代码行允许您将单元格A1和A10的颜色设置为深蓝色

Range("A1,A10").Cells.Interior.Color = RGB(0, 0, 125)

您可以使用类似的语法获取范围并设置背景色。

下面的代码将把最后一列复制到右侧并清除内容。然后,它将在前一个日期的基础上再加上一个月,并复制您的求和公式(如果您有求和公式)。我只在第7行循环介绍了您的第一组数据,您可以根据需要进行更改

Dim lCol As Long
lCol = Cells(3, Columns.Count).End(xlToLeft).Column

Columns(lCol).Copy
Columns(lCol + 1).Insert Shift:=xlToRight
Columns(lCol + 1).ClearContents

    For Each cell In Range(Cells(3, lCol), Cells(7, lCol))
        If IsDate(cell.Value) Then
            cell.Offset(, 1).Value = DateAdd("m", 1, cell.Value)
        End If

        If cell.HasFormula = True Then
            cell.Copy
            cell.Offset(, 1).PasteSpecial Paste:=xlPasteFormulas
        End If
    Next cell

Application.CutCopyMode = False

仅仅获取格式一并将其应用于整个工作表宽度是否不够?你在那里只能呆上几个月。是的,我想会的,但这只会让它尽可能整洁。谢谢你的建议和编辑,我很感激!你可以不复制它,而是用一个按钮从头开始插入它。这两种方法都有效。我以前从未使用过VBA,所以也不知道如何使用。只是碰巧点击了这个问题。