calling data from worksheet to the userform - vba

so i have a ComboBox(ComboBox1) and from this ComboBox I would put a SO#(SalesOrder#) and click on an update button, when i click on this, the following fields would fill up according to the SO#(SalesOrder#) : TextBox1, ComboBox2, TextBox2, TextBox8, ComboBox5, TextBox4, TextBox5, ComboBox3, ComboBox4, ComboBox6, and TextBox7.
The Data needed are on the worksheet: Orders Database
The Data for SO#(SalesOrder#) is on column A and are arranged according to the input above.
I don't have a sample code yet since i still have no idea on how to do this
so basically, i want to fill up the Other Fields according to the SO# field(ComboBox1) and use it as a searchbox for my database.

Asign a value to a text box
Dim OrderNumber as Range
Set OrderNumber = ActiveSheet.[A1]
Me.MyTextbox = OrderNumber 'define a range inside VBA code
' or
Me.MyTextbox = ActiveSheet.[A1] ' use absolute cell addressing
' or
Me.MyTextbox = Range("MyWSRange").Cells(1,1) 'use a range defined in worksheet
With a ComboBox basically you can do the same. If you want to include the displayed value into the ComboBox DownDown list as well, you need to add it as an item
Me.ComboBox1.AddItem ActiveSheet.[A1]

Related

Dynamic Range and one static Item to a ComboBox via VBA

