Textbox on UserForm doesn't refresh when I change ComboBox2 Value - excel

Here is the code, I have 2 ComboBox's on this form and 2 TextBox's and for some obscure reason when I change the debtor ComboBox it Refreshes the Balance, but doesn't refresh the Price; Price is a cross reference of Debtor and Quantity.
Private Sub UserForm_Initialize()
Purchase_Select_Debtor.List = Workbooks("New Template.xlsm").Worksheets("Debtor_list").Range("A2:A13").Value
Purchase_Select_Debtor.ListIndex = 0
Purchase_Select_Quantity.List = Workbooks("New Template.xlsm").Worksheets("RangeNames").Range("A15:A20").Value
Purchase_Select_Quantity.ListIndex = 0
End Sub
Private Sub cmdBuy_Purchase_Click()
Purchase_Select_Debtor.Value = Name
Purchase_Select_Price.Value = Amount
Purchase_Select_Balance.Value = Balance
If Name = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempName = Name Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempName = ""
If TempName = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance - Amount
MsgBox "You have just Purchased " & Amount & " For $" & Amount & vbCrLf & "Your Account Balance is now: " & Balance
End Sub
Private Sub cmdClose_Purchase_Click()
Unload Me
End Sub
Private Sub Purchase_Select_Debtor_Change()
Purchase_Select_Balance.Value = "$" & Application.VLookup(Purchase_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0)
End Sub
Private Sub Purchase_Select_Quantity_Change()
Purchase_Select_Price.Value = "$" & Application.Index(Sheets("Inventory_list").Range("A1:G13"), Application.Match(Purchase_Select_Debtor.Value, Sheets("Inventory_list").Range("A1:A13"), 0), Application.Match(Purchase_Select_Quantity.Value, Sheets("Inventory_list").Range("A1:G1"), 0))
End Sub

If you want Price AND Balance to be refreshed when you change the Debtor, then:
Private Sub Purchase_Select_Debtor_Change()
Purchase_Select_Balance.Value = "$" & Application.VLookup(Purchase_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0)
Purchase_Select_Price.Value = "$" & Application.Index(Sheets("Inventory_list").Range("A1:G13"), Application.Match(Purchase_Select_Debtor.Value, Sheets("Inventory_list").Range("A1:A13"), 0), Application.Match(Purchase_Select_Quantity.Value, Sheets("Inventory_list").Range("A1:G1"), 0))
End Sub

Related

Updating form responses from one sheet to another

I created a data entry form in Excel.
I would like that input to be stored in another sheet (Table format).
Code I found online and modified:
Function ValidateForm() As Boolean
SellerSKU.BackColor = vbWhite
Description.BackColor = vbWhite
ValidateForm = True
If Trim(SellerSKU.Value) = "" Then
MsgBox "SKU can't be left blank.", vbOKOnly + vbInformation, "SKU"
SellerSKU.BackColor = vbRed
SellerSKU.Activate
ValidateForm = False
ElseIf Trim(Description.Value) = "" Then
MsgBox "Description can't be left blank.", vbOKOnly + vbInformation, "Description"
Description.BackColor = vbRed
Description.Activate
ValidateForm = False
End If
End Function
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
SellerSKU.Value = ""
SellerSKU.BackColor = vbWhite
Description.Value = ""
Description.BackColor = vbWhite
End Sub
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Dim iRow As Long
iRow = Sheets("Reference Sheet (Order Hist)").Range("A1048576").End(xlUp).Row + 1
If ValidateForm = True Then
With ThisWorkbook.Sheets("Reference Sheet (Order Hist)")
.Range("A" & iRow).Value = SellerSKU.Value
.Range("C" & iRow).Value = Description.Value
End With
Call Reset
Else
Application.ScreenUpdating = False
Exit Sub
End If
Application.ScreenUpdating = True
End Sub
When I hit "Enter" on the data entry form, the table on the other sheet does not get updated.
Also is it possible to clear the form every time an entry has been successfully made?
This worked for me. Re-organized and removed some of the repetition...
Private Sub CommandButton2_Click()
Dim iRow As Long, valErrors As String
valErrors = ValidationErrors() 'checks the form
If Len(valErrors) = 0 Then
'no errors - add the data
With ThisWorkbook.Worksheets("Reference Sheet (Order Hist)")
iRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Range("A" & iRow).Value = SellerSKU.Value
.Range("C" & iRow).Value = Description.Value
End With
ResetForm 'Call keyword is deprecated...
Else
MsgBox "One or more errors in form entries:" & vbLf & vbLf & valErrors, _
vbOKOnly + vbExclamation, "Check form data"
End If
End Sub
'check the form and return a listing of any errors
Function ValidationErrors() As String
Dim msg As String
CheckNonBlank SellerSKU, "SKU can't be left blank.", msg
CheckNonBlank Description, "Description can't be left blank.", msg
ValidationErrors = msg
End Function
'utility sub - check if a control has text, flag as error if missing,
' and add some text to the overall validation message
Sub CheckNonBlank(cntrl As Object, msgErr As String, ByRef msg As String)
Dim isErr As Boolean
isErr = Len(Trim(cntrl.Value)) = 0 'true if no content
ErrorFlag cntrl, isErr
If isErr And Len(msgErr) > 0 Then
msg = msg & IIf(Len(msg) > 0, vbLf, "") & msgErr 'append this error
End If
End Sub
Private Sub CommandButton1_Click()
ResetForm
End Sub
'clear textboxes and any error flags
Sub ResetForm()
SellerSKU.Value = ""
ErrorFlag SellerSKU, False
Description.Value = ""
ErrorFlag Description, False
End Sub
'flag a control as having a problem (pass False to second parameter to clear flag)
Sub ErrorFlag(cntrl As Object, Optional HasError As Boolean = True)
cntrl.BackColor = IIf(HasError, vbRed, vbWhite)
End Sub

