标题:[求助]combo组合框问题
只看楼主
桃源书生
Rank: 1
等 级:新手上路
帖 子:99
专家分:3
注 册:2006-7-3
结帖率:33.33%
 问题点数:0 回复次数:3 
[求助]combo组合框问题
请问各位大侠,如何实现以下功能:
现有combo1和combo2两个组合框,combo1中有值“一”和“二”,当选中“一”时,combo2下拉框中显示a.xls表sheet1中第1列值;当选中“二”时combo2下拉框显示a.xls表sheet1中第2列值。

请问下面代码中的红色部分应该如何修改?如果用循环语句或case语句应该如何写?谢谢!

Option Explicit
Private Sub Form_Load()
  Dim xlsapp As Excel.Application
  Dim xlsworkbook As Excel.Workbook
  Dim xlssheet As Excel.Worksheet
  Combo1.AddItem "一"
  Combo1.AddItem "二"
  Set xlsapp = CreateObject("excel.application")
  Set xlsworkbook = xlsapp.Workbooks.Open(App.Path & "\a.xls")
  xlsapp.Visible = False
      Set xlssheet = xlsworkbook.Worksheets.Item(1)
      Dim h As Integer, l As Integer
      h = xlssheet.UsedRange.Rows.Count
      l = xlssheet.UsedRange.Columns.Count
      Dim i As Integer, j As Integer

    If Combo1.Text = "一" Then
      Combo2.Clear
      For i = 1 To h
        Combo2.AddItem xlssheet.Cells(i, 1)
      Next i
      Combo2.ListIndex = 0
    ElseIf Combo1.Text = "二" Then
      Combo2.Clear
      For j = 1 To h
        Combo2.AddItem xlssheet.Cells(j, 2)
      Next j
      Combo2.ListIndex = 0
    End If
  
  xlsapp.Quit
  Set xlssheet = Nothing
  Set xlsworkbook = Nothing
  Set xlsapp = Nothing
      
End Sub
a.rar (1.94 KB)


[ 本帖最后由 桃源书生 于 2009-11-19 22:41 编辑 ]
搜索更多相关主题的帖子: combo 
2009-11-19 22:39
haigecnpeng
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:261
专家分:510
注 册:2008-10-6
得分:0 
select case Combo1.Text
   case ""
      代码

   case ""
      代码


  ……
end select

都说到了.net年代,VB与C#都是一样的!
但为什么招聘的都是C#呢?
2009-11-19 23:05
yuk_yu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:334
专家分:134
注 册:2009-3-16
得分:0 
回复 2楼 haigecnpeng
有用
2010-01-04 21:53
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
程序代码:
Option Explicit

Private Sub Form_Load()
  Combo1.AddItem "一"
  Combo1.AddItem "二"
End Sub

Private Sub Combo1_Click()
  Dim xlsapp As Excel.Application
  Dim xlsworkbook As Excel.Workbook
  Dim xlssheet As Excel.Worksheet
  Set xlsapp = CreateObject("excel.application")
  Set xlsworkbook = xlsapp.Workbooks.Open(App.Path & "\a.xls")
  xlsapp.Visible = False
      Set xlssheet = xlsworkbook.Worksheets.Item(1)
      Dim h As Integer, l As Integer
      h = xlssheet.UsedRange.Rows.Count
      l = xlssheet.UsedRange.Columns.Count
      Dim i As Integer, j As Integer

    If Combo1.Text = "一" Then
      Combo2.Clear
      For i = 1 To h
        Combo2.AddItem xlssheet.Cells(i, 1)
      Next i
      Combo2.ListIndex = 0
    ElseIf Combo1.Text = "二" Then
      Combo2.Clear
      For j = 1 To h
        Combo2.AddItem xlssheet.Cells(j, 2)
      Next j
      Combo2.ListIndex = 0
    End If
  
  xlsapp.Quit
  Set xlssheet = Nothing
  Set xlsworkbook = Nothing
  Set xlsapp = Nothing

End Sub

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2010-01-05 12:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-292922-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028548 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved