自动填充到特定日期

Autofill to a specific date

我正在处理日期数据并希望自动填充到特定日期。具体日期将存储为值XXA

列中的最后一个值

我在 Whosebug 上查看了其他一些帖子,但我一直 运行 关注的问题是设置我的最终 "value"。我的最终值不是数字,而是停止的日期。

    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 列将获取从 A1A (02/04/2018-05/06/2018) 中最后一个值的所有日期。格式应为 dd/mm/yyyy.

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

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

也许这会奏效:

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

不客气:)