how to check if "chrome" webdriver in vba(Excel) selenium is still active

I have multiple subs that can use the same webdriver instance, opening and closing the instance is 'time consuming' which is the priority.
this is the summary of my module:
Option Explicit
Public testing_webdriver As WebDriver
Sub BrowUp()
Set testing_webdriver = New WebDriver
testing_webdriver.Start "chrome"
testing_webdriver.Window.Maximize
End Sub
Sub BrowDown()
testing_webdriver.Quit
End Sub
Function myfunct(da_row As Integer)
Dim entityAdress As String
If Range("F" & da_row).Value = "abrv1" Then
entityAdress = "site1.com"
ElseIf Range("F" & da_row).Value = "abrv2" Then
entityAdress = "site2.com"
ElseIf Range("F" & da_row).Value = "abrv3" Then
entityAdress = "site3.com"
ElseIf Range("F" & da_row).Value = "abrv4" Then
entityAdress = "site4.com"
Else
MsgBox ("cell not in work range")
End If
testing_webdriver.Get entityAdress
testing_webdriver.Wait 1000
testing_webdriver.Timeouts.ImplicitWait = 1000
testing_webdriver.FindElementByName("login_id").SendKeys "mylogin"
testing_webdriver.FindElementByName("pass_id").SendKeys "mypass"
testing_webdriver.FindElementByName("connexion").Click
testing_webdriver.Timeouts.ImplicitWait = 6000
testing_webdriver.Wait 1000
Dim conexStat As String,
Range("I" & da_row).Value = testing_webdriver.FindElementById("status").Text
End Function
Sub testconxstat()
if "testing_webdriver not exist" then
call BrowUp
else if
pass
end if
Call myfunct(ActiveCell.Row)
End Sub
Is there a way to automatically detect if the webdriver instance is still active?
Try this
If Not testing_webdriver Is Nothing Then
MsgBox "Webdriver Exists", vbInformation
Else
MsgBox "Webdriver Doesn't Exist", vbCritical
End If

VBA Dynamic Range VLOOKUP

