“从类型'Range到类型Boolean'的转换无效 [英] "Conversion from type 'Range to type Boolean' is not valid

查看:86
本文介绍了“从类型'Range到类型Boolean'的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在Solidworks的API环境中启动Visual Basic,然后转到Visual Studio.我注意到方法略有不同.我似乎有问题,我不确定我的代码对Visual Studio有多远,我将不胜感激.

在运行下面的代码时,出现此错误::

从类型"Range到布尔类型"的转换无效

Hi
I started Visual Basic in an API environment in Solidworks, moving over to visual studio Im noticing the methods are slightly different. I seem to have a problem and im not sure how far out my code is for Visual Studio, I''d appreciate any help.

On running the Code Below I get this Error ::

"Conversion from type ''Range to type Boolean'' is not valid

<br />
<pre lang="vb">Option Explicit On<br />
Imports Microsoft.Office.Interop.Excel<br />
<br />
Public Class MainForm<br />
    Dim Excelobject As Object<br />
    Dim ExcelBook As Object<br />
    Dim ExcelSheet As Object<br />
    Dim xlsh As Object<br />
    Dim CopyCell As String<br />
<br />
    Public Function GetExcellFilePath() As String<br />
        GetExcellFilePath = "C:\Data\ItemBase.xlsx"<br />
    End Function<br />
<br />
    Public Sub OpenExcellDocument()<br />
        Excelobject = GetObject(GetExcellFilePath)<br />
        xlsh = Excelobject.ActiveSheet<br />
        If Excelobject Is Nothing Then<br />
            MsgBox("Could not Open ItemBase.xlsx")<br />
        End If<br />
    End Sub<br />
<br />
    Public Sub Weapons(ByVal Row, ByVal Column)<br />
        OpenExcellDocument()<br />
        MsgBox(Row & Column)<br />
        Do While (xlsh.Cells(Row, Column))<br />
            CopyCell = xlsh.Cells(Row, Column).Value.ToString()<br />
            Me.ComboBoxWeapons.Items.Add(CopyCell)<br />
            Row = Val(Row + 1)<br />
        Loop<br />
    End Sub<br />
<br />
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _<br />
System.EventArgs) Handles Button1.Click<br />
        Weapons(3, 1)<br />
    End Sub<br />
<br />
End Class<br />
</pre>

推荐答案

您没有指出错误所在的行,虽然不好,但是xlsh.Cells(Row, Column)不正确.它需要Boolean,因为循环需要条件.我不知道您要达到什么目的,可能您需要foreach循环来遍历范围.

—SA
You did not indication the line with the error, which is not nice, but xlsh.Cells(Row, Column) is incorrect. It requires Boolean as the loop requires condition. I have not idea what you try to achieve, probably you need foreach loop to traverse the range.

—SA


这篇关于“从类型'Range到类型Boolean'的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