VB.NET 从 excel 获取特定的单元格值
VB.NET get specific cell value from excel
我正在尝试评估 excel table 中的特定单元格值是否为“”以在我的 VB.NET 应用程序的 if 语句中使用。我修改了用于写入 excel 的代码,但无法获取单元格值。我的代码:
Sub Excel_LoadProjectsSchedule()
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 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:'Public 成员 'value' 类型 'Range' 未找到。'
“
我有
Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing
在这个模块之外。
我做错了什么,有人可以帮我解决这个问题吗?
如果在特定行中查找特定单元格。
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
然后是结束部分
我认为,如果您特别想检查具有该项目编号的行的第 3 列中的内容,那么您离解决方案不远了。
我只在 VBA 内部测试过它,但有些东西是:
Sub Excel_LoadProjectsSchedule()
Dim xlWorksheet As Worksheet, Result As Range, ProjectFinished As Boolean
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 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
问题在于,“结果”尚未分配给范围,因此您的代码无法访问行 属性。
我做了一些转换,以便编译器可以识别类型。
Sub Excel_LoadProjectsSchedule(ProjectNumber As Integer)
Dim xlApp = New Excel.Application
Dim xlWorkBook = xlApp.Workbooks.Open("G:0 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 table 中的特定单元格值是否为“”以在我的 VB.NET 应用程序的 if 语句中使用。我修改了用于写入 excel 的代码,但无法获取单元格值。我的代码:
Sub Excel_LoadProjectsSchedule()
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 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:'Public 成员 'value' 类型 'Range' 未找到。' “
我有
Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing
在这个模块之外。
我做错了什么,有人可以帮我解决这个问题吗?
如果在特定行中查找特定单元格。
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
然后是结束部分
我认为,如果您特别想检查具有该项目编号的行的第 3 列中的内容,那么您离解决方案不远了。 我只在 VBA 内部测试过它,但有些东西是:
Sub Excel_LoadProjectsSchedule()
Dim xlWorksheet As Worksheet, Result As Range, ProjectFinished As Boolean
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 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
问题在于,“结果”尚未分配给范围,因此您的代码无法访问行 属性。
我做了一些转换,以便编译器可以识别类型。
Sub Excel_LoadProjectsSchedule(ProjectNumber As Integer)
Dim xlApp = New Excel.Application
Dim xlWorkBook = xlApp.Workbooks.Open("G:0 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