Ton*_*nyP 0 excel vba excel-vba
我有一列数据(C),其中包含许多包含日期的单元格.我正在尝试创建一个宏来检查每个单元格是否包含日期,如果确实如此,则按月推进日期.我有代码从这里提前一个月推进日期http://excel.tips.net/T002180_Automatically_Advancing_by_a_Month.html并且它工作正常但我不知道如何用动态范围替换范围来评估C列中的所有单元格如果可能的话,我也想消除循环的需要.这是我到目前为止所拥有的.
Sub IncreaseMonth()
Dim dDate As Date
Dim NumberofTasks As Integer
NumberofTasks = ThisWorkbook.Worksheets("Dashboard").Range("Number_of_Tasks")
Dim x As Integer
For x = 1 To NumberofTasks
dDate = Range("C30").Value
Range("C30").Value = _
DateSerial(Year(dDate), _
Month(dDate) + 1, Day(dDate))
Next x
End Sub
Run Code Online (Sandbox Code Playgroud)
尝试类似下面的代码(我使用DateAdd函数将1个月添加到当前日期值)
Sub IncreaseMonth()
Dim dDate As Date
Dim NumberofTasks As Long
Dim x As Long
With Worksheets("Dashboard")
' I suspect you want to get the last row with data in Column C
NumberofTasks = .Cells(.Rows.Count, "C").End(xlUp).Row
For x = 1 To NumberofTasks
If IsDate(.Range("C" & x).Value) Then '<-- check if current cell at Column C is Date
.Range("C" & x).Value = DateAdd("m", 1, .Range("C" & x).Value) '<-- add 1 Month to current date in Column c, use DateAdd function
End If
Next x
End With
End Sub
Run Code Online (Sandbox Code Playgroud)