0% found this document useful (0 votes)
8 views3 pages

New Text Document

The provided code is a VBA subroutine that handles the submission of a purchase transaction through a UserForm. It validates input data, retrieves relevant information from specified worksheets, updates stock and ledger records, and provides feedback to the user regarding the transaction status. Additionally, it checks for unique page numbers in the ledger and clears the UserForm fields after processing the transaction.

Uploaded by

f60simple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

New Text Document

The provided code is a VBA subroutine that handles the submission of a purchase transaction through a UserForm. It validates input data, retrieves relevant information from specified worksheets, updates stock and ledger records, and provides feedback to the user regarding the transaction status. Additionally, it checks for unique page numbers in the ledger and clears the UserForm fields after processing the transaction.

Uploaded by

f60simple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Private Sub btnSubmitPurchase_Click()

Dim wsDelivery As Worksheet, wsStock As Worksheet, wsLedger As Worksheet


Dim lastRow As Long, deliveryRow As Long, ledgerRow As Long
Dim diaryNo As String, itemName As String, category As String
Dim qtyPurchased As Double, perUnitPrice As Double, totalCost As Double
Dim supplierName As String, deliveryDate As Date
Dim ledgerSheetName As String
Dim uniquePages As Object
Dim pageNumber As Variant
Dim cell As Range

' Sheets Assign Karna


Set wsDelivery = ThisWorkbook.Sheets("Delivery Challan")
Set wsStock = ThisWorkbook.Sheets("Stock Transactions")

' UserForm se Data Lena


diaryNo = Me.txtDiaryNoPurchase.Value
category = Me.cmbCategorypurchase.Value
itemName = Me.cmbItemNamePurchase.Value
qtyPurchased = Val(Me.txtqtyPurchased.Value)
perUnitPrice = Val(Me.txtperunitprice.Value)

' Validation - Empty Fields Check


If diaryNo = "" Or category = "" Or itemName = "" Or qtyPurchased = 0 Or
perUnitPrice = 0 Then
MsgBox "Please fill all required fields.", vbExclamation, "Missing Data"
Exit Sub
End If

' Delivery Challan me Diary No. Search Karna


On Error Resume Next
deliveryRow = wsDelivery.Range("A:A").Find(What:=diaryNo, LookAt:=xlWhole).Row
On Error GoTo 0

If deliveryRow = 0 Then
MsgBox "Diary No. not found in Delivery Challan!", vbCritical, "Error"
Exit Sub
End If

' Delivery Challan se Data Fetch Karna


deliveryDate = wsDelivery.Cells(deliveryRow, 2).Value ' Delivery Date
supplierName = wsDelivery.Cells(deliveryRow, 3).Value ' Supplier Name

' Ledger Sheet ka Name Determine Karna


ledgerSheetName = itemName
On Error Resume Next
Set wsLedger = ThisWorkbook.Sheets(ledgerSheetName)
On Error GoTo 0

' Ledger Sheet agar exist nahi karti to user se poochna


If wsLedger Is Nothing Then
MsgBox "Ledger sheet '" & itemName & "' not found!", vbCritical, "Error"
Exit Sub
End If

' Last Balance Calculate Karna


ledgerRow = wsLedger.Cells(Rows.Count, 1).End(xlUp).Row
Dim balance As Double
If ledgerRow > 1 Then
balance = Val(wsLedger.Cells(ledgerRow, 7).Value) ' Ensure it's a number
Else
balance = 0
End If

' **Updated Balance Calculation**


balance = balance + Val(qtyPurchased)

' Total Cost Calculate Karna


totalCost = qtyPurchased * perUnitPrice

' Stock Transactions me Data Add Karna


lastRow = wsStock.Cells(Rows.Count, 1).End(xlUp).Row + 1
wsStock.Cells(lastRow, 1).Value = lastRow - 1 ' Sr. No.
wsStock.Cells(lastRow, 2).Value = deliveryDate
wsStock.Cells(lastRow, 3).Value = supplierName
wsStock.Cells(lastRow, 4).Value = diaryNo
wsStock.Cells(lastRow, 5).Value = category
wsStock.Cells(lastRow, 6).Value = itemName
wsStock.Cells(lastRow, 7).Value = qtyPurchased
wsStock.Cells(lastRow, 8).Value = perUnitPrice
wsStock.Cells(lastRow, 9).Value = totalCost
wsStock.Cells(lastRow, 10).Value = balance ' Updated Balance
wsStock.Cells(lastRow, 12).Value = "Added via UserForm"

' Ledger Sheet me Data Add Karna (Stock Transaction ke Sath Sath)
ledgerRow = wsLedger.Cells(Rows.Count, 1).End(xlUp).Row + 1
wsLedger.Cells(ledgerRow, 1).Value = ledgerRow - 1 ' Sr. No.
wsLedger.Cells(ledgerRow, 2).Value = deliveryDate
wsLedger.Cells(ledgerRow, 3).Value = supplierName
wsLedger.Cells(ledgerRow, 4).Value = diaryNo
wsLedger.Cells(ledgerRow, 5).Value = qtyPurchased
wsLedger.Cells(ledgerRow, 7).Value = balance ' Updated Balance
wsLedger.Cells(ledgerRow, 8).Value = perUnitPrice
wsLedger.Cells(ledgerRow, 9).Value = totalCost
wsLedger.Cells(ledgerRow, 12).Value = "Updated via UserForm"

' Page Number Fetch Karna (Column 13 se)


Set uniquePages = CreateObject("Scripting.Dictionary") ' Dictionary to store
unique values

' Ledger sheet ke column 13 ko check karna


For Each cell In wsLedger.Range("M2:M" & ledgerRow) ' Column 13 (M)
If Not IsEmpty(cell.Value) Then
uniquePages(cell.Value) = 1 ' Dictionary me unique values store karna
End If
Next cell

' Agar multiple unique page numbers hain to warning show karni hai
If uniquePages.Count > 1 Then
MsgBox "Multiple different page numbers found in Ledger Sheet!" & vbNewLine
& _
"Please verify the page numbers manually.", vbExclamation, "Warning"
ElseIf uniquePages.Count = 1 Then
' Sirf ek page number ho to show karna
For Each pageNumber In uniquePages.Keys
MsgBox "Stock transaction successfully recorded!" & vbNewLine & _
"Ledger Sr. No: " & ledgerRow - 1 & vbNewLine & _
"Page Number: " & pageNumber, vbInformation, "Success"
Next pageNumber
Else
' Agar koi page number enter nahi hua to default message
MsgBox "Stock transaction successfully recorded!" & vbNewLine & _
"Ledger Sr. No: " & ledgerRow - 1 & vbNewLine & _
"Page Number: Not Assigned", vbInformation, "Success"
End If

' UserForm Clear Karna


Me.txtDiaryNoPurchase.Value = ""
Me.cmbCategorypurchase.Value = ""
Me.cmbItemNamePurchase.Clear
Me.txtqtyPurchased.Value = ""
Me.txtperunitprice.Value = ""

End Sub

You might also like