Vb想建立一个combobox控件,里面的选项从一个外来的 excel表导入,怎么弄?
VB中,想建立一个combobox控件,其中的选项从一个已经存在的excel中的某列导入,实现当选择某个选项时,把excel表中的另外一列对应数据显示在text控件中,要...
VB中,想建立一个combobox控件,其中的选项从一个已经存在的excel中的某列导入,实现当选择某个选项时,把excel表中的另外一列对应数据显示在text控件中,要怎么弄呢?求高人,请详细解答,本人新手,20先上,采纳追加
展开
2个回答
展开全部
Private Sub Combo1_Click()
Dim xlapp As Excel.Application '定义excel类
Dim xlbook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim filepath As String
filepath = App.Path & "\123.xls"
Set xlapp = CreateObject("excel.application") '创建excel应用类
xlapp.Visible = False '设置excel不可见
Set xlbook = xlapp.Workbooks.Open(filepath) '打开excel工作簿
Set xlsheet = xlbook.Worksheets(1) '打开excel工作表
'假设 第3列
'读取 第5行
Text1.Text = xlsheet.Cells(5, 3)
Set xlsheet = Nothing
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
Private Sub Command1_Click()
Dim xlapp As Excel.Application '定义excel类
Dim xlbook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim filepath As String
filepath = App.Path & "\123.xls"
Set xlapp = CreateObject("excel.application") '创建excel应用类
xlapp.Visible = False '设置excel不可见
Set xlbook = xlapp.Workbooks.Open(filepath) '打开excel工作簿
Set xlsheet = xlbook.Worksheets(1) '打开excel工作表
'假设 第3列
For i = 1 To 5 '读取 5行
Combo1.AddItem xlsheet.Cells(i, 3)
Next
Set xlsheet = Nothing
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
Dim xlapp As Excel.Application '定义excel类
Dim xlbook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim filepath As String
filepath = App.Path & "\123.xls"
Set xlapp = CreateObject("excel.application") '创建excel应用类
xlapp.Visible = False '设置excel不可见
Set xlbook = xlapp.Workbooks.Open(filepath) '打开excel工作簿
Set xlsheet = xlbook.Worksheets(1) '打开excel工作表
'假设 第3列
'读取 第5行
Text1.Text = xlsheet.Cells(5, 3)
Set xlsheet = Nothing
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
Private Sub Command1_Click()
Dim xlapp As Excel.Application '定义excel类
Dim xlbook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim filepath As String
filepath = App.Path & "\123.xls"
Set xlapp = CreateObject("excel.application") '创建excel应用类
xlapp.Visible = False '设置excel不可见
Set xlbook = xlapp.Workbooks.Open(filepath) '打开excel工作簿
Set xlsheet = xlbook.Worksheets(1) '打开excel工作表
'假设 第3列
For i = 1 To 5 '读取 5行
Combo1.AddItem xlsheet.Cells(i, 3)
Next
Set xlsheet = Nothing
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
展开全部
有点麻烦吧,而且似乎没多少实用的
在通用那儿声明:
dim xlsApp As new Excel.Application
Dim xlBook As Excel.Workbook
Dim ws As Excel.Worksheet
以后是添加到combobox的方法
private sub addtocombo()
set xlBook=xlsApp.Workbooks.Open(“d:\test.xls")
set ws=xlBook.sheets(1)
dim r as long
r=2 ’假设从第二行导入
while len(trim(ws.cells(r,1))>0
combo1.additem ws.cells(r,1)
r=r+1
wend
xlsApp.quit
msgbok "OK"
end sub
Private Sub Combo1_Click()
set xlBook=xlsApp.Workbooks.Open(“d:\test.xls")
set ws=xlBook.sheets(1)
dim r as long
r=2 ’假设从第二行导入
while len(trim(ws.cells(r,1))>0
if ws.cells(r,1)=combo1.text then
text1.text=ws.cells(r,2)'假设你要在文本框中显示的数据在第二列
exit
endif
r=r+1
wend
我没测试过,大体就是这样
xlsApp.quit
End Sub
在通用那儿声明:
dim xlsApp As new Excel.Application
Dim xlBook As Excel.Workbook
Dim ws As Excel.Worksheet
以后是添加到combobox的方法
private sub addtocombo()
set xlBook=xlsApp.Workbooks.Open(“d:\test.xls")
set ws=xlBook.sheets(1)
dim r as long
r=2 ’假设从第二行导入
while len(trim(ws.cells(r,1))>0
combo1.additem ws.cells(r,1)
r=r+1
wend
xlsApp.quit
msgbok "OK"
end sub
Private Sub Combo1_Click()
set xlBook=xlsApp.Workbooks.Open(“d:\test.xls")
set ws=xlBook.sheets(1)
dim r as long
r=2 ’假设从第二行导入
while len(trim(ws.cells(r,1))>0
if ws.cells(r,1)=combo1.text then
text1.text=ws.cells(r,2)'假设你要在文本框中显示的数据在第二列
exit
endif
r=r+1
wend
我没测试过,大体就是这样
xlsApp.quit
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询