I'm having some difficulty finding a workable solution (been looking for 2 days now).
Hopefully you can help me figure it out.
Purpose - I'm trying to use VBA to drag and drop text between listboxes
(see image)
Note: I know there are Pivot Wizards already, I'm not so interested in them (long story)
Question
Is there any solution that you know of that could help me move "Column A" to any of the other listbox?
If you don't know of a solution, a blog or site might be helpful as well.
Further to my comments above here is the most simple way to do it.
Create a Userform with 2 Listboxes and 1 Command Button as shown in the below image.
And paste this code in the Userform Code area
Dim i As Long
Private Sub UserForm_Initialize()
For i = 1 To 10
ListBox1.AddItem i
Next i
End Sub
Private Sub CommandButton1_Click()
If ListBox1.ListIndex = -1 Then
MsgBox "Please select an item from listbox1"
Exit Sub
End If
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub
HTH
Related
I want to use VBA code to pull up my form. Right now the code is causing a runtime error on Sub UserForm_Initilize(). I don't know a lot about VBA. I know I might need to do something like column.selectAll but I am not sure assume I am trying to select the first 4 columns for my form. Thanks
Sub UserForm_Initialize()
Me.TextBox1.Value = Sheet1.Range("H" & Rows.Count).End(xlUp).Value
End Sub
Sub Rectangle1_Click()
UserForm1.Show
End Sub
I am writing code for a button in Excel with the intention of taking the row number of the clicked button, duplicate the corresponding row given the row number, and then inserting and shifting the duplicated row below the original row of the click button. See the pictures for example:
Click here for picture before button is pressed.
Click here for picture after button is pressed.
I have tried several revisions of code based on solutions to similar issues I find online such as here and here(among other places), but I cannot find a solution to my reoccurring error which is Unable to get the Buttons property of the Worksheet class. From what I've gathered online about this error, it occurs often when any argument passed to the worksheet function is not of the correct type or simply doesn't make sense. Below I posted two iterations of my codes;
Private Sub CorrugatedR_Click()
Dim b As Object, RowNumber As Integer
ActiveSheet.Activate
Set b = ActiveSheet.Buttons("CorrugatedR")
With b.TopLeftCell
RowNumber = .Row
Rows(RowNumber + 1).Insert Shift:=xlDown
Rows(RowNumber).Copy Rows(RowNumber + 1)
End With
End Sub
The other version, which should do the same thing(I've been doing a lot of playing around):
Private Sub CorrugatedR_Click()
Dim b As Object, RowNumber As Integer
ActiveSheet.Activate
ActiveSheet.Buttons("CorrugatedR").Select
ActiveSheet.Buttons("CorrugatedR").Copy
b.Paste
With b.TopLeftCell
RowNumber = .Row
End With
Rows(RowNumber + 1).Insert Shift:=xlDown
Rows(RowNumber).Copy Rows(RowNumber + 1)
End Sub
A quick important note is that originally instead of calling the button's name itself: ActiveSheet.Buttons("CorrugatedR") I implemented ActiveSheet.Buttons(Application.Caller) instead because several people made this suggestion, but this gave me another error Application.Caller = Error 2023. After doing research I think these problems both relate to the same reoccurring issue that ether it's relating to typing or something i'm unaware of; I tried implementing and using the information from the solution to the same error found here, and still no luck. My intuition is that the issue may lie in the code where I set b: Set b = ActiveSheet.Buttons("CorrugatedR")
I'm still new to VBA and Excel, so my whole approach could be miscued in itself, and I would really appreciate any help, I've been stuck on this one for a while.
If your code is in a module, this sample code will work (replace CommandButton1 with the name of your button):
Private Sub example()
MsgBox ActiveSheet.CommandButton1.TopLeftCell.Row
End Sub
...Or if your code is on the sheet:
Private Sub CommandButton1_Click()
MsgBox Me.CommandButton1.TopLeftCell.Row
End Sub
I have been trying to code my mutlipage userform to select a certain worksheet based on the multipage section. I seem to be stuck there. Would someone happen to know the correct way to fix this problem? this is my last attempt that did not work.
Thank You,Steve
Private Sub MultiPage1_Change()
Select Case MultiPage1.Value
Case Page0
Workbooks("Enova")Sheets.Activate
End Select
End Sub
You are very close to the solution. MultiPage.Value returns the index of the page, so 0, 1, 2, etc. Use the below. You will need to specify, as per #Tim Williams comment, which workbook/sheet you want to select.
Private Sub MultiPage1_Change()
Select Case MultiPage1.Value
Case 0
Workbooks("Enova").Sheets(1).Activate 'This may not be correct, need more information
End Select
End Sub
Cant seem to find the answer to this, I need a combobox (which is inside a userform) that has three selection
eg Rental,Purchase and Finance
Which the selected input is then put into example cell A1. I know how to do this if I have the list already on my excel sheet, but I don't want the list on the excel sheet only the selection. so I suppose I'm asking for a coded list of option inside the module which then outputs the result in to a cell.
Any help would be appreciated (I'm a Newbie)
Thanks
Basic code in the userform:
Private Sub ComboBox1_Change()
If me.combobox1.listindex > -1 then sheets("Sheet name").Range("A1").value = me.combobox1.Value
End Sub
Private Sub UserForm_Initialize()
Me.ComboBox1.List = Array("Rental", "Purchase", "Finance")
End Sub
I am new to Visual Basics (2 days so far), and the only other programming I've done is MATlab.
I am trying to have the program print different numbers in different columns in Excel, depending if a checkbox in a userform is checked or not. There are 26 checkboxes in total - along with some textboxes - and I'm trying to use a For Each Control loop to run through all the checkboxes.
I've looked up a few tutorials and some forums, but when I try to run a test, the code doesn't work. More specifically, no errors show up but the "Testing if it Works?" is not printed anywhere.
Private Sub Add_Button_Click()
Dim Ctrl As Control
For Each Ctrl In DataInput.Controls
If TypeName(Ctrl) = "Checkbox" Then
If Ctrl.Value = True Then
Sheets("Data").Range("A1") = "Testing if it Works?"
End If
End If
Next
End Sub
I've followed the same setup as all the other forums or tutorials I've come across, but nothing seems to be happening. Any advice is appreciated.
EDIT: This answer only applies when putting checkboxes directly on the Excel sheets, not in a user form as was asked. Feel free to downvote!
I played around with it and it was not entirely simple at all. It's a bit different whether you're using Form controls or ActiveX controls. The code below works for me, though I'm not entirely sure they're the best ways.
With ActiveX controls:
Private Sub Add_Button_Click()
For Each Ctrl In ActiveSheet.OLEObjects
If TypeName(Ctrl.Object) = "CheckBox" Then
If Ctrl.Object = True Then
Sheets("Data").Range("A1") = "Testing if it Works?"
End If
End If
Next
End Sub
With Form controls
Private Sub Add_Button_Click2()
For Each Item In Me.Shapes
If Item.FormControlType = xlCheckBox Then
If Item.DrawingObject = 1 Then
Sheets("Data").Range("A1") = "Testing if it Works?"
End If
End If
Next
End Sub