简体   繁体   English

Excel VBA:将WHEN公式插入到具有可变列的特定范围的单元格中

[英]Excel VBA: inserting WHEN formula into specific range of cells with variable changing column

Hello dear StackOverflow users, 您好亲爱的StackOverflow用户,

My issue is regarding inserting a Formular via Macro into specific range of cells. 我的问题是关于通过宏将Formular插入特定范围的单元格。 Again, again and again. 一次又一次。

So the Basic function of the formula should be the following: 因此,公式的基本功能应为:

=WHEN('ZEK '!F3="X";"6";WHEN('ZEK '!G3="X";"8";"1"))

In my macro it has to Change the column for the next specific range. 在我的宏中,必须将列更改为下一个特定范围。 This is Happening in a for Loop for i growing. 对于i这正在for循环中发生。

meaning --> =When('ZEK '!$F$"& i+2 &"....... or in any other Syntax that should work. My try fairly does not, thats why I need your help. 意思是-> =When('ZEK '!$F$"& i+2 &".......或其他应该起作用的语法。我的尝试完全没有,这就是为什么我需要您的帮助。

This is my Initial try where i exchanged literally everything with strings: 这是我的最初尝试,在这里我实际上用字符串交换了所有内容:

Sub 123()
Dim a,b,c As String
a = 1
b = 6
c = 8

....

'used and declared variables k, x, f, a, b, c

Range(Cells(k + 2, 5), Cells(k + 27, 5)).FormulaR1C1 = _
"=WHEN('ZEK'!$F$" & f & "=" & x & ";" & a & ";WHEN('ZEK'!$G$" & f & "=" & x & ";" & b & ";" & c & "))"

End Sub

With that i get Runtime Error 1004. (changed it to .Formula from .FormulaR1C1) 有了它我得到运行时错误1004。(将其从.FormulaR1C1更改为.Formula)

I hope i gave enough Information, so that you could help me. 希望我能提供足够的信息,以便您可以帮助我。 This really does not have to be performant, i just Need to get the formula into about 100.000 cells with i changing for each Range of cells 这实际上不一定是高性能的,我只需要将公式放入大约100.000个单元格中, i就每个单元格范围都进行了更改

I think it's a language problem... Try 我认为这是语言问题...尝试

"=IF('ZEK'!$F$" & f & "=" & x & "," & a & ",IF('ZEK'!$G$" & f & "=" & x & "," & b & "," & c & "))"

Notice the use of IF and , as a delimiter instead of ; 注意使用IF,作为分隔符来代替; . Is WHEN even a valid worksheet function? WHEN有效的工作表功能? To be honest I don't know if the worksheet functions get translated or not when setting the formula via VBA. 老实说,我不知道通过VBA设置公式时工作表函数是否被翻译。 The delimiters are adapted though. 分隔符虽然可以修改。

edit: there is also the .FormulaLocal property that should work with ; 编辑:还有.FormulaLocal属性应与;一起使用; as delimiter! 作为分隔符! So change .Formula to .FormulaLocal . 因此,将.Formula更改为.FormulaLocal This will probably cause an error if you execute it on a machine that uses , as default delimiter so I'd try to stick with .Formula . 如果在使用默认分隔符的计算机上执行它,可能会导致错误,因此我将尝试使用.Formula

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

相关问题 Excel VBA-用可变范围公式填充空的数据库单元格 - Excel VBA - Fill in empty database cells with variable range formula 在VBA Excel中引用公式内的一系列单元格 - Referring to a range of cells inside a formula in VBA Excel Excel VBA设置变量以选择特定工作表中的单元格区域 - Excel VBA set variable to select a range of cells in a specific worksheet excel vba-将工作表名称插入一系列单元格 - excel vba - Inserting sheetname into a range of cells VBA Excel - 如何将公式放置在表格中新添加列的主体范围的单元格范围中 - VBA Excel - How can a formula be placed in a range of cells of a body range of a newly added column in a table 如何使用VBA或公式对Excel范围内具有特定值且具有注释的单元格进行计数? - How do I count cells with in a range in excel with specific value that has a comment using vba or formula? 在 Excel 中插入带有 vba 的公式时出现“@” - '@' appearing when inserting formula with vba in Excel 使用Cells.Range在Excel中插入Vlookup公式时出错 - Error Inserting Vlookup Formula in Excel using Cells.Range 使用从列尾开始更改范围值,并将其用于公式中的下一列excel vba - Use changing range value from end of column and use it in a formula next column excel vba Excel VBA-将字符串公式转换为一系列单元格中的Excel计算 - Excel VBA - Convert a string Formula to an Excel Calculation in a range of cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM