excel - VB.NET 从 excel 中获取特定的单元格值

标签 excel vb.net

我正在尝试评估 Excel 表中的特定单元格值是否为“”以在我的 VB.NET 应用程序的 if 语句中使用。我修改了用于写入 excel 的代码,但无法获取单元格值。我的代码:

   Sub Excel_LoadProjectsSchedule()

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Open("G:\100 Databases\Projects Schedule.xlsx")
        xlApp.Visible = False
        xlWorkSheet = xlWorkBook.Worksheets("sheet1")
        Dim ProjectFinished  as boolean

        'Set variables
        Result = xlWorkSheet.Cells.Find(ProjectNumber, LookAt:=Excel.XlLookAt.xlWhole)

        If xlWorkSheet.Cells(Result.Row, 3).value = "" Then
            ProjectFinished = False
        Else
            ProjectFinished = True
        End If

        'Save and close
        xlApp.DisplayAlerts = False
        xlWorkBook.Close(SaveChanges:=True)
        xlApp.Quit()

    End Sub
错误已开启
If xlWorkSheet.Cells(Result.Row, 3).value = "" Then
它说“System.MissingMemberException:'未找到'Range'类型的'公共(public)成员'值'。'
"
我有
Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing
在此模块中的子之外。
我做错了什么,有人可以帮我解决这个问题吗?

最佳答案

我做了一些转换,以便编译器可以识别类型。

Sub Excel_LoadProjectsSchedule(ProjectNumber As Integer)

    Dim xlApp = New Excel.Application
    Dim xlWorkBook = xlApp.Workbooks.Open("G:\100 Databases\Projects Schedule.xlsx")
    xlApp.Visible = False
    Dim xlWorkSheet = DirectCast(xlWorkBook.Worksheets("sheet1"), Excel.Worksheet)
    Dim ProjectFinished As Boolean

    'Set variables
    Dim Result = xlWorkSheet.Cells.Find(ProjectNumber, LookAt:=Excel.XlLookAt.xlWhole)
    Dim row = Result.Row
    Dim cell = DirectCast(xlWorkSheet.Cells(row, 3), Excel.Range)
    If cell.Value Is Nothing Then
        'What do you want to do?
    Else
        If cell.Value.ToString = "" Then
            ProjectFinished = False
        Else
            ProjectFinished = True
        End If
    End If

    'Save and close
    xlApp.DisplayAlerts = False
    xlWorkBook.Close(SaveChanges:=True)
    xlApp.Quit()

End Sub

关于excel - VB.NET 从 excel 中获取特定的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69753022/

相关文章:

excel - 无法在 VbScript 中复制 Excel 中的大量数据

javascript - Office.js Excel Range getRow 给出错误的范围地址

vb.net - 使用 DLL 中的类创建 VB6 应用程序,然后在构建后换出该 DLL?

vb.net - 在生产环境中调试 CLR20r3 System.InvalidOperationException

vba - 值更改时添加带值的行

vba - 使用 VBA 自动过滤多列,使用来自不同工作表的值

VB.Net DateTimePicker编辑日历部分

vb.net - 如何使用 linq to sql 的文本框编辑/更新数据库中的记录?

javascript - 将 onclick 事件上的两个或多个属性添加到数据网格行

vba - 在继续循环之前提示用户