首页 > 解决方案 > 自动填充到特定日期

问题描述

我正在使用日期数据并希望自动填充到特定日期。具体日期将存储为 value 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

    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

所以最终的结果是, ColumnB将获得从A1到最后一个值的所有日期A (02/04/2018-05/06/2018)。格式应为 dd/mm/yyyy。

在 ColumnA我有一大堆日期,但有些丢失了,所以我也抓住了所有丢失的日期。

有了下面的图片,我就有了Dates专栏。我希望 VBA 吐出该All Dates列。因此,代码将复制并粘贴A2B2然后自动填充,直到列A(不是行)中的最后一个值。所以在这个例子中All Dates应该继续向下直到 value/date 01/06/2018

在此处输入图像描述

标签: vbadateautofill

解决方案


也许这会起作用:

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

推荐阅读