I'm new to VBA and need get some help with a VLOOKUP?
I keep getting Compile error for Expected: end of statement
This is the line that is giving me problems.
I added the & sign after (row_number) and am now getting a run-time error '9': Subscript out of range error.
Sheets("WIP Count").Range("G" & (row_number)).Formula = "=VLOOKUP(C" & (row_number) & ",PRISM!L:T,8,FALSE)"
Here is the rest of the code.
Sub CommandButton1_Click()
q1_answer = Sheets("Tracker").Range("F8")
q2_answer = Sheets("Tracker").Range("F9")
q3_answer = Sheets("Tracker").Range("F10")
q4_answer = Sheets("Tracker").Range("F11")
If q1_answer = "" Then
MsgBox "Fill in Name"
Exit Sub
End If
If q2_answer = "" Then
MsgBox "Fill in Serial Number"
Exit Sub
End If
If q3_answer = "" Then
MsgBox "Fill in Part Number"
Exit Sub
End If
If q4_answer = "" Then
MsgBox "Fill in Quantity"
Exit Sub
End If
row_number = 1
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("WIP_Count").Range("A" & row_number)
Loop Until item_in_review = ""
last_transaction_id = Sheets("WIP_Count").Range("A" & (row_number - 1))
Dim next_transaction_id As Integer
next_transaction_id = last_transaction_id + 1
Sheets("WIP_Count").Range("A" & (row_number)) = next_transaction_id
Sheets("WIP_Count").Range("B" & (row_number)) = q1_answer
Sheets("WIP_Count").Range("C" & (row_number)) = q2_answer
Sheets("WIP_Count").Range("D" & (row_number)) = q3_answer
Sheets("WIP_Count").Range("E" & (row_number)) = q4_answer
Sheets("WIP_Count").Range("F" & (row_number)).Value = Date
Sheets("WIP Count").Range("G" & (row_number)).Formula = "=VLOOKUP(C" & (row_number) & ",PRISM!L:T,8,FALSE)"
Sheets("Tracker").Range("F8") = ""
Sheets("Tracker").Range("F9") = ""
Sheets("Tracker").Range("F10") = ""
Sheets("Tracker").Range("F11") = ""
MsgBox "Done"
End Sub

Looping through Multi-selected Listbox values to create and name workbook

The listbox is not assigning the selected values to "n". The "n" value is 0 regardless if I select values from the listbox or not. I'm learning, so it could be something simple that I'm missing... suggestions? Thanks!
Private Sub UserForm_Initialize()
With cbomonth
.AddItem "January"
.AddItem "February"
End With
With cboyear
.AddItem "2013"
.AddItem "2014"
End With
With cboteam
.AddItem "Team1"
.AddItem "Team2"
End With
With cbodocument
.AddItem "Task1"
.AddItem "Task2"
End With
With ListBox1
.AddItem "Name"
.AddItem "Name"
End With
cboteam.ListIndex = 0
cboyear.ListIndex = 4
cbomonth.ListIndex = 6
cbodocument.ListIndex = 1
End Sub
Private Sub cmdSubmit_Click()
Dim year As String
Dim month As String
Dim days As Integer
Dim team As String
Dim n as Long
Dim tallboxynames As Variant
Dim tallynewfile As String
Unload Me
year = cboyear.Value
month = cbomonth.Value
team = cboteam.Value
document = cbodocument.Value
TallyPath = "\\network path\Tally of orders\Master Template\"
TallyPath1 = "\\network path\Tally of orders\" & year & "\"
TallyPath2 = "\\network path\Tally of orders\" & year & "\" & month & "\"
TallyTemplate = "Tally_Template_ver1.xls"
If document = "Tally Sheets" Then
For n = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(n) Then
tallynewfile = ListBox1.Selected(n) & ".xls"
Else
MsgBox "No data from listbox"
End If
If Len(Dir(TallyPath1, vbDirectory)) = 0 Then
MkDir TallyPath1
End If
If Len(Dir(TallyPath2, vbDirectory)) = 0 Then
MkDir TallyPath2
FileCopy TallyPath & TallyTemplate, TallyPath2 & tallynewfile
End If
Next n
End If
Exit Sub
End Sub
Move the Unload Me to the end of the procedure:
Private Sub cmdSubmit_Click()
' code here ...
Unload Me
End Sub
To get the selected item use Value if ListBox1.MultiSelect = 0 (fmMultiSelectSingle):
Me.ListBox1.Value
If MultiSelect > 0 then use Selected property, example:
Private Function GetSelectedItems() As String
Dim text As String
Dim i As Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
text = text & Me.ListBox1.List(i) & vbNewLine
End If
Next i
MsgBox "Selected items are: " & text
GetSelectedItemsText = text
End Function
Instead of
Unload Me
try to use
Me.Hide
When you unload all values on the form are deleted. They are kept if you use Hide.

