简体   繁体   English

Excel vba:基于列A中的单元格获取列B中的单元格范围

[英]Excel vba: Getting range of cells on column B based on cell in column A


I have a vba script that gets info from all sheets, adds them to different comboboxes and whenever a value is selected from a combobox different things happens. 我有一个vba脚本,该脚本从所有工作表中获取信息,将其添加到不同的组合框,并且每当从组合框选择一个值时,都会发生不同的情况。
The first combo I populate as following: 我填充的第一个组合如下:

Private Sub Workbook_Open()

Dim oSheet As Excel.Worksheet
Dim oCmbBox As MSForms.ComboBox
Set oCmbBox = ActiveWorkbook.Sheets(1).cmbSheet
oCmbBox.Clear
For Each oSheet In ActiveWorkbook.Sheets
    If oSheet.Index > 1 Then
        oCmbBox.AddItem oSheet.Name
    End If
Next oSheet

End Sub

The second combobox is doing the following: 第二个组合框正在执行以下操作:

Private Sub cmbSheet_Change()

Dim oSheet As Excel.Worksheet

'Report combo box
Dim oCmbBox As MSForms.ComboBox
Set oCmbBox = ActiveWorkbook.Sheets(1).cmbSheet

'Tech combo box
Dim tCmbBox As MSForms.ComboBox
Set tCmbBox = ActiveWorkbook.Sheets(1).techCombo
tCmbBox.Clear

Dim rng As Range
Set rng = Sheets(oCmbBox.Value).Range("A2:A10")
For Each cell In rng
    If Not IsEmpty(cell.Value) Then
        tCmbBox.AddItem cell.Value
    End If
Next cell

ActiveWorkbook.Sheets(1).techCombo.ListIndex = 0
End Sub

I have a following sheet: 我有以下表格:
片

Now, selecting a value from techCombo (ie Teknik_1) I want the third combobox to be populated with data ranging from B6 - B9. 现在,从techCombo(即Teknik_1)中选择一个值,我希望在第三个组合框中填充B6-B9范围内的数据。
Is that possible?? 那可能吗??
Thanks in advance! 提前致谢!

Given your provided example data, something like this should work for you. 给定您提供的示例数据,类似这样的方法应该适合您。 Note that this code is in the Sheet1 code module: 请注意,此代码在Sheet1代码模块中:

Private Sub techCombo_Change()

    Dim ws As Worksheet
    Dim rFound As Range
    Dim cbo3 As ComboBox

    Set cbo3 = Me.ComboBox3 'Change to the actual name of the third combobox

    cbo3.Clear

    With Me.cmbSheet
        If .ListIndex = -1 Then Exit Sub    'Nothing selectd in cmbSheet
        Set ws = ActiveWorkbook.Sheets(.Text)
    End With

    With Me.techCombo
        If .ListIndex = -1 Then Exit Sub    'Nothing selected in techCombo
        Set rFound = ws.Columns("A").Find(.Text, ws.Cells(ws.Rows.Count, "A"), xlValues, xlWhole)
    End With

    If Not rFound Is Nothing Then
        If Trim(Len(rFound.Offset(2, 1).Text)) = 0 Then
            cbo3.AddItem rFound.Offset(1, 1).Value
        Else
            cbo3.List = ws.Range(rFound.Offset(1, 1), rFound.Offset(1, 1).End(xlDown)).Value
        End If
    End If

End Sub

Private Sub ComboBox21_Change() Dim TeckCMBxIndex TeckCMBxIndex = ActiveWorkbook.Sheets(3).ComboBox21.ListIndex Select Case TeckCMBxIndex Case 0 ActiveWorkbook.Sheets(3).ComboBox22.Clear Exit Sub Case 1 Set rng = Worksheets("SHEET3").Range("B6:B10") Case 2 Set rng = Worksheets("SHEET3").Range("B11:B15") End Select ActiveWorkbook.Sheets(3).ComboBox22.Clear For Each cell In rng If Not IsEmpty(cell.Value) Then ComboBox22.AddItem cell.Value End If Next cell End Sub

Hope this should help you... I tried it and it worked form me. 希望这对您有帮助...我尝试过,它对我有用。 Note that i had the Values and comboboxes in sheet3. 请注意,我在sheet3中有“值”和组合框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM