VBA UserForm - Code not working as expected when correlating - excel

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

Related

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])"

excel VBA find doesn't work with specific values

Find function works quite good, but there are few exceptions that I don't understand. I have userform, I use find method to get all information about product/item by its code and showing it after button is pressed in userform. Product codes in my table consists of such codes: 1230, 1231, 1232... 1239. The main problem is that I don't understand why numbers like: 1-9, 123 doesn't trigger the msgbox "Can't find product"?
Private Sub btnSearch_Click()
Dim i As Long
Dim totalRows As Long
Dim itemCode As Range
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Me.txtCode.Value)
totalRows = Worksheets("Data").Range("A:A").CurrentRegion.Rows.Count
'searching by code
If Trim(Me.txtCode.Value) = "" Then
Me.txtCode.SetFocus
MsgBox "Need item code"
Exit Sub
End If
If itemCode Is Nothing Then
MsgBox "Can't find product with such code"
End If
For i = 2 To totalRows
If Trim(Cells(i, 1)) = Trim(Me.txtCode) Then
txtName.Text = Cells(i, 2)
'unit of measurement name
txtUnitName.Text = Cells(i, 3)
txtPrice.Text = Cells(i, 4)
Exit For
End If
Next i
End Sub
If you want an exact match, you should add LookAt:=xlWhole to the find parameters.
Otherwise this should do about the same thing, without using find:
Private Sub btnSearch_Click()
Dim i As Long
Dim totalRows As Long
Dim arrData As Variant
With Worksheets("Data")
totalRows = .Cells(Rows.Count, 1).End(xlUp).Row
arrData = .Range("A1:D" & totalRows)
End With
'searching by code
If Trim(Me.txtCode.Value) = "" Then
Me.txtCode.SetFocus
MsgBox "Need item code"
Exit Sub
End If
For i = 2 To totalRows
If Trim(arrData(i, 1)) = Trim(Me.txtCode) Then
txtName.Text = arrData(i, 2)
'unit of measurement name
txtUnitName.Text = arrData(i, 3)
txtPrice.Text = arrData(i, 4)
Exit For
End If
If i = totalRows Then MsgBox "Can't find product with such code"
Next i
End Sub
Replace:
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Me.txtCode.Value)
With:
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Trim(Me.txtCode.Value), LookIn:=xlValues, LookAt:=xlWhole)

Connect newly added Sheet to existing one

