Vba listbox.column to range

pulpo

New Member
Joined
Feb 10, 2022
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hello,
simple question this time. I populated a listbox with3 columns with a range from my worksheet.
If I want to write just one column back to a range in my worksheet do I need to loop through all values of the desired column?
To use one value, selected or described, is easy but the entire column was something I couldn't to without looping.
Any ideas?

With best regards
pulpo
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Let's say that you wanted to transfer the items from the 3rd column of your listbox to your worksheet, starting at A2, you could do it this way . . .

VBA Code:
    With Me.ListBox1
        Worksheets("Sheet1").Range("A2").Resize(.ListCount).Value = Application.Index(.List, 0, 3)
    End With

Change the references accordingly.

Hope this helps!
 
Upvote 0
Let's say that you wanted to transfer the items from the 3rd column of your listbox to your worksheet, starting at A2, you could do it this way . . .

VBA Code:
    With Me.ListBox1
        Worksheets("Sheet1").Range("A2").Resize(.ListCount).Value = Application.Index(.List, 0, 3)
    End With

Change the references accordingly.

Hope this helps!
Hey Domenic,
that works great except that I have problems when the data comes from a range that is filtered. I will try to figure out what is excactly the reason. If I cannot solve it on my own I will upload an example to show what I mean.

Thank you
pulpo
 
Upvote 0
Hey Domenic,
that works great except that I have problems when the data comes from a range that is filtered. I will try to figure out what is excactly the reason. If I cannot solve it on my own I will upload an example to show what I mean.

Thank you
pulpo
ok, that´s surprisingly more difficult with filtered data. Please see attached pictures.
  • 3 columns with 500 values each on the worksheet (pic1)
  • filter for column C in worksheet is activated (manually), (pic2)
  • button "column" click => userform to show the listbox with all values in 3 columns (working).(pic3)
  • userform is closed => transfer the 3rd column of the listbox to column E on the worksheet (not working).(pic4,5)
Now it only transfers one value which seems to be the first one to whatever cell that the filter is used on. I hope that helps to understand what I need.

The code looks like this
VBA Code:
Sub userform_initialize()
Call column2
End Sub

Sub column2()

Dim LastRow As Integer

Me.Caption = "column"

With Me.ListBox1
    .ColumnCount = 3
    .ColumnWidths = "50;50;50"

End With

With Worksheets("pos")

LastRow = .UsedRange.Rows.Count

Me.ListBox1.List = .Range("A2:C" & LastRow).Value

End With
End Sub
Private Sub UserForm_QueryClose(cancel As Integer, closemode As Integer)

 With Me.ListBox1
        Worksheets("pos").Range("E2").Resize(.ListCount).Value = Application.Index(.List, 0, 3)
    End With

End Sub


Many thanks

pulpo
 

Attachments

  • pic1.JPG
    pic1.JPG
    104.1 KB · Views: 14
  • pic2.JPG
    pic2.JPG
    62.2 KB · Views: 19
  • pic3.JPG
    pic3.JPG
    170.3 KB · Views: 29
  • pic4.JPG
    pic4.JPG
    88.4 KB · Views: 11
  • pic5.JPG
    pic5.JPG
    118.7 KB · Views: 25
Upvote 0
When the data is filtered, you'll need to loop through each item in your listbox and transfer them to your worksheet one by one. The following code will first check whether the data is filtered. If so, it loops through each item, one by one, etc. If not, it transfers the items to your worksheet without looping.

VBA Code:
Private Sub UserForm_QueryClose(cancel As Integer, closemode As Integer)

    Dim dataWorksheet As Worksheet
    Set dataWorksheet = Worksheets("Pos")

    Dim outputRange As Range
    Set outputRange = dataWorksheet.Range("E2")
   
    With dataWorksheet
   
        If .FilterMode Then
   
            Dim i As Long
            With Me.ListBox1
                For i = 0 To .ListCount - 1
                    outputRange.Offset(i).Value = .List(i, 2)
                Next i
            End With
           
        Else
       
            With Me.ListBox1
                outputRange.Resize(.ListCount).Value = Application.Index(.List, 0, 3)
            End With
       
        End If
       
    End With

End Sub

Hope this helps!
 
Upvote 0
Solution
When the data is filtered, you'll need to loop through each item in your listbox and transfer them to your worksheet one by one. The following code will first check whether the data is filtered. If so, it loops through each item, one by one, etc. If not, it transfers the items to your worksheet without looping.

VBA Code:
Private Sub UserForm_QueryClose(cancel As Integer, closemode As Integer)

    Dim dataWorksheet As Worksheet
    Set dataWorksheet = Worksheets("Pos")

    Dim outputRange As Range
    Set outputRange = dataWorksheet.Range("E2")
  
    With dataWorksheet
  
        If .FilterMode Then
  
            Dim i As Long
            With Me.ListBox1
                For i = 0 To .ListCount - 1
                    outputRange.Offset(i).Value = .List(i, 2)
                Next i
            End With
          
        Else
      
            With Me.ListBox1
                outputRange.Resize(.ListCount).Value = Application.Index(.List, 0, 3)
            End With
      
        End If
      
    End With

End Sub

Hope this helps!
Hey, thanks. It's looping then ok. I appreciate your time to answer.

Best regard
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top