【问题标题】:Autofill to a specific date自动填充到特定日期
【发布时间】:2019-10-19 17:05:05
【问题描述】:

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

我在 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

【问题讨论】:

  • 您能否举例说明您希望如何查看日期?你面临什么错误?
  • 好的,我编辑了我的帖子。

标签: vba date autofill


【解决方案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

【讨论】:

  • @Mikku 你能解释一下+2吗?
  • 是的,我们说的是 B2:B ,这就是为什么我们必须加 2。实际上现在计算的 X 是 2 个日期的差,与行结构无关。
  • 但是如果你拿走了-.Range("B2").Value。你还需要+2 吗?
  • 你可以试试。将 B2 更改为 B1。这实际上取决于我们获得的价值。尝试使用F8 浏览代码。并在每一步验证值。这会让你清楚。
【解决方案2】:

这段代码运行良好:

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

不客气:)

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2014-02-09
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多