简体   繁体   English

从文本文件到单元格的Excel公式

[英]Excel formula from textfile into cell

I want to get formula from textfile into cell to identify the rows by named range. 我想从文本文件中获取公式到单元格中,以按命名范围标识行。 I have this macro: 我有这个宏:

Dim objStream
Dim strData As String
Set objStream = CreateObject("ADODB.Stream")
objStream.Charset = "utf-8"
objStream.Open
objStream.LoadFromFile ("path_of_txt_file")
strData = objStream.ReadText()
strData = Trim(Application.Clean(strData))
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.Value = "Name"
Range("A2").Select

Range("A2").FormulaR1C1 = strData

The textfile contains like this: 文本文件包含如下内容:

=IF(
    OR(
        named range="this";
        named range="that";
        named range="something"
    );
    "True";
    "False"
)
...

Its a nested IF formula. 它是一个嵌套的IF公式。

Without the equality sign it work, but i need to put it mannually. 没有平等标志,它就可以工作,但是我需要手动把它放进去。 With that, i get this error 这样,我得到这个错误

Runtime error '1004':
Application-defined or object-defined error

How can i fix this? 我怎样才能解决这个问题? I tried with Value instead of FormulaR1C1, but the same results. 我尝试使用Value而不是FormulaR1C1,但是结果相同。

Thanks for all answers! 感谢所有答案!

There doesnt seem to be a problem with Range("A2").FormulaR1C1 = strData . Range("A2").FormulaR1C1 = strData似乎没有问题。 Replacing the ; 更换; in the formula with , worked. 在的公式中,有效。

Posting the answer as it worked for OP. 发布答案,因为它适用于OP。

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

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