我正在寻找一种方法来编写VBA脚本,该脚本将自动填充sheet17.range(“f14:aj14”),其中包含当前月份的日期。日期格式需要为mm / d / yyyy,我需要能够使用“FoundCell”方法使用此范围查找“Date”。我找到了这个代码,并根据我的需要进行了调整,但是从下一行的单元格开始填充右边的下一列。我已经尝试更改“偏移”数字并且不能让它填充我的单元格范围,因为我是VBA的新手,我有一个宏将表单复制到新表格,保存新表格然后清除内容范围(f15:AN73)原版,我不知道在每个月的第一天凌晨12:00自动发生的代码。以下是我对日期人口的代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Set Sht = Worksheets("MAR") 'change to suit
Target = Sht.Range("f14:aj14")
intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))
Target.Resize(intDaysInMonth, 1).ClearContents
For i = 1 To intDaysInMonth
Target.Offset(i - 0, 1) = DateSerial(Year(Now()), Month(Now()), i)
Next i
Set Sht = Nothing
End Sub
我只需要日期填充我的sheet17.range(“f14:aj14”)而不是从1单元格开始向右的下一列,然后是自动将工作表保存到新工作表并清除内容的代码原始sheet.range(“f15:an73”)。任何帮助将不胜感激!提前谢谢。
米歇尔
答案 0 :(得分:0)
以下是问题日期填写部分的答案。而且,您需要将其放在常规模块中,而不是放在工作表模块中。
Sub myDates()
Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Dim Target As Range
Set Sht = Worksheets("MAR") 'change to suit
intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))
Set Target = Sht.Range("f14").Resize(, intDaysInMonth) 'set target to days count
Target(1).Resize(, 31).ClearContents 'clear all previous
For i = 1 To intDaysInMonth
Target(i).Value = DateSerial(Year(Now()), Month(Now()), i)
Next i
Set Sht = Nothing
End Sub
但是为了使它更有用,你应该从另一个sub调用它,并将工作表名称作为参数,这样你就可以访问正确的月份工作表。然后它看起来像这样:
Sub myDates(ShtName As String)
Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Dim Target As Range
Set Sht = Worksheets(ShtName) 'change to suit
intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))
Set Target = Sht.Range("f14").Resize(, intDaysInMonth) 'set target to days count
Target(1).Resize(, 31).ClearContents 'clear all previous
For i = 1 To intDaysInMonth
Target(i).Value = DateSerial(Year(Now()), Month(Now()), i)
Next i
Set Sht = Nothing
End Sub