I have input a combobox in an Excel sheet. I want it to work so that the user who does not have access to the VBA can select a value from the dropdown and then the value in another cell will perform a vlookup on this value.
In the first instance I have inserted a box and am trying to set a cell value based on this.
Sub InsertComboBox()
#inserts dropdown box on the front page and sets the values as the list of DMA from the pipe_totals sheet
#this should be the most complete list so will not change dependant on asset
Dim arearange As Range
Set arearange = Sheets("pipe_totals").Range("a:a")
lastrowi = Application.WorksheetFunction.CountA(arearange)
Sheets("front page").Activate
With Range("f5:g5")
Set Combo = ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height)
End With
Combo.List() = Sheets("pipe_totals").Range("A2:A" & lastrowi).Value
.Range("k9").Value = Combo.Value 'only works on current combobox value which is 0
End Sub
Is there a way I can set this so the vlookup is dynamic depending on the users selection?
In this example, just set the right combo name. It should be ok, provided that your combobox lists values from "Range("A2:A" & lastrowi)" as you mention above.
Sub "comboname"_Change()
Dim list_val As Long
list_val = Worksheets("front page").Shapes("comboname").ControlFormat.Value
Range("K9") = Worksheets("pipe_totals").Cells((list_val + 1), 1)
End Sub
Sub test()
Dim z As Shape
For Each z In Worksheets("front page").Shapes
Debug.Print z.Name
Next z
End Sub
As far as I understand, you want that everytime the combobox value changes, cell K9 will have the same value also. Is that right? If this the case, then right click on the combobox and select "Assign Macro". Then select "Create". Then inside the sub created, which should look like this:
Sub "comboname"_Change()
End Sub
You should also paste the final code line.
.Range("k9").Value = Combo.Value
Doing so, means you want that line of code executed every time the combobox value changes.
Related
I have a userform within a userform (userform-ception) where I implement a search function for part list in Row B. Prior to searching I want to check for duplicates, which triggers a second userform when true. I am having trouble populating userform2's list box when it initialises. My logic is
If duplicate count > 1, filter by column B for that search criteria
Open second userform
From the current visible cells, grab the first 7 columns and populate a list box with this (Using visible cells as array)
However when I try to open the userform it turns out blank so I am wondering if anyone can shed some light on this.
This is in my Search Function
Dim Dupl As Integer
Dupl = Application.WorksheetFunction.CountIf(Range("B:B"), PartNumberSearch.Text)
If Dupl > 1 Then
With ActiveSheet.Range("A1:M1")
.AutoFilter Field:=2, Criteria1:=PartNumberSearch
End With
UserForm2.Show
'Exiting the sub here so that when userform 2 is used to disambiguate the part it can continue to search using PartID instead
Exit Sub
This is in my userform2_initialise() function
Private Sub UserForm2_Initialize()
DupList.ColumnWidths = "35;50;60;25;10;20;40"
'Checks last row of the list
DupRow = ActiveSheet.Range("A65536").End(xlUp).Row
'Puts all the result into the ResultsList
DupList.List = Range("A2:G" & DupRow).Value
End Sub
A sample snippet of my table
Any help is appreciated. Thanks!
I am trying to add a spin button to increment a value where ever the data is present.
I manually added a spin button for one field to increment the value. How can add spin buttons where ever a the value is present. Something like shopping cart.
Private Sub quantity_SpinDown()
Range("M6").Value = Range("M6") - 1
End Sub
Private Sub quantity_SpinUp()
Range("M6").Value = Range("M6") + 1
End Sub
I am expecting this spin button where ever there is an item in cart is present to increment the value of the item in the cart.Check Image here
You can create a macro to dynamically create spinners. In the example bellow you can select some cells in your worksheet and then execute the following Sub:
Option Explicit
Sub createSpinnersInSelection()
Dim spinner As OLEObject
Dim selectedCell As Range
For Each selectedCell In Selection
' Creates a spinner
Set spinner = selectedCell.Parent.OLEObjects.Add(ClassType:="Forms.SpinButton.1", Top:=selectedCell.Top, Left:=selectedCell.Left, Height:=selectedCell.RowHeight, Width:=15)
' Setting spinner properties
With spinner
' Cell to change the value
.LinkedCell = selectedCell.Address(0, 0)
With .Object
' Initial value
.Value = 0
' Increment of cell
.SmallChange = 1
End With
End With
Next selectedCell
End Sub
You may add more properties such as spinner color and others (the code was adapted from this thread)
Important note: You may need to tweak a little bit the .LinkedCell value since when you filter the columns it may keep the original cell reference, changed the value in a different cell than the expected one.
I've added a button to an Excel file which, when clicked, reads a text file and populates a column with lines from a text file. I need to add a checkbox to cells adjacent to certain lines, depending on what the line contains.
Can I create components like checkboxes in code, and if so, how?
Any responses are appreciated.
While the link #Siva provided is certainly valid I just prefer to have an answer on StackOverflow instead of an external link. Hence, here is the solution you might be looking for:
Option Explicit
Public Sub tmpSO()
Dim i As Long
Dim chk As CheckBox
With ThisWorkbook.Worksheets(1)
.CheckBoxes.Delete
For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
If .Cells(i, "A").Value2 = "need checkbox" Then
Set chk = .CheckBoxes.Add(Left:=.Cells(i, "B").Left, Top:=.Cells(i, "B").Top, Width:=.Cells(i, "B").Width, Height:=10)
chk.OnAction = "runThisSub"
chk.Name = "CheckBowInRow" & i
chk.Caption = "CheckBowInRow" & i
End If
Next i
End With
End Sub
Sub runThisSub()
MsgBox "You clicked the checkbox " & Application.Caller _
& Chr(10) & "in cell " & ThisWorkbook.Worksheets(1).CheckBoxes(Application.Caller).TopLeftCell.Address
End Sub
Copy both subs into a Module into your Excel file and change in the first sub
the sheet where the text is imported to (here it is Worksheet(1)),
the column where the condition can be found (here column A), and also
what the condition is (here the value in column A must be need checkbox).
The code will now look through all cells in column A in sheet Worksheet(1) and check if the value is need checkbox. If so, the code will automatically add a checkbox into column B adjacent to that cell.
If you click on any of the newly created checkboxes then the second sub fires up and will show you in a message box which checkbox in which row has been clicked.
I am using a userform with multiple checkboxes in it. My goal is to have all checkboxes return their values (unique two letter acronyms) to a single cell. I am not sure how to accomplish this. I have no VBA experience prior to today.
You can record a macro to get your other relevant code for VBA but this should help:
Range("A" & LastRow).Value = CheckBox1.Value & CheckBox2.Value & CheckBox3.Value 'etc.
'Concatenate CheckBox values with "&" (the values will be True or False though)
Set linkcell of each checkbox to a different cell -.e.g. checkbox1 to cell A1, checkbox2 to cell A2, etc
For each link cell, use If formula in its adjacent cell to test the link cell's content and return a string or a blank,
e.g. if(A1=True,"AA","") in cell B1, if(A2=True,"BB","") in cell B2, etc
Finally, concatenate results of 'if' Formulas in another cell: e.g. B1&","&B2&","&B3, using comma as separaters
To do this, you can make a collection of all of the Checkboxes in the form. Heres my code:
Private mcolCHK As Collection 'This creates a modular level collection, which will keep track of all of the checkboxes
Private Sub cmdOK_Click()
Dim objA As Object, strResults As String, i As Integer
i = 1
For Each objA In mcolCHK
strResults = strResults & i & Left(objA.Value, 1)
i = 1 + i
Next
Unload frmTestForm 'rename this the name of your form
MsgBox strResults
'instead of using a message box, you can say "strResults = Sheets(sheet1).range("A1").value
'This part may be customized as needed
End Sub
Private Sub UserForm_Initialize()
Set mcolCHK = New Collection
mcolCHK.Add chkAtt1 'you will have to add one of these for each checkbox (simply replace "chkAtt1" with the name of your first checkbox
mcolCHK.Add chkAtt2 'and repeat for each checkbox
End Sub
Checkboxes always give a value of True or False. with this method, you will get the number of the checkbox (in order of how you added them to the collection in the UserForm_Initialize() sub) and the first letter of the result (T or F). When i ran this, I had two checkboxes, and checked each one when the form ran. This gave me a result of "1T2T". This can be changed as needed. All you really need is how this all works. just make sure to change all the names to match the names of the variables you used.
Good morning,
I am in yet another rut and need some help. I have created a user form that allows a user to delete an entire rows worth of data on a second sheet (rawdata). Everything works fine using the code below, however the combo box ONLY shows the row number. I am in desperate need of changing the column so it will show the project names of the rows that need to be deleted.
Example:
Row: Project
1 Alpha
2 Beta
I would like the combo box to show Alfa and Beta and have the user be able to select the row they would like to delete based on that criteria.
The code below unhides and then hides the sheet that I want this deletion to occur on. This was done with purpose.
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim lRw As Long
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetVisible
'get the row number. add 1 because ListIndex starts at zero
lRw = Me.ComboBox1.ListIndex + 1
ActiveWorkbook.Sheets("RAWDATA").Select
Cells(lRw, 1).EntireRow.Delete
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetHidden
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Initialize()
'assumes data starts in A1 and has a header row
Me.ComboBox1.List = ActiveWorkbook.Sheets("RAWDATA").Cells(1, 1).CurrentRegion.Offset(1).Value
End Sub
Thanks for the help!
Change .Cells(1, 1) to .Cells(1, 2)
The Cells() method gives the code co-ordinates to a specific range using the row and the column number like so:
Cells(r, c)
so in your original code, the .Cells(1, 1) points to "A1" and then uses .CurrentRegion to get all cells within the region of A1.
By replacing the column number and using .Cells(1, 2) we tell it to look at "B1" instead - therefore shifting the column over to the right.
EDIT:
You could apply this logic to the Offset(r, c) function to shift the returned value over by 1 column - so:
.Cells(1, 1).CurrentRegion.Offset(1, 1)
This will more than likely be the culprit as the .Cells() method will point to a specific cell, but the .CurrentRegion() method will return the same result regardless unless we offset it.