Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel VBA中插入并拖动公式直到最后一行_Vba_Excel_Excel 2010 - Fatal编程技术网

如何在Excel VBA中插入并拖动公式直到最后一行

如何在Excel VBA中插入并拖动公式直到最后一行,vba,excel,excel-2010,Vba,Excel,Excel 2010,我想在E行末尾插入仅计算每个月第一天(2017年11月1日、2017年12月1日等)的公式,并将其拖到D行值的末尾。我使用了下面的代码,但不起作用 仅当A:D有数据时,我需要2017年11月1日“E12:E21”中的值。但A:D将自动计算。下个月A22:D24将包含数据。所以我需要“E22:E24”中的值作为01。2017年12月。帮帮我 Private Sub CommandButton1_Click() Range("E" & Rows.Count).End(xlUp).Offse

我想在E行末尾插入仅计算每个月第一天(2017年11月1日、2017年12月1日等)的公式,并将其拖到D行值的末尾。我使用了下面的代码,但不起作用

仅当A:D有数据时,我需要2017年11月1日“E12:E21”中的值。但A:D将自动计算。下个月A22:D24将包含数据。所以我需要“E22:E24”中的值作为01。2017年12月。帮帮我

Private Sub CommandButton1_Click()

Range("E" & Rows.Count).End(xlUp).Offset(1, 0).Select
Run FirstDayInMonth()
Selection.AutoFill Destination:=Range("D" & Column.count).End(xlUp).Offset(0, 1), Type:=xlFillCopy

End Sub

Function FirstDayInMonth(Optional dtmDate As Date = 0) As Date
Range("E" & Rows.Count).End(xlUp).Offset(1, 0).Select
    If dtmDate = 0 Then
       dtmDate = Date
    End If
FirstDayInMonth = DateSerial(Year(dtmDate), _
Month(dtmDate), 1)
End Function

首先,您过度使用Select。如果希望宏指向末尾的某个单元格,则在代码中只能使用一种情况
其次,避免智能UI反模式。什么是智能用户界面:

第三,我认为应该使用sub,而不是函数

Sub FillFirstDay(Optional dtmDate As Double = 1)

Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long, firstRow As Long

Set ws = ActiveSheet 'You should assign your sheet here, for example by name

'Then we find our range in E column
With ws
    lastRow = .Range("D" & .Rows.Count).End(xlUp).Row
    firstRow = .Range("E" & .Rows.Count).End(xlUp).Row + 1
    Set rng = Range(.Range("E" & firstRow), .Range("E" & lastRow))
End With

If firstRow >= lastRow Then Exit Sub

'And finally add first date of month
With rng
    .Value = DateSerial(Year(dtmDate), Month(dtmDate), 1)
    .NumberFormat = "yyyy-mm-dd" 'or whatever date format do you like
End With

End Sub

如果firstRow>=lastRow,则Exit Sub行在列E中的日期已填充时终止该过程

也许我遗漏了一个原因,那就是这必须以复杂的方式完成,但您不能只使用工作表函数吗

是否希望列E在有数据的行中显示日期(月的第一天)

直接或通过编程(使用WorksheetFunction)将其放入单元格E2(或列E的所有部分,如果需要):


修正公式:

我使用了下面的代码,但不起作用
。。。告诉我,这句话的哪一部分提供了关于你遇到的问题的很好的信息?我也不清楚你需要什么。您想在D列中进行计算,其中C列中的同一行上有数据?是否有理由必须使用VBA执行此操作?@jsotola For every month列a到D将自动更新,而不是E列。我需要添加2017年11月1日和下个月2017年12月1日,以此类推。它必须在E行上。i、 e“E12”直到结束。从下个月的“E22”到最后。当解析为
range($E$20”)。value=DateSerial(1899,12,1)
时,我得到了一个应用程序定义的错误,默认值为代码中的默认值。这是一个有趣的问题,值得提出另一个问题。不管怎样,如果可选参数是2,一切都应该正常。@MarcinSzaleniec-这就是你所说的1。。。两个可能是两倍糟糕。:-)现在我上传了我的新图片。请检查输出。设置rng时只需添加偏移量:设置rng=Range(.Range(“E”和.Rows.Count)。结束(xlUp)。每个月的偏移量(1),.Range(“E”和.lastRow))列A到D将自动更新,但不更新E列。我需要添加2017年11月1日和下个月2017年12月1日,以此类推。它必须在E行上。i、 e“E12”直到结束。从下个月到下个月结束,好的。您希望在所有4个单元格(A:D)都有数据后填充E列,还是只有一列重要?是的,仅当A:D有数据时,我需要2017年11月1日“E12:E21”中的值。但A:D将自动计算。下个月A22:D24将包含数据。所以我需要“E22:E24”中的值作为01。2017年12月。这就是为什么发布样本数据(代表实际数据)很有帮助,包括您现在拥有的数据和您需要拥有的数据。无论如何,通过上面的公式,您可以将日期输入到本月有数据的单元格中。如果您想在月末“锁定值”,可以在E中包含数据的单元格上复制一行或两行代码。在和查看提示可能是个好主意。祝你好运!@ashleedawg我在E12:E22单元中说,我只需要2017年11月1日的值。不包括2017年11月1日、2017年12月1日。我不需要excel公式。因为我的原始数据包含超过400000行。
=IF(D2="","",DATE(YEAR(NOW()),MONTH(NOW()),1))