Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

Sub into a private sub and

$
0
0

I have written a macro which does as I need with respect to output. However, it would be ideal to have the command button to present itself immediately when I open up the workbook. I assume I need to make it a private sub, rather than a public sub but I am completely unfamiliar with this. Would someone be able to suggest how I can get this to work.

Additionally, I have sourced code which addresses when I want to exit out of the userform, or hit "cancel" or "ok". Would someone be able to tie this all together for me - It would be greatly appreciated.

enter image description here


Sub SerialSalesOrderComp()

Dim CodeSalesOrder As Variant
Dim TestSerial As Variant
Dim DeviceSerial As Variant
Dim SalesCodeLength As Integer
Dim TestSerialLength As Integer
Dim DeviceSerialLength As Integer
Dim RngD As Range

Range("D1:D3").Clear
StartHandler:
Do
CodeSalesOrder = InputBox("Please Scan Page...")

Range("D1").Value = CodeSalesOrder

SalesCodeLength = Len(CodeSalesOrder)

If SalesCodeLength = 13 Then Exit Do

MsgBox "Sales Code Requires Format"
MsgBox "Please Try Again"
GoTo StartHandler
Loop

StartHandler1:
Do
TestSerial = InputBox("Please Scan Test Page")

Range("D2").Value = TestSerial

TestSerialLength = Len(TestSerial)

If TestSerialLength = 11 Then Exit Do

MsgBox "Sales Code Requires Format YYY..."
MsgBox "Please Try Again"
GoTo StartHandler1

Loop

StartHandler2:
Do
DeviceSerial = InputBox("Please Scan")

Range("D3").Value = DeviceSerial

DeviceSerialLength = Len(DeviceSerial)

If DeviceSerialLength = 11 Then Exit Do
'Do Nothing


MsgBox "Serial Number Requires Format YYY..."
MsgBox "Please Try Again"
GoTo StartHandler2

Loop


SalesOrderNum = Left(CodeSalesOrder, InStr(CodeSalesOrder, "-") - 1)


If StrComp(TestSerial, DeviceSerial) = 0 Then


Dim TestSerialLetter As Variant
TestSerialNum1 = Left(Right(TestSerial, Len(TestSerial)), Len(TestSerial) - 8)
TestSerialLetter = Right(Left(TestSerialNum1, Len(TestSerialNum1)), Len(TestSerialNum1) - 2)


DeviceSerialNum1 = Left(Right(DeviceSerial, Len(DeviceSerial)), Len(DeviceSerial) - 8)
DeviceSerialNum = Right(Left(DeviceSerialNum1, Len(DeviceSerialNum1)), Len(DeviceSerialNum1) - 2)

Else:
MsgBox "Do Not Match - Report to Supervisor Immediately"
GoTo ExitHandler

End If

Dim RngNomenclature As Variant
Dim DeviceNumCode1 As String


RngNomenclature = ThisWorkbook.Sheets(1).Range("A2:B"& ThisWorkbook.Sheets(1).Cells(ThisWorkbook.Sheets(1).Rows.Count, 1).End(xlUp).Row).Value2

DeviceNumCode = Application.WorksheetFunction.VLookup(TestSerialLetter, RngNomenclature, 2, False)

If StrComp(DeviceNumCode, SalesOrderNum, vbBinaryCompare) = 0 Then

MsgBox "Device Label Type Verification Complete"

Else:
MsgBox "Do Not Match - Report to Supervisor Immediately"
GoTo ExitHandler

End If

ExitHandler:

End Sub


Public Property Get Cancelled() As Boolean
    Cancelled = IsCancelled
End Property
Private Sub OkButton_Click()
    Me.Hide
End Sub

Private Sub CancelButton_Click()
    OnCancel
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        Cancel = True
        OnCancel
    End If
End Sub

Private Sub OnCancel()
    IsCancelled = True
    Me.Hide
End Sub

'Private IsCancelled As Boolean

The macro itself is used for checking the 3 strings in column D match with certain character intelligence.

Overall the first 3 letters in cell D1 are compared to a "Number" in column B. This "Number" in column B is derived by taking the 3rd character in Cells D2 and D3. This 3rd character is compared to Column A. It finds it match, then takes the corresponding "Number" from column B in the same row. This "Number" from Column B is then compared to the first 3 letters in cell D1. If it finds a match it is verified. If it doesn't, report it.


Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>