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
Related
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
'''
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.
I'm new in programming. this is the first system application that I did. I'm struggling in closing or killing the excel application once the system has successfully uploaded in a repository. this is my code. I do hope you can help me to fix it. thank you in advance.
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
srcXlApp = New Excel.Application
dstXlApp = New Excel.Application
Dim numToDo As Integer = CInt(e.Argument)
Dim ComputerName As String
Dim strMissingValues As String
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(srcXlWb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(dstXlWb)
isValidUploading = True
strMissingValues = "Cannot upload there are missing fields: "
bgwState = e
strRemarks = txtRemarks.Text
srcXlApp.DisplayAlerts = False
srcXlWbs = srcXlApp.Workbooks
srcXlWb = srcXlWbs.Open(srcFilePath)
srcXlWrksht = srcXlWb.Worksheets(1)
srcXlApp.Visible = False
'Step 1 - open source
bgw.ReportProgress(Convert.ToInt32((1 / numToDo) * 100))
'System.Threading.Thread.Sleep(5000)
ComputerName = System.Net.Dns.GetHostName
'dstFilePath = "C:\Users\" & ComputerName & "\Desktop\Mazza\trunk\QA Task Monitoring_2020.xlsx"
If ComputerName = "jonas" Then
'MessageBox.Show(ComputerName)
dstFilePath = "C:\Users\jonas.ONE-SOURCE\Desktop\Mazza\trunk\QA Task Monitoring_for_testing_only.xlsx"
Else
dstFilePath = "C:\Users\" & ComputerName & "\Desktop\Mazza\trunk\QA Task Monitoring_for_testing_only.xlsx"
End If
'dstFilePath = "C:\Users\jonas.ONE-SOURCE\Desktop\Mazza\trunk\QA Task Monitoring_2020.xlsx"
'dstFilePath = "C:\Users\" & ComputerName & "\Desktop\Mazza\trunk\QA Task Monitoring_for_testing_only.xlsx"
dstXlApp.DisplayAlerts = False
dstXlWbs = dstXlApp.Workbooks
dstXlWb = dstXlWbs.Open(dstFilePath)
dstXlApp.Visible = False
'Step 2 - open destination
bgw.ReportProgress(Convert.ToInt32((2 / numToDo) * 100))
'System.Threading.Thread.Sleep(5000)
'Step 3 - Copy Range
bgw.ReportProgress(Convert.ToInt32((3 / numToDo) * 100))
'System.Threading.Thread.Sleep(3000)
'Code for copying cells
srcXlWrksht = srcXlWb.Worksheets(1)
strTicketNumber = srcXlWrksht.Range("B3").Value
strCID = srcXlWrksht.Range("B4").Value
strIteration = srcXlWrksht.Range("B13").Value
If Not IsNothing(strIteration) Then
strIteration = strIteration.Substring(2)
End If
strSystem = srcXlWrksht.Range("B17").Value
strAssignedBy = srcXlWrksht.Range("B10").Value
strPMOBA = srcXlWrksht.Range("B9").Value
strRequest = srcXlWrksht.Range("B5").Value
strAssign = srcXlWrksht.Range("B6").Value
strStart = srcXlWrksht.Range("B14").Value
strEnd = srcXlWrksht.Range("B15").Value
strStatus = srcXlWrksht.Range("C24").Value
strTask = srcXlWrksht.Range("B16").Value
If srcXlWb.Worksheets.Count >= 2 Then
srcXlWrksht = srcXlWb.Worksheets(2)
strDescription = srcXlWrksht.Range("D3").Value
Else
'strMissingValues = strMissingValues & vbCrLf & "Title/Description"
isValidUploading = False
End If
'srcXlWrksht = srcXlWb.Worksheets(2)
'strDescription = srcXlWrksht.Range("D3").Value
If strTicketNumber = "" Then
strMissingValues = strMissingValues & vbCrLf & "Ticket ID"
isValidUploading = False
End If
If strCID = "" Then
strMissingValues = strMissingValues & vbCrLf & "Change ID"
isValidUploading = False
End If
If strIteration = "" Then
strMissingValues = strMissingValues & vbCrLf & "Iteration Number"
isValidUploading = False
End If
If strSystem = "" Then
strMissingValues = strMissingValues & vbCrLf & "System Name"
isValidUploading = False
End If
If strAssignedBy = "" Then
strMissingValues = strMissingValues & vbCrLf & "Assigned By"
isValidUploading = False
End If
If strPMOBA = "" Then
strMissingValues = strMissingValues & vbCrLf & "Assigned PMO/BA"
isValidUploading = False
End If
If strRequest = "" Then
strMissingValues = strMissingValues & vbCrLf & "Requested Date"
isValidUploading = False
End If
If strAssign = "" Then
strMissingValues = strMissingValues & vbCrLf & "Assigned Date"
isValidUploading = False
End If
If strStart = "" Then
strMissingValues = strMissingValues & vbCrLf & "Start Date"
isValidUploading = False
End If
If strEnd = "" Then
strMissingValues = strMissingValues & vbCrLf & "End Date"
isValidUploading = False
End If
If strStatus = "" Then
strMissingValues = strMissingValues & vbCrLf & "Status"
isValidUploading = False
End If
If strTask = "" Then
strMissingValues = strMissingValues & vbCrLf & "Task type"
isValidUploading = False
End If
If strDescription = "" Then
strMissingValues = strMissingValues & vbCrLf & "Title/Description"
isValidUploading = False
End If
'If CheckBox1.CheckState = CheckState.Checked And txtRemarks.Text = "" Then
' strMissingValues = strMissingValues & vbCrLf & "Empty Remarks"
' MessageBox.Show("Please input remarks", "Empty Remarks", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
' isValidUploading = False
'End If
If Not isValidUploading Then
MessageBox.Show(strMissingValues, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
srcXlWb.Close()
dstXlWb.Close()
srcXlApp.UserControl = True
dstXlApp.UserControl = True
srcXlApp.Quit()
dstXlApp.Quit()
Marshal.ReleaseComObject(dstXlWrksht)
Marshal.ReleaseComObject(dstXlWb)
Marshal.ReleaseComObject(dstXlWbs)
Marshal.ReleaseComObject(dstXlApp)
Marshal.ReleaseComObject(dstXlWrksht)
Marshal.ReleaseComObject(srcXlWb)
Marshal.ReleaseComObject(srcXlWbs)
Marshal.ReleaseComObject(srcXlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
bgw.CancelAsync()
e.Cancel = True
Exit Sub
End If
'strTicketNumber = srcXlWrksht.Range("B4").Value.ToString
'code for pastespecial
dstXlWb.Worksheets("Tasks").Range("A3").Select()
Last_Row = dstXlWb.Worksheets("Tasks").Range("A3").End(Excel.XlDirection.xlDown).Row + 1
dstXlWb.Worksheets("Tasks").Range("A" & Last_Row).Select()
'Step 4 - Pasting values
bgw.ReportProgress(Convert.ToInt32((4 / numToDo) * 100))
'System.Threading.Thread.Sleep(3000)
dstXlWb.Worksheets("Tasks").Range("C" & Last_Row).Value = strTicketNumber
dstXlWb.Worksheets("Tasks").Range("B" & Last_Row).Value = strQAT
dstXlWb.Worksheets("Tasks").Range("D" & Last_Row).Value = strCID
dstXlWb.Worksheets("Tasks").Range("E" & Last_Row).Value = strIteration
dstXlWb.Worksheets("Tasks").Range("F" & Last_Row).Value = strDescription
dstXlWb.Worksheets("Tasks").Range("G" & Last_Row).Value = strSystem
dstXlWb.Worksheets("Tasks").Range("H" & Last_Row).Value = strAssignedBy
dstXlWb.Worksheets("Tasks").Range("I" & Last_Row).Value = strPMOBA
dstXlWb.Worksheets("Tasks").Range("J" & Last_Row).Value = strSBU
dstXlWb.Worksheets("Tasks").Range("K" & Last_Row).Value = strTypes
dstXlWb.Worksheets("Tasks").Range("L" & Last_Row).Value = strProcess
dstXlWb.Worksheets("Tasks").Range("M" & Last_Row).Value = strRequest
dstXlWb.Worksheets("Tasks").Range("N" & Last_Row).Value = strAssign
dstXlWb.Worksheets("Tasks").Range("O" & Last_Row).Value = strStart
dstXlWb.Worksheets("Tasks").Range("P" & Last_Row).Value = strEnd
dstXlWb.Worksheets("Tasks").Range("Q" & Last_Row).Value = strStatus
dstXlWb.Worksheets("Tasks").Range("S" & Last_Row).Value = strRemarks
dstXlWb.Worksheets("Tasks").Range("Y" & Last_Row).Value = strTask
dstXlWb.Worksheets("Tasks").Range("W" & Last_Row).Value = "=+TEXT(QATM[[#This Row],[End Date]]," & """MM""" & ")"
dstXlWb.Worksheets("Tasks").Range("X" & Last_Row).Value = "=+TEXT(QATM[[#This Row],[End Date]]," & """YYYY""" & ")"
If CheckBox1.CheckState = CheckState.Checked Then
dstXlWb.Worksheets("Tasks").Range("R" & Last_Row).Value = "=""FOR QAT"" " & "& " & "TEXT(" & dstXlWb.Worksheets("Tasks").Range("E" & Last_Row).Value + 1 & ", ""00"")"
Else
dstXlWb.Worksheets("Tasks").Range("R" & Last_Row).Value = "YES"
End If
'Step 5 - Saving
bgw.ReportProgress(Convert.ToInt32((5 / numToDo) * 100))
'System.Threading.Thread.Sleep(3000)
dstXlApp.ActiveWorkbook.Save()
Catch ex As Exception
'MsgBox(ex.Message)
Me.Cursor = Cursors.Default
MessageBox.Show("Please check excel file.")
'MsgBox("Error has occured. " & ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error")
bgw.CancelAsync()
Exit Sub
srcXlWb.Close()
dstXlWb.Close()
srcXlApp.UserControl = True
dstXlApp.UserControl = True
srcXlApp.Quit()
dstXlApp.Quit()
Marshal.ReleaseComObject(dstXlWb)
Marshal.ReleaseComObject(dstXlWbs)
Marshal.ReleaseComObject(dstXlApp)
Marshal.ReleaseComObject(srcXlWb)
Marshal.ReleaseComObject(srcXlWbs)
Marshal.ReleaseComObject(srcXlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
Finally
'srcXlWb.Close()
'dstXlWb.Close()
'srcXlApp.UserControl = True
'dstXlApp.UserControl = True
'srcXlApp.Quit()
'dstXlApp.Quit()
Marshal.ReleaseComObject(dstXlWb)
Marshal.ReleaseComObject(dstXlWbs)
Marshal.ReleaseComObject(dstXlApp)
Marshal.ReleaseComObject(srcXlWb)
Marshal.ReleaseComObject(srcXlWbs)
Marshal.ReleaseComObject(srcXlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr,
ByRef lpdwProcessId As Integer) As Integer
Friend Sub KillSpecificExcel(xlsApplication As Excel.Application)
Try
Dim psi As ProcessStartInfo = New ProcessStartInfo
Dim XLProcID As Integer = 0
Dim hwd As Integer
hwd = xlsApplication.Hwnd
GetWindowThreadProcessId(hwd, XLProcID)
Dim XLProc As Process = Process.GetProcessById(XLProcID)
psi.Arguments = $"/PID {XLProc.Id} /T /F"
psi.FileName = "taskkill"
Dim p As Process = New Process()
p.StartInfo = psi
p.Start()
Catch ex As Exception
Throw
End Try
End Sub
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
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