VBA Dynamic Range VLOOKUP - excel

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

Related

Excel MaxVal + 1 not ticking up when document is finished

im currently having an error code 1004 with VBA for excel, I'm not too familiar with the program but I have been able to determine the issue with the code although i definitely do not know how to fix it.
TLDR on what the code is supposed to do;
once the form is filled copy relevant data to a separate workbook
send an email to the relevant party of new entry
save the entry as a PDF
reset the workbook with the ticket number + 1 to mark up
the issue lies within the last step, once the first PDF file was created the workbook will no longer save as the ticker is stuck on Ticket# 1
Application.ScreenUpdating = False
If Range("H3").Value = "" Then MsgBox "Please Enter Device Serial Number"
Range("H3").Select
If Range("H3").Value = "" Then Exit Sub
If Range("M3").Value = "" Then MsgBox "Please Enter Reference Standard ID"
Range("M3").Select
If Range("M3").Value = "" Then Exit Sub
If Range("K9").Value = "" Then MsgBox "Please Enter Atleast One Dimensional Check"
Range("K9").Select
If Range("K9").Value = "" Then Exit Sub
If Range("Q9").Value = "" Then MsgBox "Please Enter Visual Check for Damage"
Range("Q9").Select
If Range("Q9").Value = "" Then Exit Sub
If Range("U9").Value = "" Then MsgBox "Please Enter Inital for Damage Check"
Range("U9").Select
If Range("U9").Value = "" Then Exit Sub
If Range("Q10").Value = "" Then MsgBox "Please Enter Visual Check for Wear"
Range("Q10").Select
If Range("Q10").Value = "" Then Exit Sub
If Range("U10").Value = "" Then MsgBox "Please Enter Inital for Wear Check"
Range("U10").Select
If Range("U10").Value = "" Then Exit Sub
If Range("Q11").Value = "" Then MsgBox "Please Enter Visual Check for Travel"
Range("Q11").Select
If Range("Q11").Value = "" Then Exit Sub
If Range("U11").Value = "" Then MsgBox "Please Enter Inital for Travel Check"
Range("U11").Select
If Range("U11").Value = "" Then Exit Sub
If Range("Q12").Value = "" Then MsgBox "Please Enter Visual Check for Zero"
Range("Q12").Select
If Range("Q12").Value = "" Then Exit Sub
If Range("U12").Value = "" Then MsgBox "Please Enter Inital for Zero Check"
Range("U12").Select
If Range("U12").Value = "" Then Exit Sub
If Range("Q13").Value = "" Then MsgBox "Please Enter Visual Check for Repeatability"
Range("Q13").Select
If Range("Q13").Value = "" Then Exit Sub
If Range("U13").Value = "" Then MsgBox "Please Enter Inital for Repeatability Check 3x"
Range("U13").Select
If Range("U13").Value = "" Then Exit Sub
If Range("C23").Value = "True" Then MsgBox "Please Check Final Verification Pass or Fail"
If Range("C23").Value = "True" Then Exit Sub
Workbooks.Open "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\VerificationData(DONOTDELETE).xlsx"
Application.Run (["GetMax"])
Application.Run (["SavePrintEmail"])
Application.Run (["CopyClear"])
Application.ScreenUpdating = True
End Sub
Private Sub GetMax()
Dim WorkRange As Range
Dim MaxVal As Double
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set WorkRange = ActiveWorkbook.Worksheets("Data").Range("AK:AK")
MaxVal = WorksheetFunction.Max(WorkRange)
Workbooks("PIV-001.xlsm").Activate
ActiveWorkbook.Worksheets("PIV-001").Unprotect ("Moldamatic")
ActiveWorkbook.Worksheets("PIV-001").Range("U21").Value = MaxVal + 1
End Sub
Private Sub SavePrintEmail()
ThisWorkbook.Save
If Len(Dir("\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date), vbDirectory)) = 0 Then
MkDir "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date)
End If
Sheets("PIV-001").Select
Sheets("PIV-001").ExportAsFixedFormat xlTypePDF, "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date) & "\" & Range("U21").Value & "-" & Year(Date), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)
On Error Resume Next
With OutMail
.To = "Spage#moldamatic.com"
.CC = ""
.BCC = ""
.Subject = "NEW INSTRUMENT VERFICATION (TICKET# " & Range("U21").Value & " INSTRUMENT ID# " & Range("H3").Value & " RESULT: " & Range("H22").Value & ")"
.HTMLBody = "An instrument has just been verfied, please see attached verification report. Verficiation results: " & Range("H22").Value & " "
.Attachments.Add "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date) & "\" & Range("U21").Value & "-" & Year(Date) & ".pdf"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
ThisWorkbook.Save
End Sub
Private Sub CopyClear()
'Change path to database in line below
Dim historyWks As Worksheet
Dim inputWks As Worksheet
Dim nextRow As Long
Dim oCol As Long
Dim myRng As Range
Dim myCopy As String
Dim myCell As Range
'cells to copy from Input sheet - some contain formulas
myCopy = "U21,C22,E22,H3,M3,B9,F9,I9,K9,B10,F10,I10,K10,B11,F11,I11,K11,B12,F12,I12,K12,B13,F13,I13,K13,Q9,U9,Q10,U10,Q11,U11,Q12,U12,Q13,U13,G17"
Set inputWks = ThisWorkbook.Worksheets("PIV-001")
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set historyWks = ActiveWorkbook.Worksheets("Data")
With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With
With inputWks
Set myRng = .Range(myCopy)
End With
With historyWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
.Cells(nextRow, "B").Value = Application.UserName
oCol = 3
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With
Workbooks("PIV-001.xlsm").Activate
Range("H3,M3,B9,F9,K9,B10,F10,K10,B11,F11,K11,B12,F12,K12,B13,F13,K13,Q9,U9,Q10,U10,Q11,U11,Q12,U12,Q13,U13,G17").Select
Selection.ClearContents
ActiveSheet.CheckBoxes.Value = False
Range("H3:L3").Select
ThisWorkbook.Worksheets("PIV-001").Protect ("Moldamatic")
Workbooks("PIV-001.xlsm").Save
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Workbooks("VerificationData(DONOTDELETE).xlsx").Save
Workbooks("VerificationData(DONOTDELETE).xlsx").Close
End Sub
the issue laid within the MaxVal string, changed the code to get rid of it
new code
Private Sub GetMax()
Dim WorkRange As Range
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set WorkRange = ActiveWorkbook.Worksheets("Data").Range("AK:AK")
Workbooks("PIV-001.xlsm").Activate
ActiveWorkbook.Worksheets("PIV-001").Unprotect ("Moldamatic")
ActiveWorkbook.Worksheets("PIV-001").Range("U21").Value =
Range("U21").Value + 1
End Sub

Delete Data from VBA Excel

So currently i'm working on VBA forms(Product Master) , and got stuck across this problem. So basically, i have 3 buttons for my data i.e., Add, Update and Delete. Done with Add and Update button.
Just don't know how to delete my data from listbox which is in userform, and this user form is connected to a worksheet.
'''
Option Compare Text
Private Sub btn_ProductMaster_Add_Click()
If Me.txt_Product_Name.Value = "" Then
MsgBox "Please Enter Product Name", vbCritical
Exit Sub
End If
If IsNumeric(Me.txt_Purchase_Price) = False Then
MsgBox "Please Enter Product Price", vbCritical
Exit Sub
End If
If Me.txt_Product_colour.Value = "" Then
MsgBox "Please Enter Product Colour", vbCritical
Exit Sub
End If
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Product_Master")
Dim lr As Integer
lr = Application.WorksheetFunction.CountA(sh.Range("A:A"))
Me.txt_id = StrConv(Me.txt_Product_Name.Value, vbProperCase) & "_" & StrConv(Me.txt_Product_colour.Value, vbProperCase)
If Application.WorksheetFunction.CountIf(sh.Range("B:B"), Me.txt_id.Value) > 0 Then
MsgBox "This Product is already Available in product master", vbCritical
Exit Sub
End If
sh.Range("A" & lr + 1).Value = lr
sh.Range("B" & lr + 1).Value = Me.txt_id.Value
sh.Range("C" & lr + 1).Value = StrConv(Me.txt_Product_Name.Value, vbProperCase)
sh.Range("D" & lr + 1).Value = StrConv(Me.txt_Product_colour.Value, vbProperCase)
sh.Range("E" & lr + 1).Value = Me.txt_Purchase_Price.Value
Me.txt_id.Value = ""
Me.txt_Product_Name.Value = ""
Me.txt_Product_colour.Value = ""
Me.txt_Purchase_Price.Value = ""
MsgBox "Product has been added", vbInformation
Call show_data
End Sub
Sub show_data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Product_Master")
Dim lr As Integer
lr = Application.WorksheetFunction.CountA(sh.Range("A:A"))
If lr = 1 Then lr = 2
With Me.list_ProductMaster
.ColumnCount = 5
.ColumnHeads = True
.ColumnWidths = "40,110,110,80,80"
.RowSource = "Product_Master!A2:E" & lr
End With
End Sub
Private Sub btn_ProductMaster_Dlt_Click()
list_ProductMaster.SetFocus
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Product_Master")
Dim lr As Integer
lr = Me.txt_srno.Value
MsgBox lr
sh.Cells(lr + 1, "A").EntireRow.Delete
Me.txt_srno.Value = ""
Me.txt_id.Value = ""
Me.txt_Product_Name.Value = ""
Me.txt_Product_colour.Value = ""
Me.txt_Purchase_Price.Value = ""
MsgBox "Product has been Deleted", vbInformation
Call show_data
End Sub
Private Sub btn_ProductMaster_Extract_Click()
Dim nwb As Workbook
Set nwb = Workbooks.Add
ThisWorkbook.Sheets("Product_Master").UsedRange.Copy nwb.Sheets(1).Range("a1")
End Sub
Private Sub btn_ProductMaster_Updt_Click()
If Me.txt_Product_Name.Value = "" Then
MsgBox "Please Enter Product Name", vbCritical
Exit Sub
End If
If IsNumeric(Me.txt_Purchase_Price) = False Then
MsgBox "Please Enter Product Price", vbCritical
Exit Sub
End If
If Me.txt_Product_colour.Value = "" Then
MsgBox "Please Enter Product Colour", vbCritical
Exit Sub
End If
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Product_Master")
Dim lr As Integer
lr = Me.txt_srno.Value
Me.txt_id = StrConv(Me.txt_Product_Name.Value, vbProperCase) & "_" & StrConv(Me.txt_Product_colour.Value, vbProperCase)
sh.Range("A" & lr + 1).Value = lr
sh.Range("B" & lr + 1).Value = Me.txt_id.Value
sh.Range("C" & lr + 1).Value = StrConv(Me.txt_Product_Name.Value, vbProperCase)
sh.Range("D" & lr + 1).Value = StrConv(Me.txt_Product_colour.Value, vbProperCase)
sh.Range("E" & lr + 1).Value = Me.txt_Purchase_Price.Value
Me.txt_srno.Value = ""
Me.txt_id.Value = ""
Me.txt_Product_Name.Value = ""
Me.txt_Product_colour.Value = ""
Me.txt_Purchase_Price.Value = ""
MsgBox "Product has been Updated", vbInformation
Call show_data
End Sub
Private Sub list_ProductMaster_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.txt_srno.Value = Me.list_ProductMaster.List(Me.list_ProductMaster.ListIndex, 0)
Me.txt_id.Value = Me.list_ProductMaster.List(Me.list_ProductMaster.ListIndex, 1)
Me.txt_Product_Name.Value = Me.list_ProductMaster.List(Me.list_ProductMaster.ListIndex, 2)
Me.txt_Product_colour.Value = Me.list_ProductMaster.List(Me.list_ProductMaster.ListIndex, 3)
Me.txt_Purchase_Price.Value = Me.list_ProductMaster.List(Me.list_ProductMaster.ListIndex, 4)
End Sub
Private Sub UserForm_Activate()
Call show_data
End Sub
'''

Calculating time elapsed from current time in excel using VBA

In my program, a user fills out a userform which automatically generates the info and time that it was filled out on another excel sheet. I would like to use VBA to calculate the duration from the original entry time from the form to the current time. I don't understand why my code isn't working. When I try to use it, the duration keeps coming up as zero. I am not sure what I need to subtract from "Now()" in order to make this work and give me an actual value.
Here is the code:
Private Sub cmdOkUpdate_Click()
Dim length As Date
length = Format(Now(), "hh:mm:ss")
strBOL = txtBOL.Value
strID = txtID.Value
details = txtDet.Value
opt = lbxOption.Value
currtime = time()
today = Format(Now(), "MM/DD/YYYY")
emp = TextBox1.Value
dur = Format(Now() - currtime, "hh:mm:ss")
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
.Range("A1").Value = "Time"
.Range("B1").Value = "Date"
.Range("C1").Value = "Location"
.Range("D1").Value = "Category"
.Range("E1").Value = "BOL"
.Range("f1").Value = "Trailer #"
.Range("g1").Value = "Details"
.Range("H1").Value = "EE Name"
.Range("I1").Value = "Duration"
.Range("A2").EntireRow.Insert
.Range("A2").Value = currtime
.Range("B2").Value = today
.Range("C2").Value = spot
.Range("D2").Value = opt
.Range("E2").Value = strBOL
.Range("F2").Value = strID
.Range("G2").Value = details
.Range("H2").Value = emp
.Range("I2").Value = dur
.Columns("A:I").AutoFit
End With
If Not IsEmpty(opt) Then
cellFill = opt & " " & vbCrLf & "BOL (last 5 digits): " & strBOL & " " & vbCrLf & "Trailer # " & strID & " " & vbCrLf & "Details: " & details & " " & vbCrLf & "EE Name: " & emp & " " & vbCrLf
ActiveCell.Value = cellFill
Call RealTimeTracker
End If
End If
Unload Me
Sheet1.Activate
End Sub
#Leah, your code is actually working. If you try this (adapted from your code), you'll see it work:
Sub test()
currtime = Time()
waitTill = Now() + TimeValue("00:00:05")
While Now() < waitTill
DoEvents
Wend
dur = Format(Now() - currtime, "hh:mm:ss")
MsgBox (dur)
End Sub
The problem you are having is that dur is too close to currtime, thus, the actual time elapsed is 0.
You can try placing it lower in the code, like this:
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
'...
.Range("I2").Value = Format(Now() - currtime, "hh:mm:ss")
'...
End With
'...
End If
However, I don't think it will make a difference, because the code doesn't seem to be doing anything 'complicated' enough to take more than a second.

Global variable in userforms does not work

I am creating userforms. Userforms are pretty connected to each other - use informations one from another. So... I thought about creating few global variables to make my life easier:
Public nazwa_arkusza As String
Public skoroszyt As Workbooks
Public arkusz As Worksheet
They are wrote in Useform Klient_kraj. Edytuj is Combobox within Klient_kraj Userform. I want to execute arkusz variable in different userform, but I get run-time error: "Object does not support this method"
Private Sub but_next_Click()
Dim Faktura As Range, faktury_range As Range
Dim LastRow As Integer
LastRow = Klient_kraj.skoroszyt.arkusz.Cells(Rows.Count, 1).End(xlUp).Row 'error line
Set faktury_range = skoroszyt.Range("A1:A" & LastRow)
(...)
end sub
.
Private Sub edytuj_Click()
Dim nazwa As String
nazwa_arkusza = kraj.List(kraj.ListIndex, 1) & " " & Mid(okres1.Value, 4, 2) & Mid(okres2.Value, 3, 3)
nazwa = "C:\1\" & klient.Text & ".xlsx"
'Jeżeli kraj nie wybrany = msgbox
If kraj.Value = "" Then
MsgBox ("Nie wybrałeś kraju")
Exit Sub:
Else
If okres1.Value = "" Or okres2.Value = "" Then
MsgBox ("Nie wybrałeś okresu rozliczeniowego")
Exit Sub:
End If
End If
'Jeżeli nie ma pliku - utwórz nowy
If Dir(nazwa) = "" Then
Workbooks.Add(1).SaveAs Filename:="C:\1\" & klient.Text, FileFormat:=51
Worksheets(1).Name = nazwa_arkusza
Else
'Jeżeli nie jest otwarty - otwórz
On Error GoTo niema_pliku:
If GetObject(, "Excel.Application").Workbooks(Klient_kraj.klient.Text & ".xlsx") Is Nothing Then
Workbooks.Open Filename:="C:\1\" & klient.Text & ".xlsx"
Else
Workbooks(Klient_kraj.klient.Text).Activate
End If
End If
Set skoroszyt = Workbooks(Klient_kraj.klient.Text & ".xlsx")
'Jeżeli arkusz nie istnieje - utwórz; istnieje - aktywuj
On Error GoTo niema_arkusza:
If skoroszyt.Worksheets(nazwa_arkusza).Name = "" Then
Else
skoroszyt.Worksheets(nazwa_arkusza).Activate
End If
On Error GoTo 0
Set arkusz = Sheets(nazwa_arkusza)
Application.Windows(klient.Text & ".xlsx").Visible = False
Faktura.Show
niema_pliku:
If Err.Number = 9 Then
Resume Next
End If
niema_arkusza:
If Err.Number = 9 Then
skoroszyt.Worksheets.Add.Name = nazwa_arkusza
skoroszyt.Worksheets(nazwa_arkusza).Activate
Resume Next
End If
End Sub
This variable will also be used in different userform.
What am I doing wrong?

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