使用变量作为单元格引用的 VBA Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16959309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:30:13  来源:igfitidea点击:

VBA Excel using variables as cell reference

excelvbaexcel-vba

提问by Tino

I have the following VBA Code:

我有以下 VBA 代码:

Public maxnumrows As Integer

Sub count_num_rows()
    maxnumrows = Sheets("Monetary All").UsedRange.Rows.Count
End Sub

Sub calc_external_sales()
    Sheets("Monetary All").[C5].FormulaLocal = "=SUMMEWENNS(Rawdata!K2:K3446;Rawdata!I2:I3446;""bezahlt"")"
End Sub

I would like the cell references in the calc_external_sales() to use the public variable in the range.

我希望 calc_external_sales() 中的单元格引用使用范围内的公共变量。

Example: Rawdata!K2:K"maxnumrows"

示例:Rawdata!K2:K"maxnumrows"

How do I have to change the syntax?

我该如何更改语法?

采纳答案by Tino

This is how you can use a variable within a string

这是在字符串中使用变量的方法

Sheets("Monetary All").[C5].FormulaLocal = _ 
"=SUMMEWENNS(Rawdata!K2:K" & maxnumrows & ";" & _ 
            "Rawdata!I2:I" & maxnumrows & ";""bezahlt"")"