使用 vb.net 编写 Excel 公式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6427559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing Excel formula using vb.net
提问by Sun
I want to write a formula to SUM from column B4 to M4, this should be done using vb.net programming.
我想从 B4 列到 M4 写一个公式到 SUM,这应该使用 vb.net 编程来完成。
I tried using the following stuff:
我尝试使用以下内容:
oXLWsheet.Range(4, 14).Formula = "=SUM(oXLWsheet!$B:M)"
"=SUM(B4:M4)"
"=SUM(B4,C4,D4,E4,F4,G4,H4,I4,J4,K4,L4,M4)"
Nothing is working for me. I'm getting the following error when I run the code:
没有什么对我有用。运行代码时出现以下错误:
"Exception from HRESULT: 0x800A03EC".
"Exception from HRESULT: 0x800A03EC".
回答by Lance Roberts
Try quoting the range, and if you're trying to fill two different rows you have to show each row like so:
尝试引用范围,如果您要填充两个不同的行,则必须像这样显示每一行:
oXLWsheet.Range("4:4,14:14").Formula = "=SUM(oXLWsheet!$B:M)"
This of course gives you a circular reference. If you're just trying to put the values in cell N4, then use this:
这当然会给你一个循环引用。如果您只是想将值放在单元格 N4 中,请使用以下命令:
oXLWsheet.Range("$N").Formula = "=SUM(oXLWsheet!$B:M)"
(Dollar signs optional)
(美元符号可选)
回答by Erwan Leroux
You have to use Cells instead of Range with this notation :
您必须使用 Cells 而不是 Range 与此表示法:
oXLWsheet.Cells(4, 14).Formula = "=SUM(oXLWsheet!$B:M)"
回答by zorroot
You have to split your statements when you are working with late binding:
当您使用后期绑定时,您必须拆分您的语句:
Dim oXLRange as Excel.Range
oXLRange = CType(oXLWsheet.Cells(4, 14), Excel.Range)
oXLRange.Formula = "SUM(Q8:T9)"
Using the .Formula
property you have to use the english version of the Excel formula. If you want to use the localized version of an Excel formula, use .FormulaLocal
使用该.Formula
属性,您必须使用 Excel 公式的英文版本。如果要使用 Excel 公式的本地化版本,请使用.FormulaLocal