自动填充到特定日期

时间:2019-06-04 07:29:35

标签: 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

    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列。因此,代码会将A2复制并粘贴到B2上,然后自动填充,直到A列中的最后一个值(非行)为止。因此,在此示例中,All Dates应该继续向下直到值/日期01/06/2018

enter image description here

2 个答案:

答案 0 :(得分:1)

也许这行得通:

with open('input.csv') as fdin, open('output.csv', 'w', newline='') as fdout:
    rd = csv.DictReader(fdin)
    fields = list(rd.fieldnames)
    fields.insert(2, 'new')
    wr = csv.DictWriter(fdout, fieldnames=fields)
    wr.writeheader()
    for row in rd:
        row['new'] = compute_val(row)    # or compute_val(*row)
        wr.writerow(row)

更新后的代码以填充到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" & .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

答案 1 :(得分:0)

此代码可以正常工作:

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

不客气:)