简体   繁体   English

VBA 将字符串的文字值放入 excel 单元格中

[英]VBA put literal value of string into an excel cell

Excel 2010 Is there a way to put the literal contents of a string variable into an excel cell? Excel 2010 有没有办法将字符串变量的文字内容放入 excel 单元格中?

I can achieve what I want to do with this hard code , but this code is deep inside nested subroutines.我可以用这段硬代码实现我想做的事情,但这段代码深藏在嵌套的子例程中。 I want to soft code this, so that the contents of Formula_wanted is inserted in the cell.我想对此进行软编码,以便将 Formula_wanted 的内容插入到单元格中。 Here's the code that puts the value of the formula in the cell.下面是将公式放入单元格的代码。

   If modifier1 = "ILMROS " Then
          Range("M4").Select
          ActiveCell.FormulaR1C1 = "Sales Total"
          Range("M5").Select
          ActiveCell.FormulaR1C1 = "=[@701]+[@707]+[@708]+[@709]+[@712]"

The problem with this is that the categories change all the time.问题在于类别一直在变化。 They are read from 1 table on the input sheet.它们是从输入表上的 1 个表中读取的。 If I could make it so that only this input table needed to be altered when categories change, no one would have to dig through the code to find the resulting problem.如果我能做到在类别更改时只需要更改此输入表,那么就没有人需要深入研究代码来查找由此产生的问题。

I can get VBA to create the string that I want inside a variable, but, trying everything below, I canNOT get it to put the string from the variable into cell "M5."我可以获得 VBA 在变量中创建我想要的字符串,但是,尝试下面的所有操作,我无法将变量中的字符串放入单元格“M5”。 Per the rules of OS, I've included everything I've tried with the error received.根据操作系统的规则,我已经包含了我尝试过的所有内容以及收到的错误。 THANK YOU for any help you can give.感谢您提供的任何帮助。

   Dim Formula_wanted as string (also tried Dim as Variant, same results)

    Formula_wanted = "=[@"
              For x = 1 To UBound(ILMROS) - 1
                  Formula_wanted = Formula_wanted & ILMROS(x) & "]+[@"
              Next x
      Formula_wanted = Formula_wanted & ILMROS(UBound(ILMROS)) & "]"

  MsgBox "my formula is:  " & Formula_wanted

   'shows this in msgbox: my formula is: = "=[@701]+[@707]+[@708]+[@709]+[@712]"

   '@@@@@@@@@@@@@@@@@@@@@@

   These all failed:
  Range("Y5").Text = Formula_wanted
     ' error 424
  Range("Y5").Value = Formula_wanted
       error 1004
  Range("y5").Formula = Formula_wanted
      'error 1004
  Range("Y5").FormulaR1C1 = Formula_wanted
      error 1004
  Cells(4, 24).Select
     error 424
  ActiveCell.Text = Formula_wanted
    'error 424

  Set Ob = Range("Y5") ' Ob dim as "object"
    Ob.Value = Formula_wanted
      '1004
 Ob.Text = Formula_wanted
      '1004
  Cells(4, 24).Text = Formula_wanted
      '1004

  ActiveCell.Text = Formula_wanted
         '1004    
  Range("Y5").FormulaLocal = Formula_wanted
      'error 1004

  End Sub

It's not clear what your formula is trying to do, but I don't have enough reputation to comment.目前尚不清楚您的公式试图做什么,但我没有足够的声誉来发表评论。 By setting a formula to "=[@701]+[@707]+[@708]+[@709]+[@712]" it looks like you are trying to add values from table columns named 701, 707 etc.通过将公式设置为“=[@701]+[@707]+[@708]+[@709]+[@712]”,看起来您正在尝试从名为 701、707 等的表列中添加值。

If this is what you are doing then either:如果这是你正在做的,那么要么:

a) you referencing a column that doesn't exist in the table; a) 您引用了表中不存在的列; or b) "Y5" is not in the table.或 b) “Y5”不在表中。

If it's a), then correct the column names.如果是 a),则更正列名。 If it's b) then change your formula to如果是 b) 则将您的公式更改为

"=Table1[@701]+Table1[@707]+Table1[@708]+Table1[@709]+Table1[@712]" "=表 1[@701]+表 1[@707]+表 1[@708]+表 1[@709]+表 1[@712]"

If you're trying to do something else such as reference a specific row in the table, then you may need to completely rethink and look at using ListObject.如果您正在尝试执行其他操作(例如引用表中的特定行),那么您可能需要完全重新考虑并考虑使用 ListObject。 I've found this link very helpful in the past.我发现此链接过去非常有用。

List Object helpful reference 列表 Object 有帮助的参考

To clarify further, the formula value in your string variable appears to be invalid.为了进一步澄清,您的字符串变量中的公式值似乎无效。

Example with String variable that works带有有效字符串变量的示例

Dim validFormulaString As String
validFormulaString = "=1+2"
Range("A1").Formula = validFormulaString

Example with String variable that does not work as the resulting formula is not valid String 变量的示例不起作用,因为结果公式无效

Dim invalidFormulaString As String
invalidFormulaString = "=1$2"
Range("A1").Formula = invalidFormulaString

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

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