Excel Spreadsheet Convert to UserForm VBA Problem

I made a Sheet which I have now converted to a UserForm but the VB code I was using no longer works, I will copy the old code used and the new one which keeps telling me "Debtor not found", any help would be Appreciated.
Old Code:
Sub Buy_Click()
Name = Worksheets("Purchase").Range("G19").Value
Amount = CSng(Worksheets("Purchase").Range("I19").Value)
Balance = CSng(Worksheets("Purchase").Range("J19").Value)
If Name = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempName = Name Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempName = ""
If TempName = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance - Amount
MsgBox "You have just Purchased " & Range("H19") & " For $" & Range("I19") & vbCrLf & "Your Account Balance is now: " & Range("J19")
Application.Goto Reference:="Purchase_Debtor"
Selection.ClearContents
Application.Goto Reference:="Purchase_Quantity"
Selection.ClearContents
Sheets("Menu").Select
End Sub
New Code:
Private Sub cmdBuy_Purchase_Click()
Purchase_Select_Debtor.Value = Name
Purchase_Select_Price.Value = Amount
Purchase_Select_Balance.Value = Balance
If Name = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempName = Name Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempName = ""
If TempName = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance - Amount
MsgBox "You have just Purchased " & Amount & " For $" & Amount & vbCrLf & "Your Account Balance is now: " & Balance
End Sub
And another Code I've used for a different UserForm with the same issue;
Old Code:
Sub Pay_Click()
Name = Worksheets("pay_balance").Range("F18").Value
Amount = CSng(Worksheets("pay_balance").Range("G18").Value)
If Name = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempName = Name Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempName = ""
If TempName = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance + Amount
MsgBox "You have just Credited $" & Range("G18") & vbCrLf & "Your Account Balance is now: " & Range("H18")
Application.Goto Reference:="Creditbox"
Selection.ClearContents
Application.Goto Reference:="Balance_Debtor"
Selection.ClearContents
Sheets("Menu").Select
End Sub
New Code:
Private Sub cmdPay_Balance_Click()
Pay_Balance_Balance.Value = Name
Pay_Balance_Credit.Value = Amount
If Name = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempName = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempName = Name Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempName = ""
If TempName = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance + Amount
MsgBox "You have just Credited $" & Amount & vbCrLf & "Your Account Balance is now: " & Name
End Sub
Private Sub cmdBuy_Purchase_Click()
Debtor = Purchase_Select_Debtor.Value
Amount = CSng(txtPrice.Value)
Balance = CSng(txtBalance.Value)
If Debtor = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempDebtor = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempDebtor = Debtor Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempDebtor = ""
If TempDebtor = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance - Amount
Balance = Application.VLookup(Purchase_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0)
MsgBox "You have just Purchased " & Purchase_Select_Quantity.Value & " For $" & Amount & vbCrLf & "Your Account Balance is now: " & Balance
Unload FrmPurchase
End Sub
&
Sub cmdPay_Balance_Click()
Debtor = Pay_Balance_Select_Debtor.Value
Amount = CSng(txtCredit.Value)
If Debtor = "" Then
MsgBox "Select Debtor"
Exit Sub
End If
DebtorRow = 1
Do
TempDebtor = Worksheets("Debtor_list").Range("A" & DebtorRow).Value
If TempDebtor = Debtor Then
DebtorBalance = CSng(Worksheets("Debtor_List").Range("B" & DebtorRow).Value)
Exit Do
End If
DebtorRow = DebtorRow + 1
Loop Until TempDebtor = ""
If TempDebtor = "" Then
MsgBox "Debtor not found"
Exit Sub
End If
Worksheets("Debtor_List").Range("B" & DebtorRow).Value = DebtorBalance + Amount
txtBalance.Value = Application.VLookup(Pay_Balance_Select_Debtor.Value, Sheets("Debtor_list").Range("A2:B13"), 2, 0)
MsgBox "You have just Credited $" & Amount & vbCrLf & "Your Account Balance is now: " & txtBalance
Unload frmPay_Balance
End Sub

Resources