下面是我參考以前有發文過的資料自己試著改寫,但真的很菜,寫完都沒有反應
請問大家可否指導一下那邊有問題!!
Sub 本月貼上月公式()
Dim xArea As Range, xColumn As Range, M1%, M2%
Set xArea = [B2:M8] '設定範圍
M1 = Month(Date) '取月份
For Each xColumn In xArea.Columns
M2 = Val(xColumn.Cells(1, 0))
If M1 - M2 = 1 Then
If xColumn(2).HasFormula Then Exit Sub
If xColumn.HasFormula Then
xColumn.Copy xColumn(2)
Else
MsgBox xColumn.Cells(1, 0) & "無公式或公式不齊全!! ": Exit Sub '同一列儲存格都有公式才會複製
End If
End If
If M2 < M1 Then xColumn = xColumn.Value
不如保留1月份公式, 當月複製1月份公式, 並將前一個月至2月份貼成值~~
Sub 複製公式()
Dim xArea As Range, M%
Set xArea = [B2:M8]
M = Month(Date)
If M = 1 Then Exit Sub
xArea.Columns(1).Copy xArea.Columns(M)
If M <= 2 Then Exit Sub
With xArea.Columns(2).Resize(, M - 2): .Value = .Value: End With
End Sub