解决方案 »

  1.   

    '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 LongSet 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
      

  2.   

    For i = 1 To xlsheet.Range("B:B").Rows(v).End(xlUp).Row 
    提示实时错误“1004”应用程序定义或对象定义错误
      

  3.   

    哦,我没在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 LongSet 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
      

  4.   

    我觉得用ExcelApp不是很好,尤其是数据量大的情况下,我现在都用sql连接excel,速度比较快,楼主的要求写在sql语句的条件里也好实现,关键是不用循环。