簡體   English   中英

VB.NET 從 excel 中獲取特定的單元格值

[英]VB.NET get specific cell value from excel

我正在嘗試評估 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' 類型的公共成員 'value' 未找到。”

我有

Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing

在這個模塊中的 sub 之外。

我做錯了什么,請有人幫我解決這個問題嗎?

我認為,如果您特別想使用該項目編號檢查行的第 3 列中的內容,那么您離解決方案不遠了。 我只在 VBA 內部測試過它,但有以下幾點:

Sub Excel_LoadProjectsSchedule()
    Dim xlWorksheet As Worksheet, Result As Range, ProjectFinished  As Boolean
    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open("G:\100 Databases\Projects Schedule.xlsx")
    xlApp.Visible = False
    Set xlWorkSheet = xlWorkBook.Worksheets("sheet1")
    
    'Set variables
     Set Result = xlWorksheet.Cells.Find(Projectnumber, LookIn:=Excel.XlLookin.xlValues, LookAt:=Excel.XlLookAt.xlWhole)
     if not Result is nothing then
     If Cells(Result.Row, 3).Value = "" Then
         ProjectFinished = False
     Else
         ProjectFinished = True
     End If
 End Sub

問題是,“結果”尚未分配給范圍,因此您的代碼無法訪問 Row 屬性。

如果在某行中尋找某個單元格。

 Dim AppExcel As Object
    Dim workBook As Object
    AppExcel = CreateObject("Excel.Application")

    workBook = AppExcel.Workbooks.Open("C:\SO\SO.xlsx")
    AppExcel.Visible = False

    Worksheets = workBook.worksheets
    If Worksheets("Blad1").Cells(2, 3).text = "" Then

        MessageBox.Show("Empty")

    Else
        MessageBox.Show("Text")
    End If

然后是關閉部分

我做了一些轉換,以便編譯器可以識別類型。

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.ToString = "" Then
        ProjectFinished = False
    Else
        ProjectFinished = True
    End If

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

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM