Exporting items in list box to range in Excel - vba

So I have scoured google and forum after forum (including the Stack) trying to figure this out. All I want to do is have an ActiveX button export the contents of a list box to a range in Excel.
Below is the code I have that adds items from ListBox1 to ListBox2. After all desired items are moved to ListBox2, said ActiveX button (SomeButton_Click) would then export all the items in ListBox2 to "Sheet15" starting at range("a1").
(Please note, this is an ActiveX ListBox that is not on an actual form. It is within a worksheet)
Public Sub BTN_MoveSelectedRight_Click()
Dim iCtr As Long
Dim n As Long, lRow As Long
Dim cStartCell As Range
For iCtr = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(iCtr) = True Then
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
End If
Next iCtr
For iCtr = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(iCtr) = True Then
Me.ListBox1.RemoveItem iCtr
End If
Next iCtr
End Sub
Below would be the button that would perform the export:
Public Sub SomeButton_Click()
'What code can I put here to perform the export to abovementioned range?
End sub
Any help would be extremely appreciated as I have spent hours trying to figure this out (as much as I don't want to actually admit this, it is true)
Thank you!

Here you go:
Public Sub SomeButton_Click()
Dim v
v = Sheet15.ListBox2.List
Sheet15.[a1].Resize(UBound(v)) = v
End Sub
If the listbox is on a different worksheet, you'll need to adjust that.
Edit #1
This is better:
Public Sub SomeButton_Click()
With Sheet15
.[a1].Resize(.ListBox1.ListCount) = .ListBox1.List
End With
End Sub

Related

VBA remove ListBox row on click

I have a list box not linked to any range.
I want to click on a row and remove it. If I step through the code below, the ListBox1_Click() function ends up being called twice for some reason and the application produces an "Unspecified Error" on the second run
My entire code:
Private Sub ListBox1_Click()
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub
Private Sub UserForm_Initialize()
For i = 0 To 10
ListBox1.AddItem ("A" & Str(i))
Next i
End Sub
If you make a button about it, then this solution would be quite ok there:
Private Sub CommandButton1_Click()
Dim cnt As Long
For cnt = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(cnt) Then
Me.ListBox1.RemoveItem cnt
Exit Sub
End If
Next cnt
End Sub

excel 2010 vba how can i declare a listbox?

I have a userform which has the following bit of code included:
Private Sub RemoveRecipientCommandButton_Click()
Application.ScreenUpdating = False
Dim intCount As Integer
For intCount = RecipientsListBox.ListCount - 1 To 0 Step -1
If RecipientsListBox.Selected(intCount) Then RecipientsListBox.RemoveItem (intCount)
Next intCount
Application.ScreenUpdating = True
End Sub
This code is run on a listbox which is MultiSelect 1 - fmMultiSelectMulti, and works just fine. The problem comes when I try to allow parameters to be passed to it, so I can use the same sub on more than one ListBox. I've tried:
Private Sub RemoveRecipientCommandButton_Click()
Application.ScreenUpdating = False
RemoveSelected (RecipientsListBox)
Application.ScreenUpdating = True
End Sub
with
Private Sub RemoveSelected(LB As ListBox)
Dim intCount As Integer
For intCount = LB.ListCount - 1 To 0 Step -1
If LB.Selected(intCount) Then LB.RemoveItem (intCount)
Next intCount
End Sub
I've also tried:
Private Sub RemoveSelected(LB As MSForms.ListBox)
and as
Private Sub RemoveSelected(LB As ListObject)
with the rest of the RemoveSelected code being the same. All of these forms of this code throw Error 424 - object required. I'm by no means an Excel pro, so my main concern here is just finding code that works - I want to be able to make this into something I can use on more than one ListBox, if necessary, without having to write the code as new for each ListBox. If someone could even point me in the right direction, I'd appreciate any help I can get. Thanks.
Multiple issues here.
Don't wrap parameters in parentheses calling Subs without Call.
So either Call RemoveSelected(RecipientsListBox) or RemoveSelected RecipientsListBox.
Default modus to hand over parameter is ByRef but that's not possible here. So using ByValis needed.
Correct type is MSForms.ListBox
Code:
Private Sub RemoveRecipientCommandButton_Click()
Application.ScreenUpdating = False
RemoveSelected RecipientsListBox
'Call RemoveSelected(RecipientsListBox)
Application.ScreenUpdating = True
End Sub
Private Sub RemoveSelected(ByVal LB As MSForms.ListBox)
Dim intCount As Integer
For intCount = LB.ListCount - 1 To 0 Step -1
If LB.Selected(intCount) Then LB.RemoveItem intCount
Next intCount
End Sub
Edit:
As #Patrick Lepelletier stated, ByRef is possible in this case. I had the Control object stored in a local variable Set oListboxControl = RecipientsListBox : RemoveSelected oListboxControl what caused the problem with ByRef
So
Private Sub RemoveSelected(LB As MSForms.ListBox)
Dim intCount As Integer
For intCount = LB.ListCount - 1 To 0 Step -1
If LB.Selected(intCount) Then LB.RemoveItem intCount
Next intCount
End Sub
will also work.

Detecting Controls in VB.NET Forms

I wrote the code below to delete control in another VB.NET Form; that workd fine; But the code cannot Detect that the Form has NO Controls; What is wrong with the code please:
Sub DeleteControls() ' WORKING
For i As Integer = Form2.Controls.Count - 1 To 0 Step -1
Dim ctrl = Form2.Controls(i)
ctrl.Dispose()
Next
End Sub
Sub TestForm() ' NOT WORKING
If Form2.Controls Is Nothing Then
MessageBox.Show("Form2 has No Controls")
End If
End Sub
Thanks
First you need to count the controls on form 2."
Dim GetControls As Integer = Form2.Controls.Count"
Then Check if GetControls is smaller then 1 "No controls"
Sub TestForm()
Dim GetControls As Integer = Form2.Controls.Count
If GetControls < 1 Then
MessageBox.Show("Form2 has No Controls")
End If
End Sub

Returning multiselected responses to a Bookmark in MS Word

I have a word document where the user clicks a commandbutton that brings up a userform with a multiselect listbox (listbox1) and a commandbutton (cmnd1) on it. I wish for the user to select multiple items and then click the command button.
This causes the userform to disappear and the selected items to appear as a string at a bookmark in the MS word document (bkmrk1a).
I can do this easily with single select. I am getting tripped up with multiselect.
Here is the code I am using:
`Option Explicit
Private Sub UserForm_Initialize()
Dim i, Str As String
Str = "Rating1,Rating2,Rating3,Rating4,"
For i = 0 To UBound(Split(Str, ","))
ListBox1.AddItem Split(Str, ",")(i)
Next
End Sub
Private Sub CommandButton1_Click()
Selection.Text = ListBox1.Value
Application.ScreenRefresh
End Sub
Any help would be appreciated. I am new to VBA.
Mark
To enable multiselect on a list box, simply set the .MultiSelect property to either fmMultiSelectMulti or fmMultiSelectExtended. The later allows you to use the shift key to select multiple items. ie in the initialization:
ListBox1.MultiSelect = fmMultiSelectExtended
Then when you click the commandbutton, you need to loop through the list items and check if they have been selected. ie:
Private Sub CommandButton1_Click()
Dim ii As Integer
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
Selection.Text = ListBox1.List(ii)
Selection.MoveRight
Selection.TypeParagraph
End If
Next ii
Application.ScreenRefresh
End Sub

Self-referencing from inside an Excel VBA control

I'm trying to get a property value of a button control from inside the button's click event without using the button's name (since I want to use the same code for each of many buttons on the Excel sheet).
After much research, I see many references to try the following:
Me.ActiveControl.name
or
Me.Shapes(Application.Caller).Name
However, both of those throw an error when executed from within Excel. Note that I'm using Excel 2010.
Thanks for any help in advance.
Lee
What you want is possible but for that you need to create a Class
Do this.
Insert a Class Module and paste this code there.
Option Explicit
Public WithEvents MyButton As MSForms.CommandButton
Private Sub MyButton_Click()
MsgBox MyButton.Name
End Sub
Next Insert a module and place this code there
Dim shpButtons() As New Class1
Sub StartCode()
Dim shp As Shape
Dim btnCount As Long
ReDim shpButtons(1 To 1)
btnCount = 0
For Each shp In ActiveSheet.Shapes
If shp.OLEFormat.Object.OLEType = xlOLEControl Then
btnCount = btnCount + 1
ReDim Preserve shpButtons(1 To btnCount)
Set shpButtons(btnCount).MyButton = shp.OLEFormat.Object.Object
End If
Next
End Sub
Sub StopCode()
Dim iBtn As Long
On Error Resume Next
For iBtn = LBound(shpButtons) To UBound(shpButtons)
Set shpButtons(iBtn).TheText = Nothing
Next
End Sub
Now simply run the Sub StartCode()
Next when you click the ActiveX CommandButton then you will get it's name.
Try ActiveSheet.Shapes(Application.Caller).Name