Adding a data based on the specific value of combobox given? - excel

I have an excel file automated with a macro. I have a userform interface (see photo below). Inside the userform is a combobox and a textbox.
I want to add something in the worksheet but depending on what type of expenses it is and I don't understand what is wrong in my code below. What I want is if I add something in the column of "taxi" (2nd row), I can also in "carwash" (2nd row) but it appears to a different result. I am also not able to add data on 3rd, 4th, 5th rows and so on.
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'find first empty row in database
Set rngNullString = Intersect(ws.Columns("A"), ws.Columns("A")).Find("")
If rngNullString.row < ws.Cells(ws.Rows.Count, "A").End(xlUp).row Then
Set rngNullString = Intersect(ws.Columns("A"), _
ws.Columns("A")).SpecialCells(xlCellTypeBlanks)
End If
iRow = rngNullString.row
'check for Name number
If Trim(Me.TextBox1.value) = "" Then
Me.TextBox1.SetFocus
MsgBox "Please complete the FORM"
Exit Sub
End If
'copy the data to the database
If Me.ComboBox1.value = "Taxi" Then
ws.Cells(iRow, 1).value = Me.ComboBox1.value
ws.Cells(iRow, 2).value = Me.TextBox1.value
End If
If Me.ComboBox1.value = "Carwash" Then
ws.Cells(iRow, 3).value = Me.ComboBox1.value
ws.Cells(iRow, 4).value = Me.TextBox1.value
End If
MsgBox "Sucessfully! Data added", vbOKOnly + vbInformation, "Data Notification"
'clear the data
Me.TextBox1.value = ""
Me.ComboBox1.value = ""
Me.TextBox1.SetFocus
Unload Me
End Sub

Related

Save value from a combobox and then clear the combobox

I have a form that with a lot of data validation. The form can be saved.
In the last update I added an ActiveX Combobox. People can choose from different license plates and it auto-fills so fewer errors are made.
I want the value filled in the combobox to be saved and the combobox field to be empty when the 'save button' is pressed.
This is the code I currently have:
Sub Store_Data()
'Takes data from one worksheet and stores it in the next empty row
Dim sourceSheet As Worksheet
Dim dataSheet As Worksheet
Dim nextRow As Integer
Dim xCombox As OLEObject --- Not sure this is correct
Set sourceSheet = Sheets("Départ")
Set dataSheet = Sheets("Data Départ")
Set xCombox = sourceSheet.OLEObjects("TempCombo") '--- not sure this is correct
'A is the colums is that will always be there; here it is matricule - unique data
nextRow = dataSheet.Range("A" & dataSheet.Rows.Count).End(xlUp).Offset(1).Row
dataSheet.Cells(nextRow, 1).Value = sourceSheet.Range("F4").Value
dataSheet.Cells(nextRow, 2).Value = sourceSheet.Range("F6").Value
dataSheet.Cells(nextRow, 3).Value = sourceSheet.Range("F8").Value
' ---- I used LinkedCell and then use the value of this cell to add to my database,
' because I have no idea how to retrieve data from the combobox directly.
dataSheet.Cells(nextRow, 4).Value = sourceSheet.Range("L10").Value
dataSheet.Cells(nextRow, 5).Value = sourceSheet.Range("F12").Value
dataSheet.Cells(nextRow, 6).Value = sourceSheet.Range("F14").Value
dataSheet.Cells(nextRow, 7).Value = sourceSheet.Range("F16").Value
dataSheet.Cells(nextRow, 8).Value = Format(Now, "HH:mm:ss")
'Clear Data
sourceSheet.Range("F4").Value = ""
sourceSheet.Range("F8").Value = ""
sourceSheet.Shapes.Range(Array("TempCombo")).Clear '----- This one gives an error
sourceSheet.Range("F12").Value = ""
sourceSheet.Range("F14").Value = ""
sourceSheet.Range("F16").Value = ""
ActiveWorkbook.Save
End Sub
Most of these codes I retrieved from the internet.
This is how the combobox looks and is named. (My Excel is in Dutch).

Update the same names on different row using loop

I have a userform with combobox 9 when you select combobox9 value it will show all the values into the each boxes and you can updated the textbox 19 value into sheet against the raw of the selected value in combobox9 however problem is if there's the same name e.g. twice same name in combobox9 it will only update the its 1st name on the raw and not the 2nd or even if there is 3 entry in sheet with same name.
Names are in column C and textvalue is updated its name on column H however I need to loop the column H if it is already updated against its name then same name needs to updated which is in new raw.
Below is the vba code I have but it is so far not working
Dim lCol As Variant
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Attendance")
If Me.ComboBox9.Value <> "" Then
If VBA.CVar(Application.Match(VBA.CVar(Me.ComboBox9.Value), sh.Range("C:C"), 0)) = True Then
MsgBox "Record Not found", vbCritical
Exit Sub
Else
i = Application.Match(VBA.Cvar(Me.ComboBox9.Value), sh.Range("C:C"), 0)
End If
lCol = Me.ComboBox9.Value
Set findvalue = sh.Range("C:C").Find(What:=lCol, LookIn:=xlValues)
If Not findvalue Is Nothing Then
adr = findvalue.Address
Do
If findvalue.Offset(0, 6).Value = Me.TextBox19 Then
sh.Unprotect "1234"
findvalue.Offset(0, 6).Value = Me.TextBox19.Value = ""
Exit Do
End If
Set findvalue = sh.Range("C:C").FindNext(findvalue)
Loop While findvalue.Address <> adr
Set findvalue = Nothing
End If

How to reference specific cell number for LOOKUP in VBA?