This is my first post in Stack Overflow so any mistake I make please just ignore.
So i made an button which runs the macro of an application inputbox, the name you enter in the inputbox will create a new sheet with the name you entered, it also will create a table on the new sheet. The name you put on the inputbox are the clients that newly came so i will have specific sheet with table for every client that comes.
On the other hand I got the Workers which will receive incomes from clients, I Got 4 Workers which have their own Sheet and Table of Incomes and Outcomes.
Now the question i am getting to is that, is it possible to creade a code on VBA that will say: If on the new sheet (inside the table, specificly: K8:K23, K28:K43, K49:K64) the name of the Worker is inserted, copy the name of the client and paste it into the existing sheet of the Worker.
The code i tried but did not work: (Only Check the First Sub and the end of line, the between code is just a bunch of macro for table to be created, that parts work, the problem of my code which is located at the end is that it does nothing, and yes I did an commend to the codes on purpose)
Sub KerkimiKlientit()
Dim EmriKlientit As String
Dim rng As Range, cel As Range
Dim OutPut As Integer
retry:
EmriKlientit = Application.InputBox("Shkruani Emrin e Klientit", "Kerkimi")
If Trim(EmriKlientit) <> "" Then
With Sheets("Hyrjet").Range("B10:B200")
Set rng = .Find(What:=EmriKlientit, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
sheet:
Flag = 0
Count = ActiveWorkbook.Worksheets.Count
For i = 1 To Count
WS_Name = ActiveWorkbook.Worksheets(i).Name
If WS_Name = EmriKlientit Then Flag = 1
Next i
If Flag = 1 Then
ActiveWorkbook.Sheets(EmriKlientit).Activate
Exit Sub
Else
Sheets.Add(, Sheets(Sheets.Count)).Name = EmriKlientit
Call KrijimiTabeles(EmriKlientit)
Exit Sub
End If
Else
OutPut = MsgBox("Klienti nuk u gjet", vbRetryCancel + vbInformation, "Provoni Perseri")
If (OutPut = vbRetry) Then
GoTo retry:
ElseIf (OutPut = vbCancel) Then
Exit Sub
End If
Exit Sub
End If
End With
End If
If userInputValue = "" Then
OutPut = MsgBox("Rubrika e Emrit e zbrazet", vbRetryCancel + vbExclamation, "Gabim")
If (OutPut = vbRetry) Then
GoTo retry:
ElseIf (OutPut = vbCancel) Then
Exit Sub
End If
Else
GoTo retry:
End If
End Sub
Sub KrijimiTabeles(EmriKlientit As String)
'
' KrijimiTabeles Macro
'
'This was just an middle code, it was too long so I did not paste it. Not an important part tho.
'This is the part that does not work, it just does nothing for some reason, there are multiple codes here and I tried them all.
'Sub Formula(EmriKlientit As String, ByVal Target As Range)
'ActiveWorkbook.Sheets(EmriKlientit).Activate
'If Not Application.Intersect(Range("K8:K23"), Range(Target.Adress)) Is Nothing Then
'Call Formula1
'End If
'End Sub
'Dim LR As Long, i As Long
'Application.ScreenUpdating = False
'Dim Rng As Range
'For Each Rng In Range("K8:K23")
'Select Case Rng.Value
'Case "M"
'Worksheets(EmriKlientit).Range("K2").Copy
'Worksheets("Mustafa").Range("K6").PasteSpecial Paste:=xlPasteFormulas
'End Select
'Next Rng
'Application.ScreenUpdating = True
'For Each cel In Rng
'If cel.Value = "M" Then
'Worksheets(EmriKlientit).Range("K2").Copy
'Worksheets("Mustafa").Range("K6").PasteSpecial Paste:=xlPasteFormulas
'End If
'Next cel
'ActiveWorkbook.Sheets(EmriKlientit).Activate
'If Not Application.Intersect(Range("K8:K23"), Range(Rng.Adress)) Is Nothing Then
'With Sheets(EmriKlientit)
'With .Range("K8:K23")
'If .Text = "M" Then
'Worksheets(EmriKlientit).Range("K2").Copy
'Worksheets("Mustafa").Range("K6").PasteSpecial Paste:=xlPasteFormulas
'End If
'End With
'End With
'End If
'Flag = 0
'Count = ActiveWorkbook.Worksheets.Count
'For i = 1 To Count
'WS_Name = ActiveWorkbook.Worksheets(i).Name
'If WS_Name = EmriKlientit Then Flag = 1
'Next i
'If Flag = 1 Then
'ActiveWorkbook.Sheets(EmriKlientit).Activate
'For Each Cell In Sheets(EmriKllientit).Range("K8:K23")
'If Cell.Value = "M" Then
'Range("K2").Copy
'Worksheets("Mustafa").Range("K6").PasteSpecial Paste:=xlPasteFormulas
'End If
'Next
'End If
End Sub
Thank you
I hope I was clear enough,
Any help would be appreciated.
Welcome to StackOverflow - i agree that your question can be a bit more specific...
I think what you are trying to achieve is something between this lines:
Dim wsClient As Worksheet, wsMustafa As Worksheet
Dim i As Long
Dim fRow As Long, lRow As Long
Set wsClient = ActiveWorkbook.Sheets("Client")
Set wsMustafa= ActiveWorkbook.Sheets("Mustafa")
'you can assign this through better ways, but to start with...
fRow = 8
lRow = 23
For i = fRow To lRow
If wsClient.Range("K" & i).Value = "M" Then
wsMustafa.Range("K6").Value = wsClient.Range("K" & i).Value 'or .Formula if that's what you want
End If
Next i
Hope this helps, good luck.

use .oleobjects.textbox.text in if statement

I have a textbox and an png image on my worksheet (not in userform). The png image loads the calendar and the value selected on calendar is returned to the textbox.
I further want to use the .oleobject in a if loop.
Like if some xyz variable >= .oleobject
Below is the code for reference.
Sub futurejoiners()
Dim lr As Long
Windows("EMPDATA.xlsm").Activate
Sheets("INPUTDATA").Select
lr = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row
For i = 2 To lr
Windows("EMPDATA.xlsm").Activate
If ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text = "" Then
MsgBox "Please select the reporting start and end dates in Sheet1"
Exit Sub
Else
If Cells(i, "J").value >= ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text Then
Cells(i, "J").Select
Selection.EntireRow.Delete
End If
End If
Next
End Sub
I get a runtime error 1004 unable to get the oleobjects property of the worksheet class on the if statement where it checks for = ""
I strongly believe something wrong in the way im calling the workbook or worksheet of that oleobject.
Please help. Thanks in advance :)
I'm not sure it works well. But it is desirable to unusing select, activate method.
Sub futurejoiners()
Dim lr As Long
Dim myWS As Worksheet
Dim myWB As Workbook
Dim myDate
Dim i As Long
Set myWB = worboooks("EMPDATA.xlsm")
Set myWS = myWB.Sheets("INPUTDATA")
'Windows("EMPDATA.xlsm").Activate
'Sheets("INPUTDATA").Select
With myWS
lr = .Cells.Find("*", SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row
For i = lr To 2 Step -1 '<~~ large to small step -1
'Windows("EMPDATA.xlsm").Activate
myDate = ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text
If myDate = "" Then
MsgBox "Please select the reporting start and end dates in Sheet1"
Exit Sub
Else
If IsNumeric(myDate) Then
Else
myDate = DateValue(myDate)
End If
'If .Cells(i, "J").Value >= ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text Then
If .Cells(i, "J").Value >= myDate Then
Cells(i, "J").EntireRow.Delete
End If
End If
Next
End Sub

Add value from combobox onto multiple columns

I have created a combobox that has different cases seen below. The current formula works except that I would like to add a additional column that replicates the same value given to columns C and want to add it column R.
Ex. ComboBox
Select Current Month
I want to add 500 units to Column C and Column R based on the part that was searched for.
Private Sub cmdAdd_Click()
Dim irow As Long
Dim lastRow As Long
Dim iCol As String
Dim C As Range
Dim ws As Worksheet
Dim value As Long
Dim NewPart As Boolean
Set ws = Worksheets("Summary")
Set C = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If C Is Nothing Then
'find first empty row in database
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
irow = lastRow + 1
NewPart = True
Else
'find row where the part is
irow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
NewPart = False
End If
'check for a part number
If Trim(Me.PartTextBox.value) = "" Then
Me.PartTextBox.SetFocus
MsgBox "Please Enter A Part Number"
Exit Sub
End If
If Trim(Me.MonthComboBox.value) = "" Then
Me.MonthComboBox.SetFocus
MsgBox "Please Enter A Month"
Exit Sub
End If
If Trim(Me.AddTextBox.value) = "" Then
Me.AddTextBox.SetFocus
MsgBox "Please Enter A Value To Add Or Substract"
Exit Sub
End If
Select Case MonthComboBox.value
Case "Current Month"
iCol = "C" And "R"
Case "Current Month +1"
iCol = "N"
Case "Current Month +2"
iCol = "O"
Case "Current Month +3"
iCol = "P"
Case "Current Month +4"
iCol = "Q"
End Select
value = Cells(irow, iCol).value
With ws
.Cells(irow, iCol).value = value + CLng(Me.AddTextBox.value)
End With
If NewPart = True Then
ws.Cells(irow, "A").value = Me.PartTextBox.value
End If
If NewPart = True Then
ws.Cells(irow, "C").value = Me.AddTextBox.value
End If
I may recommend using an Array to store the columns.
Sub t()
Dim iCol()
Dim testStr$, myValue$
Dim iRow&
Dim ws As Worksheet
testStr = "Current Month"
Select Case testStr
Case "Current Month"
iCol() = Array("C", "R")
Case "Current Month +1"
iCol() = Array("N")
End Select
Dim i&
For i = LBound(iCol) To UBound(iCol)
myValue = Cells(iRow, iCol(i)).value ' WHAT SHEET IS THIS ON??
With ws
.Cells(iRow, iCol(i)).value = myValue + CLng(Me.AddTextbox.value)
End With
Next i
End Sub
You can add to the Case as needed. Note that you need to wrap the Next i after you're done working with a column, so it can see if there's a second one to run on.
Also, since you didn't include all the code, you may have to adjust the ranges. (note the myValue doesn't have a Sheet specified for what Cells() to use).

Resources