Excel Userform could not submit after validating entries (VBA) - excel

I have created a userform which would validate if entry is of email format, if it is of directory file format, if all enteries except for the first activity is filled. However when I click submit, the values are not added into excel. Kindly advise me as I am really lost. Thanks.
Private Sub CommandButton1_Click()
Dim tDate As Date
Dim LastRow As Long
Dim strValue As String
Dim msg As String
strValue = TextBox5.Value
LastRow = ActiveSheet.Range("E65536").End(xlUp).Row + 1
If Not (TextBox5.Value = " " And TextBox1.Value = " " And (IsNull(ComboBox1.Value) = True) And (IsNull(ComboBox2.Value) = True) And TextBox6.Value = "" And TextBox4.Value = "" And (IsNull(MonthView1.Value) = True)) Then
With CreateObject("vbscript.regexp")
.Pattern = "^[\w-\.]+#([\w-]+\.)+[A-Za-z]{2,3}$"
If .test(TextBox6.Value) Then
'MsgBox "Added"
GoTo GoToHere
Else
MsgBox "Invalid"
Cancel = True
End If
End With
GoToHere: ElseIf Not (InStr(strValue, "C:\") = 1) Then
MsgBox "Please start your directory with 'C:\'"
Else
ActiveSheet.Range("Q" & LastRow).Value = Me.TextBox5
ActiveSheet.Range("E" & LastRow).Value = Me.TextBox1
'tDate = CDate(TextBox2.Text)
ActiveSheet.Range("G" & LastRow).Value = Me.MonthView1
'Format(tDate, "dd/mm/yy")
ActiveSheet.Range("H" & LastRow).Value = Me.ComboBox1
ActiveSheet.Range("I" & LastRow).Value = Me.TextBox3
ActiveSheet.Range("J" & LastRow).Value = Me.ComboBox2
ActiveSheet.Range("M" & LastRow).Value = Me.TextBox4
ActiveSheet.Range("C" & LastRow).Value = Me.TextBox7
ActiveSheet.Range("S" & LastRow).Value = Me.TextBox6
End If
End Sub

Solved!!! Yeah... Here is my solution.
Private Sub CommandButton1_Click()
Dim tDate As Date
Dim LastRow As Long
Dim strValue As String
Dim msg As String
strValue = TextBox5.Value
LastRow = ActiveSheet.Range("E65536").End(xlUp).Row + 1
If Not (TextBox5.Value = " " And TextBox2.Value = " " And TextBox3.Value = " " And (IsNull(ComboBox1.Value) = True) And (IsNull(ComboBox2.Value) = True) And TextBox4.Value = "" And (IsNull(MonthView1.Value) = True)) Then
'If Not (TextBox6.Value = "") Then
With CreateObject("vbscript.regexp")
.Pattern = "^[\w-\.]+#([\w-]+\.)+[A-Za-z]{2,3}$"
If .test(TextBox4.Value) Then
'MsgBox "Added"
GoTo GoToHere
Else
MsgBox "Invalid"
Cancel = True
End If
End With
GoToHere: If Not (InStr(strValue, "C:\") = 1) Then
MsgBox "Please start your directory with 'C:\'"
Else
ActiveSheet.Range("Q" & LastRow).Value = Me.TextBox4
ActiveSheet.Range("E" & LastRow).Value = Me.TextBox2
'tDate = CDate(TextBox2.Text)
ActiveSheet.Range("G" & LastRow).Value = Me.MonthView1
'Format(tDate, "dd/mm/yy")
ActiveSheet.Range("H" & LastRow).Value = Me.ComboBox1
ActiveSheet.Range("I" & LastRow).Value = Me.TextBox3
ActiveSheet.Range("J" & LastRow).Value = Me.ComboBox2
ActiveSheet.Range("M" & LastRow).Value = Me.TextBox7
ActiveSheet.Range("C" & LastRow).Value = Me.TextBox1
ActiveSheet.Range("S" & LastRow).Value = Me.TextBox5
End If
End If
End Sub

Related

Excel vba macro intermittently fails to send data from one worksheet to another

I have a macro that open a closed workbook
sends data to this workbook, saves, then closes
sometimes it fails to send some of the data, or All of the data.
But doesn't throw ant error messages.
Public Sub PushUpdate315_CLIP()
If Range("H49").Value = "QCS UPLOADED" Then
MsgBox ("QCS had already been uploaded, if you believe this is an error, Please see Supervisor")
Exit Sub
End If
If Range("G54").Value = "" Then
Dim coreTime As Long
Dim lidTime As Long
Dim caseTime As Long
Dim finalTime As Long
coreValue = Range("L13").Value
lidValue = Range("L19").Value
caseValue = Range("L23").Value
finalValue = Range("L44").Value
If coreValue <= 0.0034722 Then
MsgBox ("Core Time is NOT Valid")
Exit Sub
End If
If lidValue <= 1.04166666666667E-02 Then
MsgBox ("Lid Time is NOT Valid")
Exit Sub
End If
If caseValue <= 0.002777778 Then
MsgBox ("Case Time is NOT Valid")
Exit Sub
End If
If finalValue <= 0.006944444 Then
MsgBox ("Final Time is NOT Valid")
Exit Sub
End If
End If
'315 Push update
Dim serverFileName As String 'obtain url for sharepoint filename, insert below
Dim x As New Excel.Application 'make a new session for the sharepoint version
Dim w As Workbook 'grab-handle for the sharepoint file
x.Visible = True
x.ScreenUpdating = True
serverFileName = "SUPERSECRETLINK HERE/Module Impedances.xlsx"
Set w = x.Workbooks.Open(serverFileName) 'open the sharepoint version
w.Save
Application.Wait (Now + TimeValue("0:00:1"))
If sheetExists(Range("B6").Text, w) Then
'w.Sheets(Range("B6").Text).Range("A2").Value = "test"
lastrow = w.Sheets(Range("B6").Text).Range("A10000").End(xlUp).Row + 1
w.Sheets(Range("B6").Text).Range("A" & lastrow).Value = Range("B5").Text ' WO
w.Sheets(Range("B6").Text).Range("B" & lastrow).Value = Range("B7").Text ' Serial
w.Sheets(Range("B6").Text).Range("D" & lastrow).Value = Range("D11").Text ' Core Imp
'w.Sheets(Range("B6").Text).Range("E" & lastrow).Value = Range("D28").Text ' Take Off Imp
w.Sheets(Range("B6").Text).Range("F" & lastrow).Value = Range("D26").Text ' Term Imp
w.Sheets(Range("B6").Text).Range("G" & lastrow).Value = Range("H13").Text ' Core Time
w.Sheets(Range("B6").Text).Range("H" & lastrow).Value = Range("H19").Text ' Case Time (lid)
w.Sheets(Range("B6").Text).Range("I" & lastrow).Value = Range("H23").Text ' mini bms (Sink Core)
w.Sheets(Range("B6").Text).Range("J" & lastrow).Value = Range("H44").Text 'cabling time (finsihing)
w.Sheets(Range("B6").Text).Range("K" & lastrow).Value = Range("H45").Text ' balance time
'w.Sheets(Range("B6").Text).Range("L" & lastrow).Value = Range("H36").Text ' testing
w.Sheets(Range("B6").Text).Range("M" & lastrow).Value = Range("I47").Text ' total labor
w.Sheets(Range("B6").Text).Range("N" & lastrow).Value = Range("I49").Text ' total labor with waiting
w.Sheets(Range("B6").Text).Range("O" & lastrow).Value = Range("I49").Text ' total lead time
w.Sheets(Range("B6").Text).Range("P" & lastrow).Value = (Range("H13").Value) * 1440
w.Sheets(Range("B6").Text).Range("P" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("Q" & lastrow).Value = (Range("H19").Value) * 1440
w.Sheets(Range("B6").Text).Range("Q" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("R" & lastrow).Value = (Range("H23").Value) * 1440
w.Sheets(Range("B6").Text).Range("R" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("S" & lastrow).Value = (Range("H44").Value) * 1440
w.Sheets(Range("B6").Text).Range("S" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("T" & lastrow).Value = (Range("H45").Value) * 1440
w.Sheets(Range("B6").Text).Range("T" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("V" & lastrow).Value = (Range("I47").Value) * 1440
w.Sheets(Range("B6").Text).Range("V" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("W" & lastrow).Value = (Range("I49").Value) * 1440
w.Sheets(Range("B6").Text).Range("W" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("X" & lastrow).Value = (Range("I49").Value) * 1440
w.Sheets(Range("B6").Text).Range("X" & lastrow).NumberFormat = "General"
w.Sheets(Range("B6").Text).Range("Y" & lastrow).Value = MonthName(Month(Range("G44").Text), True)
w.Sheets(Range("B6").Text).Range("Z" & lastrow).Value = Year(Range("G44").Text)
w.Sheets(Range("B6").Text).Range("AA" & lastrow).Value = MonthName(Month(Range("G44").Text), True) + CStr(Year(Range("G44").Text))
w.Sheets(Range("B6").Text).Range("AG" & lastrow).Value = Range("G44").Text
w.Save
w.Close
x.Quit
MsgBox ("Data sent to Metrics")
Range("H49").Value = "QCS UPLOADED"
BuildUnit
Else
MsgBox ("This Battery Type does not exists yet in the databse, Please contact QCS Site data admin")
End If
On Error Resume Next
Workbooks("Macros.xlsm").Close
On Error GoTo 0
End Sub
Its really strange how some values get sent and others dont.
for example:
w.Sheets(Range("B6").Text).Range("F" & lastrow).Value = Range("D26").Text ' Term Imp
w.Sheets(Range("B6").Text).Range("G" & lastrow).Value = Range("H13").Text ' Core Time
w.Sheets(Range("B6").Text).Range("H" & lastrow).Value = Range("H19").Text ' Case Time (lid)
w.Sheets(Range("B6").Text).Range("I" & lastrow).Value = Range("H23").Text ' mini bms (Sink Core)
I've literally seen where the first and last value get sent but not the ones the middle?

While executing the report macro based report and getting error "Subscript out of range"

The script is running in Citrix VDI login, but it is getting an error in AWS login. I tried all the options as per Stack Overflow guidance and still the issue is coming.
Here is the code:
This particular code will copy the data from the input sheets and extract the data in new workbook with five individual tabs.
Dim CurBookName As String
Dim NewBookName As String
Dim sServerName As String
Private Sub CheckBox1_Change()
'If checkbox is checked, all the products in the listbox will be selected. If checkbox is unchecked
'all the products in the listbox will be deselected.
If CheckBox1.Value = True Then
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = True
Next
ElseIf CheckBox1.Value = False Then
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = False
Next
End If
End Sub
Private Sub CommandButton1_Click()
'This code creates new Workbook and adds required Worksheets to it
On Error GoTo errHandler
Dim i As Integer, j As Integer
Dim bSelFlag As Boolean
Dim sFormula As String
Dim sVersion As String
Dim sPath As String
Dim sRepVerVal As String, sComVerVal As String
bSelFlag = False
For j = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(j) = True Then
bSelFlag = True
Exit For
End If
Next j
If bSelFlag = False Then
MsgBox "Please select one or more products from the product list"
Exit Sub
End If
CurBookName = ActiveWorkbook.name
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
SetMenuParameters (i)
' 1. Create blank workbook
' 2. Copy all format to new workbook
' 3. Copy all value to new workbook
Workbooks.Add
NewBookName = ActiveWorkbook.name
Workbooks(NewBookName).Worksheets.Add().name = "Total Summary"
Workbooks(NewBookName).Worksheets.Add().name = "Flow"
Workbooks(NewBookName).Worksheets.Add().name = "Variance"
Workbooks(NewBookName).Worksheets.Add().name = "TBA"
Workbooks(NewBookName).Worksheets.Add().name = "Percentage"
Workbooks(NewBookName).Worksheets.Add().name = "Total"
Application.DisplayAlerts = False
Workbooks(NewBookName).Worksheets("sheet1").Delete
Workbooks(NewBookName).Worksheets("sheet2").Delete
Workbooks(NewBookName).Worksheets("sheet3").Delete
Application.DisplayAlerts = True
Windows(CurBookName).Activate
Application.StatusBar = "Please wait....Copy Process is in progress for the Product '" & ListBox1.List(i) & "' ..."
CopySheet "Total Input", "Total"
CopySheet "Price Input", "Percentage"
TBA_Built = False
CopySheet "TBA", "TBA"
Var_Built = False
CopySheet "Variance", "Variance"
CF_Built = False
CopySheet "Flow", "Flow"
CopySheet "Total Summary", "Total Summary"
sVersion = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 8).Value
sPath = "\\user\shunt\"
If val(Application.Version) > 11 Then
Call Hide_Rows_Snapshot(NewBookName, "Percentage")
NewBookName = "TM1 " & ListBox1.List(i) & " TBA (" & sVersion & ")_" & Format(Now(), "MMDDYYHHMMSS") & ".xlsx"
ElseIf val(Application.Version) = 11 Then
NewBookName = "TM1 " & ListBox1.List(i) & " TBA (" & sVersion & ")_" & Format(Now(), "MMDDYYHHMMSS") & ".xls"
End If
ActiveWorkbook.SaveAs filename:=sPath & Replace(NewBookName, "/", "_")
ActiveWindow.Close
End If
Next
MsgBox "Copy Process has been done successfully!" & vbCrLf & vbCrLf & "Please find the Snapshots at " & sPath
Application.StatusBar = ""
Exit Sub
errHandler:
MsgBox Err.Description
End Sub
Private Sub Worksheet_Activate()
'This code populates TBA products
Dim iMonth As Integer, iYear As Integer, sYear As Integer
Dim sMonth As String, sVersion As String, sVerFormula As String
Dim sVarVersion As String, sVarVerFormula As String
PopulateProducts
iYear = Format(Date, "yyyy")
sMonth = Format(Date, "mmm")
sMonthnum = Month(Date)
If sMonth = "Jan" Or sMonth = "Apr" Or sMonth = "Jul" Or sMonth = "Oct" Then
sMonthnum = sMonthnum + 1
sYear = iYear
ElseIf sMonth = "Mar" Or sMonth = "Jun" Or sMonth = "Sep" Then
sMonthnum = sMonthnum + 2
sYear = iYear
ElseIf sMonth = "Dec" Then
sMonthnum = sMonthnum - 10
iYear = iYear + 1
sYear = iYear
Else
sMonthnum = sMonthnum
sYear = iYear
End If
sMonth = MonthName(sMonthnum, True)
sVersion = sMonth & "_" & Mid(CStr(iYear), 3, 2) & "_Forecast"
'Setting default values to Report Parameters
Worksheets("CreateSnapshot").Cells(9, 8) = sVersion
sServerName = Worksheets("Globals").Range("Server_Name").Value
sVerFormula = "SUBNM(" & """" & sServerName & ":Version""" & "," & """" & """,""" & sVersion & """)"
Worksheets("CreateSnapshot").Cells(9, 8).Formula = "=" & sVerFormula
Worksheets("CreateSnapshot").Cells(10, 8) = Format(Date, "yyyy") - 1
'Setting default values to Variance Parameters
iMonth = Month(Now)
iYear = Year(Now)
iYear = Format(Date, "yyyy")
iMonth = sMonthnum
If iMonth = 2 Then
If iYear <> sYear Then
iMonth = 11
Else
iMonth = 11
iYear = iYear - 1
End If
Else
iMonth = iMonth - 3
End If
sVarVersion = MonthName(iMonth, True) & "_" & Mid(CStr(iYear), 3, 2) & "_Forecast"
Worksheets("CreateSnapshot").Cells(9, 11) = sVarVersion
sVarVerFormula = "SUBNM(" & """" & sServerName & ":Version""" & "," & """" & """,""" & sVarVersion & """)"
Worksheets("CreateSnapshot").Cells(9, 11).Formula = "=" & sVarVerFormula
End Sub
Public Sub PopulateProducts()
'The listbox is populated with Products from TBA_Products subset
Dim SubsetSize As Integer, i As Integer
Dim ElName As String
SubsetSize = Application.Run("SUBSIZ", ThisWorkbook.Worksheets(GLOBALS).Range("Server_Name") & ":Product", "TBA_Products")
ListBox1.Clear
i = 1
While i <= SubsetSize
ElName = Application.Run("SUBNM", ThisWorkbook.Worksheets(GLOBALS).Range("Server_Name") & ":Product", "TBA_Products", i, "Description")
ListBox1.AddItem (ElName)
i = i + 1
Wend
ListBox1.Height = 220
ListBox1.MultiSelect = fmMultiSelectMulti
End Sub
Private Sub CopySheet(SourceSheetName As String, TargetSheetName As String)
'This code copies all the data from source workbook to target workbook
Dim ProdName As String
On Error Resume Next
Application.ScreenUpdating = False
Workbooks(CurBookName).Worksheets(SourceSheetName).Activate
Workbooks(CurBookName).Worksheets(SourceSheetName).Cells.Copy
Workbooks(NewBookName).Worksheets(TargetSheetName).Activate
Workbooks(NewBookName).Worksheets(TargetSheetName).Cells(1, 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Workbooks(NewBookName).Worksheets(TargetSheetName).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Workbooks(CurBookName).Worksheets(SourceSheetName).Range("B2").CopyPicture xlScreen, xlBitmap
ProdName = Workbooks(CurBookName).Worksheets(MENU).Range("Input_Product").Value
Workbooks(CurBookName).Worksheets(SourceSheetName).Shapes(ProdName).Copy
Workbooks(NewBookName).Worksheets(TargetSheetName).Range("B1:B3").PasteSpecial
Worksheets(TargetSheetName).Shapes(ProdName).Left = Worksheets(TargetSheetName).Columns("B").Left
Worksheets(TargetSheetName).Shapes(ProdName).Top = Worksheets(TargetSheetName).Rows(1).Top
Worksheets(TargetSheetName).Shapes(ProdName).Height = Worksheets(TargetSheetName).Rows(4).Top - Worksheets(TargetSheetName).Rows(1).Top
ActiveWindow.Zoom = 70
Workbooks(NewBookName).Worksheets(TargetSheetName).Columns("A").Hidden = True
Workbooks(NewBookName).Worksheets(TargetSheetName).Range("E1").Select
Application.ScreenUpdating = True
End Sub
Public Sub SetMenuParameters(iVal As Integer)
'Input parameters and Report parameters are being set here
'Input Parameters
sServerName = Worksheets("Globals").Range("Server_Name").Value
Workbooks(CurBookName).Worksheets("Menu").Range("Input_Product").Value = ListBox1.List(iVal)
sFormula = "SUBNM(" & """" & sServerName & ":Product""" & "," & """TBA_Products""" & "," & iVal + 1 & "," & """Description""" & ")"
Workbooks(CurBookName).Worksheets("Menu").Cells(8, 4).Formula = "=" & sFormula
Workbooks(CurBookName).Worksheets("Menu").Cells(5, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(5, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(6, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(6, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(7, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(7, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(9, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(10, 8).Value
'Report Parameters
Workbooks(CurBookName).Worksheets("Menu").Range("Report_Product").Value = ListBox1.List(iVal)
sFormula = "SUBNM(" & """" & sServerName & ":Product""" & "," & """TBA_Products""" & "," & iVal + 1 & ", " & """Description""" & " )"
Workbooks(CurBookName).Worksheets("Menu").Cells(19, 4).Formula = "=" & sFormula
Workbooks(CurBookName).Worksheets("Menu").Cells(16, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(16, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(17, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(17, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(18, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 8).Value
Workbooks(CurBookName).Worksheets("Menu").Cells(18, 4).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 8).Formula
Workbooks(CurBookName).Worksheets("Menu").Cells(20, 4).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(10, 8).Value
'Variance Parameters
Var_Built = True
Workbooks(CurBookName).Worksheets("Variance").Cells(4, 8).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 11).Value
Workbooks(CurBookName).Worksheets("Variance").Cells(4, 8).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(7, 11).Formula
Workbooks(CurBookName).Worksheets("Variance").Cells(5, 8).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 11).Value
Workbooks(CurBookName).Worksheets("Variance").Cells(5, 8).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(8, 11).Formula
Workbooks(CurBookName).Worksheets("Variance").Cells(6, 8).Value = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 11).Value
Workbooks(CurBookName).Worksheets("Variance").Cells(6, 8).Formula = Workbooks(CurBookName).Worksheets("CreateSnapshot").Cells(9, 11).Formula
End Sub
Private Sub Hide_Rows_Snapshot(NewBookName As String, TargetSheetName As String)
Dim UsedRows As Integer
Dim UseRange As String
Workbooks(NewBookName).Activate
With ActiveWorkbook
UsedRows = .Worksheets(TargetSheetName).UsedRange.Rows.Count
UseRange = "A1:A" & Trim(str(UsedRows))
For Each c In .Worksheets(TargetSheetName).Range(UseRange).Cells
If c.Value = "hide" Then
.Worksheets(TargetSheetName).Rows(c.Row).Hidden = True
Else
.Worksheets(TargetSheetName).Rows(c.Row).Hidden = False
End If
Next
End With
End Sub
All Subscript Out of Range means is that your array doesn't have the member you are requesting.
dim v(5) as variant
v(0) = 'dog'
v(1) = 'cat'
v(2) = 'fish'
v(3) = 42
v(4) = 3.14
debug.print v(4) // Output is 3.14
debug.print v(5) //Throws Subscript out of range error.

Vlookup from userform input

I'm trying to build a check in my code, with the user input in a textbox, I'm trying to use a vlookup in previous records in a table to check if that unique value as already been used (initialized).
The target range "erpLots" contains text formatted cells, amd after checking using the VarType() function I know that assigning vValue = SpecEntry.TextBox3.Value vValue is a string type, the error that I'm getting "Type missmatch" is when doing the vlookup If Application.VLookup(vValue, erpLots, 1, False) = SpecEntry.TextBox3.Value Then.
I have a hunch that the error revolves around a type missmatch between the value being searched "vValue" and the target range "erpLots".
Here is the code:
Public intA As Integer
Public foundRow As Double
Sub StartButtonClick()
Dim rowCount As Long
Dim ws As Worksheet
Dim stg As String
Dim erpLots As Range
Dim vValue As Variant
Set erpLots = Worksheets("Inspection Data").Range("C2", Range("C2").End(xlDown))
Set ws = Worksheets("Inspection Data")
rowCount = ws.Range("A111111").End(xlUp).Row
'Checking the userform request info is complete
If Trim(SpecEntry.TextBox1.Value) = vbNullString Then
MsgBox "Please enter Operator ID"
ElseIf Trim(SpecEntry.TextBox2.Value) = vbNullString Then
MsgBox "Please scan or enter spec. number."
ElseIf Trim(SpecEntry.TextBox3.Value) = vbNullString Then
MsgBox "Please scan or enter ERP Lot #."
Else
SpecEntry.TextBox1.Value = UCase(SpecEntry.TextBox1.Value)
SpecEntry.TextBox2.Value = UCase(SpecEntry.TextBox2.Value)
SpecEntry.TextBox3.Value = UCase(SpecEntry.TextBox3.Value)
'checking if ERP Lot # already exist in the list
vValue = SpecEntry.TextBox3.Value
MsgBox "vValue is: " & vValue
If Application.VLookup(vValue, erpLots, 1, False) = SpecEntry.TextBox3.Value Then
foundRow = WorksheetFunction.Match(SpecEntry.TextBox3.Value, erpLots, 1)
Range("G" & foundRow).Value = Now()
Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
Range("H" & foundRow).NumberFormat = "h:mm"
Range("H" & foundRow).Value = Range("H" & foundRow).Value * 1440
Range("H" & foundRow).NumberFormat = "000.00"
intA = 2
ws.Activate
With ws.Cells(ws.Rows.Count, Selection.Column).End(xlUp)
.Select ' not required to change the focus/view
ActiveWindow.ScrollRow = foundRow - 1
End With
Exit Sub
Else
With ws.Range("A1")
intA = 1
.Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
.Offset(rowCount, 1).Value = SpecEntry.TextBox2.Value
.Offset(rowCount, 2).Value = SpecEntry.TextBox3.Value
.Offset(rowCount, 3).Value = Now()
End With
End If
End If
End Sub
My goal is that if the value exist, the information being captured is to be recorded in the same row but different columns, if the value does not exist, the information would become a new record.
If your Application.match() is working, why not dropthe vloopkup and just:
foundRow = Application.Iferror(WorksheetFunction.Match(SpecEntry.TextBox3.Value, erpLots, 1),0)
Then your If statement is:
If foundRow > 0 Then
Range("G" & foundRow).Value = Now()
Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
Range("H" & foundRow).NumberFormat = "h:mm"
Range("H" & foundRow).Value = Range("H" & foundRow).Value * 1440
Range("H" & foundRow).NumberFormat = "000.00"
intA = 2
ws.Activate
With ws.Cells(ws.Rows.Count, Selection.Column).End(xlUp)
.Select ' not required to change the focus/view
ActiveWindow.ScrollRow = foundRow - 1
End With
Exit Sub
Else
With ws.Range("A1")
intA = 1
.Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
.Offset(rowCount, 1).Value = SpecEntry.TextBox2.Value
.Offset(rowCount, 2).Value = SpecEntry.TextBox3.Value
.Offset(rowCount, 3).Value = Now()
End With
End If
I went with a countif, as a way to check if the input from the user existed in the target range, and then use that as a condition in the if statement.
Public intA As Integer
Public foundRow As Double
Sub StartButtonClick()
Dim rowCount As Long
Dim ws As Worksheet
Dim stg As String
Dim erpLots As Range
Dim vValue As Variant
Dim count As Integer
Set erpLots = Worksheets("Inspection Data").Range("C2", Range("C2").End(xlDown))
Set ws = Worksheets("Inspection Data")
foundRow = 0
count = 0
rowCount = ws.Range("A111111").End(xlUp).Row
'Checking the userform request info is complete
If Trim(SpecEntry.TextBox1.Value) = vbNullString Then
MsgBox "Please enter Operator ID"
ElseIf Trim(SpecEntry.TextBox2.Value) = vbNullString Then
MsgBox "Please scan or enter spec. number."
ElseIf Trim(SpecEntry.TextBox3.Value) = vbNullString Then
MsgBox "Please scan or enter ERP Lot #."
Else
SpecEntry.TextBox1.Value = UCase(SpecEntry.TextBox1.Value)
SpecEntry.TextBox2.Value = UCase(SpecEntry.TextBox2.Value)
SpecEntry.TextBox3.Value = UCase(SpecEntry.TextBox3.Value)
'checking if ERP Lot # already exist in the list and is coming back from labs
vValue = CStr(Trim(SpecEntry.TextBox3.Value))
count = Application.WorksheetFunction.CountIf(erpLots, vValue)
If count >= 1 Then
foundRow = Application.WorksheetFunction.Match(vValue, erpLots, 0) + 1
MsgBox "row to update is: " & foundRow
Range("G" & foundRow).Value = Now()
Range("G" & foundRow).NumberFormat = "mm/dd/yyyy hh:mm"
Range("H" & foundRow).Value = Range("G" & foundRow).Value - Range("E" & foundRow).Value
Range("H" & foundRow).NumberFormat = "d " & Chr(34) & "days" & Chr(34) & " , h:mm:ss"
intA = 2
ws.Activate
With ws.Cells(ws.Rows.count, Selection.Column).End(xlUp)
.Select ' not required to change the focus/view
ActiveWindow.ScrollRow = foundRow - 1
End With
Exit Sub
Else
With ws.Range("A1")
intA = 1
.Offset(rowCount, 0).NumberFormat = "#"
.Offset(rowCount, 0).Value = SpecEntry.TextBox1.Value
.Offset(rowCount, 1).NumberFormat = "#"
.Offset(rowCount, 1).Value = CStr(SpecEntry.TextBox2.Value)
.Offset(rowCount, 2).NumberFormat = "#"
.Offset(rowCount, 2).Value = CStr(SpecEntry.TextBox3.Value)
.Offset(rowCount, 3).Value = Now()
End With
End If
End If
End Sub

When I unload a UserForm it opens an unwanted sheet. How do I make it stop?

Whenever I unload this UserForm, it opens up "DataEntry" and sometimes makes the display unresponsive.
I have multiple UserForms and no other ones have the same problem.
The code for the UserForm:
Sub btnCalc_Click()
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("DataEntry")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
ws.Range("A" & LastRow).Value = TextBox1.Text 'Adds the TextBox1 into Col A & Last Blank Row
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & LastRow).Value = TextBox13.Text
LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row + 1
ws.Range("C" & LastRow).Value = TextBox12.Text
LastRow = ws.Range("D" & Rows.Count).End(xlUp).Row + 1
ws.Range("D" & LastRow).Value = TextBox11.Text
LastRow = ws.Range("E" & Rows.Count).End(xlUp).Row + 1
ws.Range("E" & LastRow).Value = TextBox10.Text
LastRow = ws.Range("F" & Rows.Count).End(xlUp).Row + 1
ws.Range("F" & LastRow).Value = TextBox7.Text
LastRow = ws.Range("G" & Rows.Count).End(xlUp).Row + 1
ws.Range("G" & LastRow).Value = TextBox8.Text
LastRow = ws.Range("H" & Rows.Count).End(xlUp).Row + 1
ws.Range("H" & LastRow).Value = TextBox9.Text
LastRow = ws.Range("I" & Rows.Count).End(xlUp).Row + 1
ws.Range("I" & LastRow).Value = ComboBox1.Text
ws.Range("O1").Value = TextBox1.Text
Worksheets("ResultSplash").Activate
Range("A1").Select
Application.ScreenUpdating = True
Application.Wait (Now + TimeValue("0:00:02"))
End Sub
Sub ResultSplash()
Worksheets("ResultSplash").Activate
End Sub
Private Sub Label22_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
For Each cPart In ws.Range("Industries")
With Me.ComboBox1
.AddItem cPart.Value
.List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
End With
Next cPart
Call RemoveTitleBar(Me)
End Sub
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "Apparel/Footwear"
Label13.Caption = "4,000"
Label15.Caption = "__"
Label16.Caption = "82%"
Label17.Caption = "1.5%"
Label18.Caption = "18%"
Label19.Caption = "__"
Label20.Caption = "$95"
Case "Beauty/Skincare"
Label13.Caption = "1,300"
Label15.Caption = "__"
Label16.Caption = "78%"
Label17.Caption = "-0.3%"
Label18.Caption = "22%"
Label19.Caption = "__"
Label20.Caption = "$90"
Case "Niche/Specialty"
Label13.Caption = "3,900"
Label15.Caption = "__"
Label16.Caption = "82%"
Label17.Caption = "0%"
Label18.Caption = "18%"
Label19.Caption = "__"
Label20.Caption = "$93"
Case "Food/Beverage"
Label13.Caption = "2,900"
Label15.Caption = "__"
Label16.Caption = "79%"
Label17.Caption = "1.6%"
Label18.Caption = "21%"
Label19.Caption = "__"
Label20.Caption = "$94"
Case "Computers/Electronics"
Label13.Caption = "1,500"
Label15.Caption = "__"
Label16.Caption = "84%"
Label17.Caption = "1.2%"
Label18.Caption = "16%"
Label19.Caption = "__"
Label20.Caption = "$903"
Case "Other"
Label13.Caption = "4,800"
Label15.Caption = "__"
Label16.Caption = "78%"
Label17.Caption = "1.5%"
Label18.Caption = "22%"
Label19.Caption = "__"
Label20.Caption = "$94"
End Select
End Sub
Private Sub TextBox13_AfterUpdate()
If IsNumeric(TextBox13) Then
TextBox13 = Format(TextBox13, "#,##0")
Else
ErrorForm.Show
TextBox13 = ""
End If
End Sub
Private Sub TextBox12_AfterUpdate()
If IsNumeric(TextBox12) Then
TextBox12 = Format(TextBox12, "#,##0")
Else
ErrorForm.Show
TextBox12 = ""
End If
End Sub
Private Sub TextBox11_AfterUpdate()
If IsNumeric(TextBox11) Then
TextBox11.Value = TextBox11.Value * 0.01
TextBox11 = Format(TextBox11, "#%")
Else
ErrorForm.Show
TextBox11 = ""
End If
End Sub
Private Sub TextBox10_AfterUpdate()
If IsNumeric(TextBox10) Then
TextBox10.Value = TextBox10.Value * 0.01
TextBox10 = Format(TextBox10, "#%")
Else
ErrorForm.Show
TextBox10 = ""
End If
End Sub
Private Sub TextBox7_AfterUpdate()
If IsNumeric(TextBox7) Then
TextBox7.Value = TextBox7.Value * 0.01
TextBox7 = Format(TextBox7, "#%")
Else
ErrorForm.Show
TextBox7 = ""
End If
End Sub
Private Sub TextBox8_AfterUpdate()
If IsNumeric(TextBox8) Then
TextBox8.Value = TextBox8.Value * 0.01
TextBox8 = Format(TextBox8, "#%")
Else
ErrorForm.Show
TextBox8 = ""
End If
End Sub
Private Sub TextBox9_AfterUpdate()
If IsNumeric(TextBox9) Then
TextBox9 = Format(TextBox9, "$#")
Else
ErrorForm.Show
TextBox9 = ""
End If
End Sub
I'm trying to enter information to the "DataEntry" sheet, and the display the "ResultSplash" page, but whenever I close the UserForm, it displays the "DataEntry" sheet.
Thanks in advance for your help!
You can add following line after unload line may be:
ActiveWorokbook.Sheets(DataEntry).Visible = False
If this is what you are looking for

VBA Spinbutton increment value textboxes

I would like to change the data values of textboxes by using a spinbutton
The data are written in an Excel table ..... could you explain to me which
syntax I should use or perhaps could you show me an example?
Private Sub SpinButton1_Change()
'Range("G15").Value = SpinButton1.Value
Dim I As Integer
For I = 2 To 10 Step 1
TextBox2.Value = Ws.Range("A"& I)
Next
Using the following code i gives a good result but but why do i need an msgbox ? Without msgbox it doesn't function ... strange ...
Private Sub SpinButton1_Change()
Dim I As Integer
For I = 2 To 10 Step 1
MsgBox ("La valeur de la Textbox" & I & " est de " & TextBox2)
ComboBox1.Value = Ws.Range("A" & I)
ActiveCell = Me.ComboBox1.Value
TextBox2.Value = Ws.Range("B" & I)
TextBox3.Value = Ws.Range("C" & I)
TextBox4.Value = Ws.Range("D" & I)
TextBox5.Value = Ws.Range("E" & I)
TextBox6.Value = Ws.Range("F" & I)
TextBox7.Value = Ws.Range("G" & I)
TextBox8.Value = Ws.Range("H" & I)
TextBox9.Value = Ws.Range("I" & I)
'Range("G15").Value = SpinButton1.Value
Next

Resources