I have the following code:
Private Sub submitFormBtn_Click()
If timeOfArrival.Value = "" Then
MsgBox "Please enter time of arrival", vbCritical
ElseIf poNumber.Value = "" Then
MsgBox "Please enter a valid PO Number", vbCritical
Else
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.DTPicker1.Value
.Cells(lRow, 2).Value = Me.timeOfArrival.Value
.Cells(lRow, 4).Value = Me.poNumber.Value
.Cells(lRow, 5).Value = "=LookupCSVResults(D3,Table_PurchaseOrderLine[POR],Table_PurchaseOrderLine[Product])"
End With
End If
End Sub
What it does
The code above, takes in the User Form Text Box values, and then inserts it into excel sheet for each available row.
Problem
This function "=LookupCSVResults(D3,Table_PurchaseOrderLine[POR],Table_PurchaseOrderLine[Product])" is the complication.
For every record inserted into Excel sheet, it is currently referencing to cell D3 - how can I reference it to the cell to the left of it?
Current problem:
Should be:
If I understand correctly, you want to use lrow:
"=LookupCSVResults(D" & lrow & ",Table_PurchaseOrderLine[POR],Table_PurchaseOrderLine[Product])"

VBA UserForm - Code not working as expected when correlating

Let me preface this with saying fairly new to VBA.
But I've created a Userform that is taking in First Name, Last Name and Job Title.
Looking to correlate training to job title.
Code references named ranges (1)Merge_Title and (2)Merge_Training from the Merged_DF.
The error I'm encountering is seen in 'Results' pic below. Looking to simply have name, job, and all relevant training associated to that job (ref. Merge_DF pic).
Any help is much appreciated.
Thanks again!
Merge_DF Pic
Results
''' Submits userform into table
Dim lRow As Long
Dim lJob As Long
Dim ws As Worksheet
Dim ws_Merge As Worksheet
Set ws = Worksheets("DATA")
Set ws_Merge = Worksheets("MERGE_DF")
'find first empty row in table
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
lJob = Me.CmboJob.ListIndex
'verify data entered
If Trim(Me.TextFirst.Value) = "" Then
Me.TextFirst.SetFocus
MsgBox "You Forgot the First Name"
Exit Sub
End If
If Trim(Me.TextLast.Value) = "" Then
Me.TextLast.SetFocus
MsgBox "You Forgot the Last Name"
Exit Sub
End If
If Trim(Me.CmboJob.Value) = "" Then
Me.CmboJob.SetFocus
MsgBox "You Forgot the Job Title"
Exit Sub
End If
'loops and records data
For Each c In ws_Merge.Range("Merge_Title")
If c = Me.CmboJob.Value Then
With ws
.Cells(lRow, 1).Value = Me.TextLast.Value
.Cells(lRow, 2).Value = Me.TextFirst.Value
.Cells(lRow, 4).Value = Me.CmboJob.Value
For Each i In ws_Merge.Range("Merge_Training")
ws.Cells(lRow, 7).Value = i.Value
lRow = lRow + 1
Next
' lRow = lRow + 1
End With
End If
Next c
Me.TextLast.Value = ""
Me.TextFirst.Value = ""
Me.CmboJob.Value = ""
Me.CmboJob.SetFocus
End Sub

Excel vba, display value in a textbox that I fetch from a table

I have a table that ranges from F2 to G230 . The F column is employee number and G column is employee name. Im trying to make a simple program that registers food orders for the employees and exports the data into a table. Everything works fine except when I choose a employee from a drop down list I want the employee number to appear in a textbox.
Here is my whole code:
Private Sub cmdbutton_add_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("listi yfir skráningar")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(Me.combobox_name.Value) = "" Then
Me.combobox_name.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.textbox_staffnr.Value
ws.Cells(iRow, 2).Value = Me.combobox_name.Value
ws.Cells(iRow, 3).Value = Me.combobox_rettir.Value
ws.Cells(iRow, 4).Value = Me.textbox_verd.Value
ws.Cells(iRow, 5).Value = Me.textbox_dags.Value
MsgBox "Komið :)", vbOKOnly + vbInformation, "Komið"
'clear the data
Me.combobox_name.Value = ""
Me.combobox_name.SetFocus
Unload Me
End Sub
Private Sub Cmdbutton_close_Click()
Unload Me
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub combobox_name_Change()
textbox_staffnr.Value = Application.VLookup(Me.combobox_name.Value, Sheet3.Range("F2:G230"), 1, 0)
End Sub
Private Sub combobox_rettir_Change()
textbox_verd.Value = Application.VLookup(Me.combobox_rettir.Value, Sheet3.Range("C2:D23"), 2, 0)
End Sub
Private Sub Name_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub textbox_staffnr_Change()
End Sub
Private Sub textbox_verd_Change()
End Sub
Private Sub UserForm_Initialize()
textbox_dags.Value = Format(Date, "dd/mm/yyyy")
End Sub
And here Im getting error:
Private Sub combobox_name_Change()
textbox_staffnr.Value = Application.VLookup(Me.combobox_name.Value, Sheet3.Range("F2:G230"), 1, 0)
End Sub
Please help.
EDIT: From user's comment:
The number is in column 1 and the name is in column 2
In that case, Vlookup won't work, you'll need to use Index/Match, or a Range.Find.Offset. Personally I prefer the Range.Find.Offset while in VBA:
Dim rngFound as Range
Set rngFound = Sheet3.Range("G2:G230").Find(Me.combobox_name.Value, , xlValues, xlWhole)
If not rngFound is Nothing Then textbox_staffnr.Value = rngFound.Offset(0, -1).Text

Resources