财产“物品”是“只读” VBA 到 VB.NET 迁移时出错

发布于 2024-11-28 20:25:23 字数 1481 浏览 0 评论 0原文

更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-12-05 20:25:23

您无法替换 Items 集合本身,您必须替换其中的所有项目。您可以使用 Clear 方法删除以前的项目,并使用 AddRange 方法添加新项目:

ListBox2.Items.Clear() ' If you need to remove previous items
ListBox2.Items.AddRange(Globals.Sheet1.Application.Transpose(Globals.Sheet1.Range(Globals.Sheet1.Range("A1"), Globals.Sheet1.Range("A1").End(xlDown)).Value))

You can't replace the Items collection itself, you have to replace all items in it. You can use the Clear method to remove previous items, and the AddRange method to add new ones:

ListBox2.Items.Clear() ' If you need to remove previous items
ListBox2.Items.AddRange(Globals.Sheet1.Application.Transpose(Globals.Sheet1.Range(Globals.Sheet1.Range("A1"), Globals.Sheet1.Range("A1").End(xlDown)).Value))
梦中的蝴蝶 2024-12-05 20:25:23

经过进一步的研究,我发现我不必使用转置方法将 excel.range 值分配给列表框。

感谢那些回复的人。

        Dim range As Excel.Range = Globals.Sheet1.UsedRange
        Dim obj As Object

        For rCnt = 1 To range.Rows.Count
            For cCnt = 1 To range.Columns.Count
                obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
                ListBox1.Items.Add(obj.value)
            Next
        Next

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.

        Dim range As Excel.Range = Globals.Sheet1.UsedRange
        Dim obj As Object

        For rCnt = 1 To range.Rows.Count
            For cCnt = 1 To range.Columns.Count
                obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
                ListBox1.Items.Add(obj.value)
            Next
        Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文