简体   繁体   English

Excel VBA-语法错误

[英]excel vba - syntax error

I can't quite figure out what the syntax error in Excel VBA is, the error I am seeing is: "run-time error 1004: Application-defined or object-defined error" 我不太清楚Excel VBA中的语法错误是什么,我看到的错误是:“运行时错误1004:应用程序定义或对象定义的错误”

Sheets("Totals").Select
x = Range("A139").Activate
Range("E139:AB166").Formula = "=INDEX(""Model""!$A$3:$Z$1000,MATCH($" & x & ",""Model""!$A$3:$A$1000,0),MATCH(E$3,""Model""!$A$3:$Z$3,0))"

I am attempting to use an Index Match Match formula where the index reference is another worksheet in the file. 我正在尝试使用索引匹配匹配公式,其中索引引用是文件中的另一个工作表。 I am trying to populate the formula in Range E139:AB166, such that the x variable is locked to Column A and the last match function is locked on the third row (E$3). 我正在尝试填充范围E139:AB166中的公式,以使x变量被锁定到列A,而最后一个匹配函数被锁定在第三行(E $ 3)。

x = Range("A139").Activate

This line of code should force the cursor to that cell in the 'model' worksheet. 这行代码应将光标强制移至“模型”工作表中的该单元格。 'Model' is the name of the worksheet in the file. “模型”是文件中工作表的名称。 I thought "" were necessary to alert vba that the string refers to the worksheet name. 我认为必须使用“”来提醒vba该字符串引用了工作表名称。 What if the worksheet name is comprised of two strings so "Model 1". 如果工作表名称由两个字符串组成,即“模型1”,该怎么办。 What would be then the syntax, this? 那么语法是什么?

""Model 1""!A3

'x' is the cell where new data is added, I wrote a line of code to have it dynamically changed. “ x”是添加新数据的单元格,我编写了一行代码来对其进行动态更改。

x = Range("A" & insert_at).Activate

Where insert_at is a variable that equals the last row,in the worksheet that is not empty, + 1. 其中insert_at是等于工作表中最后一行的变量,该工作表不为空,+ 1。

insert_at = lastRow + 1

So the idea is to dynamically add additional data from the 'Model' worksheet to the current worksheet 'Totals' below any existing data in the 'Totals worksheet. 因此,想法是将“模型”工作表中的其他数据动态添加到“总计”工作表中任何现有数据下方的当前工作表“总计”中。

I appreciate any assistance with this. 感谢您的协助。

Thanks! 谢谢!

Type a correct working formula in E139 then use this little Sub I wrote once, to turn it into a vba usable formula string. 在E139中键入正确的工作公式,然后使用我曾经写过的这个小Sub ,将其变成vba可用的公式字符串。

 Sub RngToVba(src As Range)  
 'writes the VBA code to re-create the formulae in given range  
 'by Patrick Honorez - www.idevlop.com  
 'usage: from debug window, type RngToVba [L14:R14]  
 ' or RngToVba range("L14:R14")  
  Dim c As Range  
  For Each c In src  
  Debug.Print "range(""" & c.Address & """).formula = """ & _  
        Replace(c.Formula, """", """""") & """"""  
  Next c  
 End Sub 

And...for god's sake, stop using those useless select and activate . 并且...看在上帝的份上,停止使用那些无用的selectactivate

Sheets("Totals").Range("E139:AB166").Formula = someString

is faster, easier to debug, and more readable. 更快,更容易调试且更具可读性。

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

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