vb读取excel内容
vb读取excel内容。我想获取的是这样的。在excel表格中搜索B列内容为“32KP”对应的E列的内容比如B17为“32KP”则读取E17的内容到text1中。
我是这样读取表格的
Dim ExcelApp, oBook, a, c
Set ExcelApp = CreateObject("Excel.Application")
Set oBook = ExcelApp.Workbooks.Open("d:\1.xls", missing, True)
a = Text6 '表格名
Set xlsheet = oBook.Sheets(a) vb excel
[解决办法]
哦,我没在VB6中测试。难道在VB6代码中,不能使用VBA对象的全部属性啊。
那就换一种方法吧。
但这样有一个要求:有效数据区域内,被识别的列不能有空单元格(比如你这儿,就是B列)。
'Dim ExcelApp, oBook, a, c
Dim ExcelApp As Object ' As Excel.Application
Dim oBook As Object ' As Workbook
Dim xlsheet As Object ' As Worksheet
Dim a As String, c As String
'Dim i As Long, v As Long
Dim i As Long
Set ExcelApp = CreateObject("Excel.Application")
'Set oBook = ExcelApp.Workbooks.Open("d:\1.xls", missing, True)
Set oBook = ExcelApp.Workbooks.Open("d:\1.xls", False, True)
a = Text6 '表格名
Set xlsheet = oBook.Sheets(a)
'v = xlsheet.Range("B:B").Rows.Count
'For i = 1 To xlsheet.Range("B:B").Rows(v).End(xlUp).Row
' If (xlsheet.Cells(i, 2).Text = "32KP") Then
' Text1.Text = xlsheet.Cells(i, 5).Text
' Exit For
' End If
'Next
i = 2 ' 数据从第2行开始
Do
c = xlsheet.Cells(i, 2).Text
If (Len(c) = 0) Then Exit Do
If (c = "32KP") Then
Text1.Text = xlsheet.Cells(i, 5).Text
Exit Do
End If
i = i + 1 ' 指向下一行
Loop