财产“物品”是“只读” VBA 到 VB.NET 迁移时出错
更新2
请参阅我对问题的回答。
更新
因此,我根据下面的 @Eddy 评论更新了代码,但是我仍然在 myRange = Globals.Sheet... 行上收到错误。
Dim xlDown, i As Int32
Dim myRange As Excel.Range
myRange = Globals.Sheet1.Application.Transpose(
Globals.Sheet1.Range(
Globals.Sheet1.Range("A1"),
Globals.Sheet1.Range("A1").End(xlDown)).Value)
For i = 1 To myRange.Rows.Count
ListBox2.Items.Add(myRange.Cells(i, 1).Value)
Next i
错误说:
System.RuntimeType.ForwaredCallToInvokeMember(String memberName, BindingFlags 标志、对象目标、Int32[] aWrapperTypes、MessageData& msgData) 在 Microsoft.Office.Interop.Excel.Range.get_End(XlDirection 方向)
有什么想法吗?
再次感谢
---------------------------------原始问题------------- ------------------------
我有一个 VBA excel 程序,我正在将其转换为 VB.net(使用 Visual Studio 2010),并且我得到旧 VBA 版本中没有的错误。当我尝试将 Excel 范围转置到列表框时,会发生错误:
属性项是只读的
在 With 语句中
Dim xlDown As Int32
With Globals.Sheet1
ListBox2.Items = Globals.Sheet1.Application.Transpose(
Globals.Sheet1.Range(
Globals.Sheet1.Range("A1"),
Globals.Sheet1.Range("A1").End(xlDown)).Value)
End With
是只读的,我不知道该怎么办,也许我没有正确引用 Globals.Sheet1?
谢谢
Update2
Please see my answer to my question.
Update
So, I updated the code per @Eddy comment below, however I'm still getting an error on the myRange = Globals.Sheet...line.
Dim xlDown, i As Int32
Dim myRange As Excel.Range
myRange = Globals.Sheet1.Application.Transpose(
Globals.Sheet1.Range(
Globals.Sheet1.Range("A1"),
Globals.Sheet1.Range("A1").End(xlDown)).Value)
For i = 1 To myRange.Rows.Count
ListBox2.Items.Add(myRange.Cells(i, 1).Value)
Next i
ERROR says:
System.RuntimeType.ForwaredCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData) at Microsoft.Office.Interop.Excel.Range.get_End(XlDirection
Direction)
Any Ideas?
Thanks again
---------------------------------Orig Question-------------------------------------
I have a VBA excel program that I'm converting to VB.net (using Visual Studio 2010) and I'm getting an error that I did not have in the old VBA version. The error occurs when I try to transpose the excel range to a listbox:
Property Items is readOnly
Within the With statment
Dim xlDown As Int32
With Globals.Sheet1
ListBox2.Items = Globals.Sheet1.Application.Transpose(
Globals.Sheet1.Range(
Globals.Sheet1.Range("A1"),
Globals.Sheet1.Range("A1").End(xlDown)).Value)
End With
I'm not sure what to do with this, maybe I'm not referencing the Globals.Sheet1 correctly?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法替换
Items
集合本身,您必须替换其中的所有项目。您可以使用Clear
方法删除以前的项目,并使用AddRange
方法添加新项目:You can't replace the
Items
collection itself, you have to replace all items in it. You can use theClear
method to remove previous items, and theAddRange
method to add new ones:经过进一步的研究,我发现我不必使用转置方法将 excel.range 值分配给列表框。
感谢那些回复的人。
After doing some further research I figured out that I did not have to use the transpose method to assign the excel.range values to a listbox.
Thank you to those who responded.