vb读取excel内容

chs048127 2013-11-14 12:41:00
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)
...全文
206 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dxy2798 2013-11-27
  • 打赏
  • 举报
回复
我觉得用ExcelApp不是很好,尤其是数据量大的情况下,我现在都用sql连接excel,速度比较快,楼主的要求写在sql语句的条件里也好实现,关键是不用循环。
舉杯邀明月 2013-11-15
  • 打赏
  • 举报
回复
哦,我没在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
chs048127 2013-11-14
  • 打赏
  • 举报
回复
For i = 1 To xlsheet.Range("B:B").Rows(v).End(xlUp).Row 提示实时错误“1004”应用程序定义或对象定义错误
舉杯邀明月 2013-11-14
  • 打赏
  • 举报
回复
'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

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

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