I am creating an userform where user can select existing materials, and input stock quantity.
Process:
Userform open
User will select which stock column to input values (Stock1 ~ Stock10).
User will select a material on the listbox.
User can input stock quantity & select where the stock is from.
When updating, the stock quantity will be added on the selected Stock column.
I got to the part on populating the listbox; and label showing selected material in the listbox to show name & color.
Stuck at:
I am stuck on how to make:
selected stock column to be input column.
*this is the part which user will use combobox to select available stock column on the left chart.
selected listbox material to be input row.
Example:
Below are the images of the example form & userform.
Left image is where the stock data will be input (Stock1 ~ Stock3).
Right image is the userform; user will follow the process above; and it will be entered into the left chart.
Thank you all in an advance.
If I understand you correctly ....
From the animation above, when two condition is met : the ListBox for name is selected (based on what user choose) AND the ComboBox for Stock is selected (based on what user choose), then :
the cell where the user want to update the qty is activated
the textbox for QTY is filled with the active cell value
If the user change/update the value in the textbox for QTY then he click UPDATE button, the activecell value will be the textbox value.
So if your case is similar with the animation above, then maybe you want to have a look the code below which maybe you can implement it to your case :
Private Sub UserForm_Initialize()
Set rg = Range("A2", Range("A" & Rows.Count).End(xlUp))
Set rg = rg.Resize(rg.Rows.Count, 3)
'populate the ListBox for id, name and color
With ListNameColor
.ColumnCount = 3
.ColumnWidths = "0,40,40"
.List = rg.Value
End With
'populate the combobox for stock
For i = 1 To 3: cbStock.AddItem "Stock" & i: Next
End Sub
Private Sub cbStock_Change()
If ListNameColor.ListIndex <> -1 Then Call PopQty
End Sub
Private Sub ListNameColor_Click()
If cbStock.ListIndex <> -1 Then Call PopQty
End Sub
Sub PopQty()
'get the row and column as r and c variable
r = Range("A2", Range("A" & Rows.Count).End(xlUp)).Find(ListNameColor.Value).Row
c = Rows(1).Find(cbStock.Value, lookat:=xlWhole).Column
Cells(r, c).Activate
tbQty.Value = ActiveCell.Value
End Sub
Private Sub btUpdate_Click()
ActiveCell.Value = tbQty.Value
End Sub
Private Sub btCancel_Click()
Unload Me
End Sub
Please note that the item name in the combobox for stock must be exactly the same as the header name for the stock. For example : if in the header for stock the name is : STOCK-01, STOCK-02, STOCK-03, and so on, then when populating the combobox for stock must also with the same text.
Debugging the PopQty sub :
Sub PopQty()
Dim r As Integer: Dim c As Integer
'debugging
SelectedNameID = ListNameColor.Value 'is the SelectedNameID value correct in the Locals window?
'for example if the selected name is "bbb", then the value of SelectedNameID must be 2.
Set rgData = Range("A2", Range("A" & Rows.Count).End(xlUp))
rgData.Select 'is the selection correct ?
'it should select the "#" column from row 2 to the last row with number.
Set foundCell = rgData.Find(SelectedNameID)
'is the Locals window shows that the foundCell variable is NOT nothing ?
'if the foundCell variable in Locals window is not showing "Nothing" ....
foundCellRow = foundCell.Row 'is the foundCellRow value correct in the Locals window ?
'for example if the selected name is "bbb", then the foundCellRow value must be 3.
'get the row and column as r and c variable
r = Range("A2", Range("A" & Rows.Count).End(xlUp)).Find(ListNameColor.Value).Row
c = Rows(1).Find(cbStock.Value, lookat:=xlWhole).Column
Cells(r, c).Activate
tbQty.Value = ActiveCell.Value
End Sub
Related
Wondering if someone can help me reverse the below code. Essentially, I have a userform with a combobox that generates from a list of names from a worksheet column "A". Upon submit the selected items from userform are populated to the worksheet to the row of the corresponding name from the combobox.
I am hoping to somehow reverse the code below so I can place it in "UserForm_Initialize()" to regenerate saved values back to the texboxes on the form if user closes and reopens the same day. I have a current date textbox called "currentDate". So basically if Date = currentDate.Text Than...add cell value back to textboxes.
Dim dn As Worksheet: Set dn = Sheets("DailyNumbers")
Dim EmptyRow As Long
Dim FoundVal As Range
EmptyRow = dn.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
' *** Check combobox selection ***
If procNamecombobox.ListIndex > -1 Then
Set FoundVal = dn.Range("A1:A" & EmptyRow).Find (procNamecombobox.Value) 'find Combobox value in Column A
If Not FoundVal Is Nothing Then 'if found
dn.Range("B" & FoundVal.Row).Value = currentDate.Text
dn.Range("C" & FoundVal.Row).Value = completeCount.Text 'use that row to populate cells
dn.Range("D" & FoundVal.Row).Value = handledCount.Text
dn.Range("E" & FoundVal.Row).Value = wipCount.Text
dn.Range("F" & FoundVal.Row).Value = suspendCount.Text
Else 'if not found use EmptyRow to populate Cells
dn.Range("A" & EmptyRow).Value = procNamecombobox.Value
dn.Range("B" & EmptyRow).Value = currentDate.Text
dn.Range("C" & EmptyRow).Value = completeCount.Text
dn.Range("D" & EmptyRow).Value = handledCount.Text
dn.Range("E" & EmptyRow).Value = wipCount.Text
dn.Range("F" & EmptyRow).Value = suspendCount.Text
End If
Else
MsgBox "Please select your name"
End If
Thank you!
I guess you could use something like this
Option Explicit
Private Sub UserForm_Initialize()
Dim f As Range
With Worksheets("DailyNumbers") 'reference wanted sheet
Set f = .Range("B1", .Cells(.Rows.Count, "B").End(xlUp)).Find(Date, lookat:=xlWhole, LookIn:=xlValues) 'search referenced sheet column B for current date
End With
If Not f Is Nothing Then ' if current date found
With Me 'reference userform
.completeCount.Text = f.Offset(, 1).value
.handledCount.Text = f.Offset(, 2).value
.wipCount.Text = f.Offset(, 3).value
.suspendCount.Text = f.Offset(, 4).value
End With
End If
'your other code to fill listbox
With Worksheets("NamesArchive") ' just a guess...
Me.procNamecombobox.List = Application.Transpose(.Range("A1", .Cells(.Rows.Count, "A").End(xlUp))) 'fill combobox with referenced sheet column A values from rows 1 down to last not empty one
End With
End Sub
BTW, your code could be refactored as follows:
Option Explicit
Private Sub CommandButton1_Click() ' just a guess...
Dim dn As Worksheet: Set dn = Sheets("DailyNumbers")
Dim emptyRow As Long
Dim foundRng As Range
With Me
With .procNamecombobox
If .ListIndex = -1 Then
MsgBox "Please select your name"
Exit Sub
End If
emptyRow = dn.Cells(dn.Rows.Count, "B").End(xlUp).Row + 1
Set foundRng = dn.Range("A1:A" & emptyRow).Find(.value) 'find Combobox value in Column A
If foundRng Is Nothing Then 'if no entry with input name
dn.Range("A" & emptyRow).value = .value 'fill column A first empty with input name
Else 'otherwise
emptyRow = foundRng.Row 'set found cell row index as the one to write in
End If
End With
Intersect(dn.Range("B:F"), dn.Rows(emptyRow)).value = Array(.currentDate.Text, .completeCount.Text, .handledCount.Text, .wipCount.Text, .suspendCount.Text) 'fill columns B to F proper row with textboxes values
End With
End Sub
To help get you started:
A)
Determine if there is a cell in column B with the current date. If so, locate it and use the .Row property to save the row number to a variable.
(There are a couple of range functions (.Find, .Search) that you can use to locate a cell with a particular value. For date's, this link has some helpful information.)
A.5) From the above link, if the dates are in Excel as serial dates -- not text -- then you can use
Set FoundCell = Range("A1:A100").Find _
(what:=Date,lookin:=xlFormulas)
to find the current date in column A from rows 1 to 100. VBA has a function Date() which returns the current day's date. Now() returns the current date and time, while Time() returns the current time.
B)
Set the .text values of the Text/Combo boxes to the values of the cells
(These can be located with a concatenation of the correct column with the saved row variable from earlier. Similar to how you located the cells to save the values initially)
If you're stuck on how to do a particular step or process, and can't find an existing Q&A with information, you can ask for elaboration.
I have a sheet called "lookup" and in cell B2 of that sheet, users can input a department code (ex: 190) and select a "Search" button:
Once they hit the search button it will take them to a sheet called "department_lookup" which in column A has account codes with the department code 190 in it and in column B has the account code description. In cell C1, though, I want to have the value being searched for populate cell C1 that way the query in sheet "department_lookup" can refresh to show proper data. Here is what sheet "department_lookup" looks like:
In column A & B would be a list depending on how many account codes have the department code 190 in it.
Essentially, the data in sheet department_lookup is a dynamic query and I'd like cell C1 to be the parameter value that alters the query to display the account codes that the user is searching for in cell B2 in the sheet lookup.
Here's the code I have for sheet lookup:
On Error GoTo Done:
If Target.Column = 2 And Target.Cells.Count = 1 Then
Cancel = True
mycell = Sheets("department_lookup").Range("$C$1").Value
If mycell = " " Then GoTo Done:
Sheets("department_lookup").Activate
End If
Sheets("acct_codes").Visible = False
Sheets("dept_list").Visible = False
Cancel = True
Application.ScreenUpdating = True
End Sub
Here is the vba i have for sheet department_lookup:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$1" Then
With ActiveWorkbook.Connections("deptlookup").OLEDBConnection
.CommandText = "select seg1_code+'-'+seg2_code+'-'+seg3_code+'-'+seg4_code as account_code, account_description from glchart as GL where GL.inactive_flag = 0 and seg2_code='" & Range("C1").Value & "' order by seg1_code"
End With
ActiveWorkbook.Connections("deptlookup").Refresh
End If
End Sub
Currently, when I manually change the value of cell C1 to a different department code, the query in department_lookup will change to display the proper codes but, I think my issue is properly setting C1 to equal whatever the user searched for in cell B2 in sheet lookup. Can anyone help out with this?
Set that sheet's c1 to equal the cell of your initial sheet upon click of the submit button, then filter per C1 such as:
Sheets("department_lookup").Cells(1, 3).Value = Sheets("lookup").Cells(2, 2).Value
With Sheets("department_lookup")
.Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=.Cells(1, 3).Value, VisibleDropDown:=True
End With
You would define the LR as the last row and the LC as last column.
I'm programming a table to display rows based on numeric values in one cell and text values in another (a drop down list). I completed the code for the rows, but can't seem to get my head around the columns.
Edit: What I'm trying to do is display one or two of many columns depending on what input value I have in a dropdown. At the same time as the number of rows displayed are dependent on another cell. E.g. if I have three types of candy, one per column. And in the rows I display how many of said candies I eat per day. I want to display only one of the candies, for x days. How do I code this? The row-part I solved, the column - i need help.
This is my current code, how should I go about solving my predicament?
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Not Intersect(Target, Range("Time_horizon")) Is Nothing Then
TH_row_update
TH_column_update
End If
End Sub
Public Sub TH_row_update()
Dim cell As Range
For Each cell In Range("Time_horizon_Year")
If cell.Value >= ActiveSheet.Range("Time_Horizon") Then
cell.EntireRow.Hidden = True
ElseIf cell.Value <= ActiveSheet.Range("Time_Horizon") Then
cell.EntireRow.Hidden = False
End If
Next cell
Application.ScreenUpdating = True
End Sub
Public Sub TH_column_update()
Dim cell As Range
For Each cell In Range("comparator_range")
If cell.Value = ActiveSheet.Range("Combination_comparators") Then
cell.EntireColumn.Hidden = True
ElseIf cell.Value <= ActiveSheet.Range("Combination_comparators") Then
cell.EntireColumn.Hidden = False
End If
Next cell
Application.ScreenUpdating = True
End Sub
If you want to go trough colums and cells:
For Each col In Range("comparator_range").Columns
Debug.Print "column " & col.Address
For Each cel In col.Cells
Debug.Print " - cell " & cel.Address
Next cel
Next col
So lets say your range is A1:C5
The first for each will run for each column that exists in this range mean A B C
The second for each trough every cell inside of the colum, means A1-A5, B1-B5, C1-C5
Hope this help you already, if you need more informations just tell it.
I have created an UserForm in vba as follow:
Once a user fills out the form, the record will be saved on "Sheet2". For each Control ID, there should be 3 possible Activities. What I want to do is that after users type in the Control ID and Activity name, the form will be filled with existing data from "Sheet2". Here is what I have come up with by using "Index"/"Match" but it returns with "Type Mismatch" error.
Private Sub Reg3_AfterUpdate()
With Me
.Reg2 = Application.WorksheetFunction.Index(Sheet2.Columns(3), Application.WorksheetFunction.Match(1, (Sheet2.Columns(2) = CLng(Me.Reg1)) * (Sheet2.Columns(4) = CLng(Me.Reg3)), 0))
End With
End Sub
Reg1 is Control ID located in Column 2;
Reg2 is Full Name located in Column 3; and
Reg3 is Activity located in Column 4.
Can someone please help me?
Thomas has a good solution. If you want to use only VBA (no Excel functions), you can also set a search loop like this:
With ws
For row = fisrtRow To lastRow
val = .Cells(row, col)
If val = controlID And val = activity Then
'....
Else
'...
End If
Next row
End With
Something like this could get you on track
'-- lets assume you want value from row 1, column 3
' from "Sheet2"
Dim wb As Workbook
Dim ws As Worksheet
Dim row As Integer, column As Integer
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet2")
row = 1
col = 3
Me.Reg2.Text = ws.Cells(row, col)
here is the best solution for you
write the following code ih the after update event
Private Sub Reg3_AfterUpdate()
SheetS("SHEET2").Range("A1:C4").Select
Selection.AutoFilter
SheetS("SHEET2").Range("$A$1:$C$4").AutoFilter Field:=1, Criteria1:="=Product no", Operator:=xlAnd
SheetS("SHEET2")..Range("$A$1:$C$4").AutoFilter Field:=1, Criteria1:="=Activity", Operator:=xlAnd
'add your code to get the value from range(a2:c2) like the following
FULLNAME.TEXT=SHEETS("sheet2").range("B2)
End Sub
'in this example I assume that the data is stored in range(A1:C4)
'SO AFTER YOU APPLY THE FILTER YOU WILL GET ONE UNIQUE ROW REPRESENTING
'YOUR 'DATA .THEN YOU JUST NEED TO IMPORT THE DATA FROM THE sheet2 CELLs
'and so on
I currently have this code which allows me to launch the userform, input the an item in the text box, auto populate the date, and select from a drop down menu
then paste that information into a new row.
The cbm (combo-box) item draws its values from a separate dynamically expanding table and is a drop down menu on the userform. The date is auto populated based on todays date and the text box is draws its value from whatever the user enters.
Private Sub btnSubmit_Click()
Dim ssheet As Worksheet
Set ssheet = ThisWorkbook.Sheets("InputSheet")
nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ssheet.Cells(nr, 3) = CDate(Me.tbDate)
ssheet.Cells(nr, 2) = Me.cmblistitem
ssheet.Cells(nr, 1) = Me.tbTicker
My goal here is, depending on what list item is selected I want the name of that item to be pasted in a column that corresponds to that item. i.e if the user selects "apples" and the 3rd column is the "apple" column, I want it to paste in that location.
I am assuming this has to be down with some type of "if" statement.
Any help is appreciated.
Here is pic of my worksheet
supposing I correctly made my guessings, try this code
Option Explicit
Private Sub btnSubmit_Click()
Dim f As Range
If Me.cmblistitem.ListIndex = -1 Then Exit Sub '<--| exit if no itemlist has been selected
If Len(Me.tbTicker) = 0 Then Exit Sub '<--| exit if no item has been input
With ThisWorkbook.Sheets("InputSheet")
Set f = .Rows(1).Find(what:=Me.cmblistitem.Value, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False) '<--| look for proper column header
If f Is Nothing Then Exit Sub '<--| if no header found then exit
With .Cells(.Cells(Rows.Count, "A").End(xlUp).Row + 1, f.Column) '<--| refer to header column cell corresponding to the first empty one in column "A"
.Resize(, 3) = Array(Me.tbTicker.Value, Me.cmblistitem.Value, CDate(Me.tbDate)) '<--| write in one shot
End With
End With
End Sub
it's commented so you can easily change columns references as per your needs
BTW as for the combobox filling you may want to adopt the following code:
Dim cell As Range
With Me
For Each cell In [myName]
.cmblistitem.AddItem cell
Next cell
End With
which is optimized having referenced Me once before entering the loop so that it's being kept throughout it without further memory accesses