首页 文章

在vba中使用变量工作表作为公式目标

提问于
浏览
0

我正在写一个相当复杂的宏,但我遇到的问题是在一个工作表上创建一个公式,在代码“Input_Sheet”中,将自己等同于新创建的工作表中的单元格,可变地设置为“ws” . ws的每次迭代都被命名,因此这不是问题 . 我认为正确的方法是:

ActiveCell.FormulaR1C1 = " & ws.name & !R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"

(不要担心totalRowCounter和totalColumnCounter变量,它们是相应定义的) . 我只是不知道为什么公式没有适当地引用新的ws表 . 有什么想法吗?

2 回答

  • 1

    您只需要从引号中取出 ws.name ,并在工作表名称之前和之后添加撇号,以帮助处理名称中可能包含空格的任何工作表:

    ActiveCell.FormulaR1C1 = "='" & ws.name & "'!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
    
  • 3

    我认为这将是以下内容:

    ActiveCell.FormulaR1C1 = "=" & ws.name & "!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
    

相关问题