I have this code:
With Sheet1.Shapes("comboBox1").ControlFormat
.ListFillRange = "namedRange"
.AddItem "1.Item"
End With
But after that, just "1.Item" is in my Combo-Box and the dynamic range don't appear at all.
How can I add one Item and my Range to the Combo-Box?
EDIT
The dynamic range would work if I delete .AddItem:
With Sheet1.Shapes("comboBox1").ControlFormat
.ListFillRange = "namedRange"
End With
My Question is if there is a possibility to combine those to not in a range, but rather separated from each other.
Thank you very much in advance for your answers...
If I understood your post correctly, you want to add another Item to the items in your "namedRange" and show all these items in your worksheet ComboBox (which is actually a drop-down in your worksheet).
(modify "Sheet2" to your sheet's name).
Sub PopulateCombo_fromArray()
Dim ComboArray As Variant
'clear Combo-Box from previous runs >> modify "Sheet2" to your sheet's name
Worksheets("Sheet2").Shapes("ComboBox1").ControlFormat.RemoveAllItems
' reading the NamedRange into a 1-dimension array
ComboArray = Application.Transpose(Range("namedRange").Value)
ReDim Preserve ComboArray(UBound(ComboArray))
' add another element to the array (outside the "namedRange")
ComboArray(UBound(ComboArray)) = "1.Item"
' populate "ComboBox1" with array
Worksheets("Sheet2").Shapes("comboBox1").ControlFormat.List = ComboArray
End Sub
Must you use "Shapes"?
if not, you can fill a combobox with a named range like this :
With Sheet1.ComboBox1
.List = Application.Transpose(Range("namedRange"))
.AddItem "1.Item"
End With

Adding combo box across multiple cells

I need to add combo box(ActiveX Control) or Data Validation as drop down list.
I have a range of 15 values like, high, low, medium,etc...
Have created named range called "priorityvalue".
I can create a dropdown list using combo box by adding named range under ListFillRange in the properties or data validation list by giving named range.
But my concern, I need to dropdown list for 58cells with same values mentioned above. Its tedious job to create combo box for all cells. Please suggest me better option here.
Data validation list serves the purpose. However, it makes user to scroll through dropdown list on each cell unlike combo box it has no input box..
Please suggest
Paste the below code in 'ThisWokbook'
Private Sub Workbook_Open()
Dim oItem As Object
For Each oItem In Worksheets(1).OLEObjects
If TypeName(oItem.Object) = "ComboBox" Then
If Len(oItem.Object.Value) > 0 Then
oItem.Object.Value = ""
End If
End If
Next
Set oItem = Nothing
End Sub
NOTE: There are caveats to this. Above code will reset all comboboxes in your worksheet (also, I've set the worksheet to the first worksheet in the workbook, you might want to make that dynamic). If you don't want it to reset all comboboxes and only do the ones you added via the function, you can use the name format to filter the ones you want to clear
Hope this helps
Try this:
Sub AddComboBoxToColumns(ByVal oRange As Excel.Range)
Dim oOLE As OLEObject
Dim oCell As Object
' Loop through all the cells in the range
For Each oCell In oRange.Cells
' Add ComboBox in each cell
With oCell
Set oOLE = .Parent.OLEObjects.Add("Forms.combobox.1")
oOLE.Top = .Top
oOLE.Left = .Left
oOLE.Width = .Width
oOLE.Height = .Height
oOLE.Name = "ComboBox" & .Address(False, False)
oOLE.Object.List = Array("Test1", "Test2")
End With
Next
Set oOLE = Nothing
End Sub
NOTE: Call the above function with the range of cells you want to add ComboBox to. You will have to change the Array to use the values you want (you can type them in there or give the range where your existing values are)

VBA - Listbox and Data

I have a listbox called listbox1 that shows me the data in sheet1 but the problem is that it doesn't show me all of the data sometimes i have 3000 line of data and it shows me anlo first 50 line this is the code i am using if someone can help please :
ListBox1.List = Sheets("Sheet1").Range("A1:F3000").Value
if you need datas from one column only then you'd go:
ListBox1.List = Application.Transpose(Sheets("Sheet1").Range("A1:A3000")) '<== change "A"s in Range("A1:A3000") to wanted column index
otherwise if you want to list data from more columns you have to go like this:
Private Sub UserForm_Initialize()
Dim Data As Variant
...
Data = Sheets("Sheet1").Range("A1:F3000")
With UserForm1.ListBox1 '<== change it with actual UserForm and ListBox names
.ColumnCount = 2
.List = Data
End With
but you'd be able to select one or more (up to ListBox MultiSelect property setting) entire row, and not single "cells".
Try changing the properties of the listbox to the range you need. Then change ColumnCount to 6.
Ie:
To make the RowSource dynamic, input 6 on the ColumnCount proterty and delete the range in the RowSource property and add this code to the form.
Private Sub UserForm_Initialize()
ListBox1.RowSource = Sheets("Sheet1").Range("A1:F3000").Address(external:=True)
End Sub

DataGridView Change cell to ComboBox, Edit, then return cell to TextBox with selected value

I've found another topic on this site, similar to my question, "Change Cell to Combobox on focus and TextBox after loosing focus" but no answer has been given to date.
I have a DataGridView with a "Start Odometer" column, an "End Odometer" column and a "Units" column. They are populated through Bindings from a datatable.
I want to be able to change the units of selected records and convert the Odometer column values to the selected "Unit". (either miles to kilometers or visa versa).
I've figured out how to change and load the "Units" cell to a ComboBox cell. Here is the code.
Dim gridComboBox As New DataGridViewComboBoxCell
With DataGridView1
If .Rows.Count = 0 Then Exit Sub
Dim i = DataGridView1.CurrentRow.Index
Dim celval = DataGridView1.CurrentCell.Value
gridComboBox.Items.Add(celval) 'Populate the Combobox
gridComboBox.Items.Add("km") 'Populate the Combobox
gridComboBox.Items.Add("mi") 'Populate the Combobox
.Item(GridEnum.State, i) = gridComboBox
End With
Here is where I run into problems.
I need to change this cell back to TextBox. I've basically used the same code as above substituting DataGridViewTextBoxCell.
Dim gridTextBox As New DataGridViewTextBoxCell
With DataGridView1
If .Rows.Count = 0 Then Exit Sub
Dim i = DataGridView1.CurrentRow.Index
Dim celval = DataGridView1.CurrentCell.Value
gridTextBox.value = celVal
.Item(GridEnum.State, i) = gridTextBox
End With
This last piece of code works if I call it as a separate procedure but it doesn't work if I run it from inside the ComboBox_SelectedIndexChanged procedure. (EditingControlShowing is enabled).
This makes sense as the ComboBox is still active.
The question... How can I revert cell ComboBox back to cell TextBox once the ComboBox Item index has changed? I need this event to trigger my Odometer values conversion code as well.

Get the selected value of a activex combobox using vba

How do i get the selected value of the combobox?
i have a combobox which has the values: "Corporate" and "Consumer".
I want to get the value that i selected, not the index, and store in a string.
something like this:
string a = combobox.value;
(a -> Consumer)
thank you
If your ComboBox is embedded in a spreadsheet you can use this:
Dim ws as Worksheet
Dim cboCorpConsumer as ComboBox
Dim a as String
Set ws = Worksheets("YourWorksheetName")
Set cboCorpConsumer = ws.OLEObjects("cboNameFromActiveXProperties").Object
a = cboCorpConsumer.Value
Or in one line:
a = Worksheets("YourWorksheetName").OLEObjects("cboNameFromActiveXProperties").Object.Value
Value has a capital "V" in VBA, but assuming combobox is the name of the ComboBox you created on the screen, the code you have will work (except that your assignment statement is wrong; see below). If you don't know what the name of the ComboBox is, it is likely ComboBox1. To check, look at the Name property in the VBA properties window.
Try this:
Dim a as String
a = combobox.Value