我有这个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这增加了我的月份,所以我可以点击一个按钮,它有下一个月作为标题。现在,我一直在讨论如何粘贴前几个月的格式,即有一个宏更新该格式,而不必复制每个月的格式(边框、颜色标头等),但我得到的宏只是覆盖了最后几个月的数据,而没有进行一个月的操作。
对不起,如果这没有道理(请让我澄清)。
谢谢你的帮助!
发布于 2018-11-05 04:07:09
下面的代码将把最后一列复制到右边,并清除内容。然后,它将添加一个月的前一个日期,并复制您的和公式(如果您有一个和公式)。我只循环通过第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发布于 2018-11-02 16:46:27
VBA允许您使用以下语法更改单元格的背景色。下面的代码行允许您将单元格A1和A10的颜色设置为深蓝色。
Range("A1,A10").Cells.Interior.Color = RGB(0, 0, 125)您可以使用类似的语法获取范围和设置背景色。
https://stackoverflow.com/questions/53121002
复制相似问题