简体   繁体   English

使用VBA将详细的Excel内置函数传递给单元格

[英]Pass verbose Excel's in-built function to a Cell using VBA

I'm having trouble in passing a built-in Excel function to a cell. 我在将内置Excel函数传递到单元格时遇到麻烦。 It keeps returning "Run-time error '1004': Application-defined or object-defined error". 它不断返回"Run-time error '1004': Application-defined or object-defined error".

Basically what I'm trying to do is create a dynamic calculus of average/mean using VBA. 基本上,我想做的是使用VBA创建平均/均值的动态演算。

With Sheets("TC7_DATA")
    LastColumn = .Cells(4, Columns.Count).End(xlToLeft).Column
    For i = 1 To Rows.Count
        If (.Cells(i, 3).Value = "Average Value") Then
            .Cells(i, LastColumn).Value = "=IF(AND(.Cells(i-3; LastColumn)=0; .Cells(i-2; LastColumn)=0; .Cells(i-1; LastColumn)=0);" - - - "; AVERAGE(.Cells(i-3; LastColumn), .Cells(i-3; LastColumn)))"
        End If
    Next i
End With

If I insert the value without the "=" in the front of the function, it interprets as text and nothing goes wrong (except that the function don't actually works, of course). 如果我在函数的前面插入不带"="的值,它将解释为文本,并且不会出错(当然,该函数实际上不起作用)。 I tried to add "=" in the front of the cell without using VBA and the function works correctly. 我尝试在不使用VBA的情况下在单元格的前面添加"=" ,并且该功能正常运行。 If I concatenate USING VBA 如果我连接使用VBA

.Cells(i, LastColumn).Value = "=" & .Cells(i, LastColumn).Value

The error returns. 错误返回。

How can I pass a VERBOSE Excel in-built function: 如何传递VERBOSE Excel内置函数:

("=IF(2>3; "True"; "False")") 

to a Cell using VBA? 使用VBA到单元格?

Use .Formula instead. 使用.Formula代替。

Few other points: 其他几点:

  • You don't need spaces in your If statement like some other languages unless you want to group statements together 您不需要像其他语言一样在If语句中使用空格,除非您希望将语句分组在一起

  • Your For loop doesn't explicitly define which sheet it should be 您的For循环未明确定义应为哪张纸
    counting the rows on. 计算行数。 Use .Rows.Count instead to specify the sheet Sheets("TC7_DATA") or declare another sheet 使用.Rows.Count代替来指定工作表Sheets("TC7_DATA")或声明另一个工作表

  • Your For loop also will loop through every row in the sheet 您的For循环还将遍历工作表中的每一行
    (possibly all 1,048,576 of them). (可能是所有1,048,576个)。 This is incredibly verbose. 这是非常冗长的。 I'd ID
    recommend just looping through the ones you actually need instead. 建议您只遍历实际需要的内容。
    You already get the LastColumn why not get the LastRow as well 您已经获得了LastColumn为什么不也获得LastRow
    using something like LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row and use this value in your for loop instead 使用类似LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row并在您的for循环中使用此值


With Sheets("TC7_DATA")
    LastColumn = .Cells(4, Columns.Count).End(xlToLeft).Column
    For i = 1 To Rows.Count
        If .Cells(i, 3).Value = "Average Value" Then
            .Cells(i, LastColumn).Formula = "=IF(AND(" & .Cells(i - 3, LastColumn) & "=0; " & .Cells(i - 2, LastColumn) & "=0; " & .Cells(i - 1, LastColumn) & "=0);"" - --""; AVERAGE(" & .Cells(i - 3, LastColumn) & ", " & .Cells(i - 3, LastColumn) & "))"
        End If
    Next i
End With

You have to write to the Formula property, not the Value property. 您必须写入Formula属性,而不是Value属性。

Example: 例:

Sheets("CTC7_DATA").[A11].Formula ="=IF(2>3; "True"; "False")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM