Vba 自动填充到特定日期
我正在处理日期数据,希望自动填充到特定日期。具体日期将存储为值Vba 自动填充到特定日期,vba,date,autofill,Vba,Date,Autofill,我正在处理日期数据,希望自动填充到特定日期。具体日期将存储为值XX是列A中的最后一个值 我已经看过了其他一些关于StackOverflow的帖子,但我一直遇到的问题是设置我的最终“值”。我的最终值不是一个数字,而是一个停止的日期 With ThisWorkbook.Sheets("Form Date 2") 'Select this workbook Dim X as Date 'I am not sure what I need to Dim this as
X
X
是列A中的最后一个值
我已经看过了其他一些关于StackOverflow的帖子,但我一直遇到的问题是设置我的最终“值”。我的最终值不是一个数字,而是一个停止的日期
With ThisWorkbook.Sheets("Form Date 2")
'Select this workbook
Dim X as Date
'I am not sure what I need to Dim this as
Range("B2").Value = Range("A2").Value
'B2 is the value I want to autofill down with
X = Range("A" & Rows.Count).End(xlUp).Value
'X will be the last value in column A
Range("B2").Autofill Destination:=Range("B2:B" & X), Type:=xlFillSeries
'Not sure what to set my Type as
End With
因此,最终结果将是,B列
将获得从A1
到A
(2018年4月2日-2018年6月5日)
中最后一个值的所有日期。格式应为dd/mm/yyyy
在A列中
我有一大堆日期,但有些日期不见了,所以我也要抓住所有不见的日期
下面的图片显示了日期栏。我想让VBA吐出所有日期
列。因此,代码将复制并粘贴A2
到B2
,然后自动填充,直到列A
中的最后一个值(不是行)。因此在本例中,所有日期
应继续向下,直到value/date2018年6月1日
也许这会奏效:
With ThisWorkbook.Sheets("Sheet2")
'Select this workbook
Dim X As Integer
'I am not sure what I need to Dim this as
.Range("B2").Value = .Range("A2").Value
'B2 is the value I want to autofill down with
X = .Range("A" & .Rows.Count).End(xlUp).Row
'X will be the last value in column A
.Range("B2").Select
Selection.AutoFill Destination:=.Range("B2:B" & X), Type:=xlFillDefault
'Not sure what to set my Type as
End With
更新代码以填充到A列最后一个单元格的日期:
With ThisWorkbook.Sheets("Sheet2")
'Select this workbook
Dim X As Integer
'I am not sure what I need to Dim this as
.Range("B2").Value = .Range("A2").Value
'B2 is the value I want to autofill down with
X = .Range("A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value - .Range("B2").Value
'X will be the last value in column A
.Range("B2").Select
Selection.AutoFill Destination:=.Range("B2:B" & X + 2), Type:=xlFillDefault
'Not sure what to set my Type as
End With
此代码工作正常:
Sub test()
Dim X As Integer
ThisWorkbook.Sheets("Sheet2").Range("B2").Value = Range("A2").Value
'B2 is the value I want to autofill down with
X = ThisWorkbook.Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
'X will be the last value in column A
ThisWorkbook.Sheets("Sheet2").Range("B2").AutoFill Destination:=ThisWorkbook.Sheets("Sheet2").Range("B2:B" & X), Type:=xlFillDefault
End Sub
不客气:)您能举个例子说明您希望如何查看日期吗?你面临什么错误?好的,我编辑了我的帖子。@Mikku你能解释一下+2
?是的,我们说的是B2:B,这就是为什么我们必须添加2。实际上,现在计算的X是两个日期的差值,与行结构无关。但是如果去掉-.Range(“B2”).Value
。您还需要+2
吗?您可以试试。把B2换成B1。这实际上取决于我们得到的值。尝试使用F8
检查代码。并在每个步骤中验证值。那会让你明白的。