VBA using a ListBox to multi select entire column in excel

北城余情 提交于 2020-02-07 05:18:26

问题


I have an excel file where I load the column headers dynamically from Row 2 across until I hit a null and put all those values into a list box transposed. This part is working as I expect it to.

My question is, how do I use the list box items to select the entire column that the named header exists in?

So in A2 B2 C2 I have the headers called Widget 1, 2, 3 respectively loaded into the listbox. Those load in order in the list box when the userform loads. In the list box, I would like to be able to click Widget 2 and 3 and have those entire columns selected. I don't want it hard coded, as I want it to be a selection as I could select widget 1 and 3 or any random selection as needed.

I could have as many as 50 widgets....so those will all load in the listbox on startup, I need to be able to select any of those values and have them select their corresponding column....

That is where I'm having issues, how to make the multi select happen.

Thanks in advance for any help.

EDITS:

This is the code used on the Private Sub UserForm_Initialize()

    'Figure out how many actual columns headers there are and then search for signal names

'Dim Lastcol As String
Dim FoundColumnRangeCalculated As Variant
Dim Lastcol As Variant
Dim FoundColumnRange As Variant

With ActiveSheet
Lastcol = .Cells(2, .Columns.Count).End(xlToLeft).Column
''MsgBox Lastcol
End With


'Convert numerical column location to letter value to use as dynamic range header lookup

Dim NumberToColumn As Variant
Dim SearchColumn
SearchColumn = Lastcol
NumberToColumn = Left(Cells(1, SearchColumn).Address(1, 0), InStr(1, Cells(1, SearchColumn).Address(1, 0), "$") - 1)
'MsgBox NumberToColumn

'Build the actual range from found column headers
FoundColumnRangeCalculated = "A2:" & (NumberToColumn & Lastcol)

'Transfer headers vertically to the list box for user to see

ListBox1.List = Application.WorksheetFunction.Transpose(Range(FoundColumnRangeCalculated))

Imported List Box Transposed on UserForm Load

So now when I click or multi click (not shown in this image, but multi with choose options to be enabled) the item(s), I would like the corresponding column it represents to be selected when each item is clicked.

Columns that the items are drawn from

Since they are built in "order" from left to right, I assume it is a 1:1 match and search and select, but I'm having trouble trying to sort that piece of it out..lots of examples about getting it's data, parsing, etc.....I just simply need a "When listbox items selected, use selection to enable its column".

The columns can't be hard coded for range as the headers could be A:F, A:AA, or A:ZZ.......so it has to be a dynamic matching.

Thanks to those that responded, hopefully this edited post and images satisfy the on hold status.


回答1:


To select multiple columns may try something like

Option Explicit
Private Sub ListBox1_Change()
Dim Ws As Worksheet, Rng As Range, c As Range, Sel As Range
Dim i As Long, Xval As Variant
Set Ws = ThisWorkbook.Sheets("Sheet1")
Set Rng = Ws.Range(Ws.Cells(2, 1), Ws.Cells(2, Me.ListBox1.ListCount))

    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
        Xval = Me.ListBox1.List(i)
            Set c = Rng.Find(Xval, LookIn:=xlValues)
                If Not c Is Nothing Then
                    If Sel Is Nothing Then
                    Set Sel = Ws.Columns(c.Column)
                    Else
                    Set Sel = Union(Sel, Ws.Columns(c.Column))
                    End If
                End If
        End If
    Next
    If Not Sel Is Nothing Then
    Sel.Select
    End If
End Sub



回答2:


In a case if you want to iterate through header's cells. It selects multiple columns for one widget if it's name matched more than once.

Works with ActiveSheet.

Dim result As Range
Dim criteria As String
Dim colcount As Integer

criteria = "widget1" ' Matching value
colcount = 10 ' Header columns count

For i = 1 To colcount
  ' Loop on 2nd row
  If Cells(2, i).Value = criteria Then
    ' If string matched with a cell's value
        If result Is Nothing Then
            ' if it's first match, set it as result column selection
            Set result = Columns(i)
        Else
            ' if it's not first match, add it to result selection
            Set result = Union(result, Columns(i))
        End If
  End If
Next

' Select result seletion
If Not result Is Nothing Then
    result.Select
End If


来源:https://stackoverflow.com/questions/57439067/vba-using-a-listbox-to-multi-select-entire-column-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!