Vlookup from userform input - excel

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

Related

Excel Userform coding revise

I would need help on how to revise the code below. I was able to create the template to enter all the informations needed in the userform when the header is in row 1 on the template. But when I need to relocate the header to row 29. It doesn't work as expected even though I did revised the coded to match with row 29. Please help.
This is a good picture of the header in row1 with the code below. It is working fine.
here is the file https://1drv.ms/x/s!AixhKuqjnB1cgW8qhYoRMmt0oN0o?e=W52afT
You will find "Original" Tab. with the original VBA coding working with header in row 1. The "CID" tab will be the one I need to revise the code to work with the header moved to row 29.
This is the original code that work with header in row 1
Sub Refresh_Data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Original")
Dim last_row As Long
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 12
.ColumnWidths = "30,100,100,70,100,100,50,100,50,50,120,200"
If last_row = 1 Then
.RowSource = "Original!A2:L2"
Else
.RowSource = "Original!A2:L" & last_row
End If
End With
End Sub
Private Sub Add_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Original")
Dim last_row As Long
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
'Validations---------------------------------------------------------------------------------------
If Me.TextBox1.Value = "" Then
MsgBox "Please Fill Signal Name. If it is not required, fill -", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox2.Value = "" Then
MsgBox "Please Fill (From) Connector REF DES", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox3.Value = "" Then
MsgBox "Please Fill (From) Connector Pin Location", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox4.Value = "" Then
MsgBox "Please Fill Contact P/N or Supplied with Connector", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox5.Value = "" Then
MsgBox "Please Fill Wire Gauge", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox6.Value = "" Then
MsgBox "Please Fill Wire/Cable P/N", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox7.Value = "" Then
MsgBox "Please Fill (To) Connector REF DES", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox8.Value = "" Then
MsgBox "Please Fill (To) Pin Location", vbCritical
Exit Sub
End If
'------------------
If Me.TextBox9.Value = "" Then
MsgBox "Please Fill Contact P/N or Supplied with Connector", vbCritical
Exit Sub
End If
'------------------
If Me.ComboBox10.Value = "" Then
MsgBox "Use Drop Down Arrow to Select Wire Color", vbCritical
Exit Sub
End If
'--------------------------------------------------------------------------------------------------
sh.Range("A" & last_row + 1).Value = "=Row()-1"
sh.Range("B" & last_row + 1).Value = Me.TextBox1.Value
sh.Range("C" & last_row + 1).Value = Me.TextBox2.Value
sh.Range("D" & last_row + 1).Value = Me.TextBox3.Value
sh.Range("E" & last_row + 1).Value = Me.TextBox4.Value
sh.Range("F" & last_row + 1).Value = Me.TextBox5.Value
sh.Range("G" & last_row + 1).Value = Me.TextBox6.Value
sh.Range("H" & last_row + 1).Value = Me.TextBox7.Value
sh.Range("I" & last_row + 1).Value = Me.TextBox8.Value
sh.Range("J" & last_row + 1).Value = Me.TextBox9.Value
sh.Range("K" & last_row + 1).Value = Me.ComboBox10.Value
sh.Range("L" & last_row + 1).Value = Me.TextBox11.Value
'------------------
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
Me.TextBox8.Value = ""
Me.TextBox9.Value = ""
Me.ComboBox10.Value = ""
Me.TextBox11.Value = ""
'------------------
Call Refresh_Data
End Sub""
And this is the picture of the header moved to row 29.
Use a constant for the header row and then it's easy to change in the future.
Option Explicit
Const HEADER = 29
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("CID")
Dim last_row As Long
last_row = sh.Cells(Rows.Count, "A").End(xlUp).Row
If last_row < HEADER Then
last_row = HEADER
End If
Dim arMsg(10) As String, n As Integer, msg As String
arMsg(1) = "Signal Name. If it is not required, fill -"
arMsg(2) = "(From) Connector REF DES"
arMsg(3) = "(From) Connector Pin Location"
arMsg(4) = "Contact P/N or Supplied with Connector"
arMsg(5) = "Wire Gauge"
arMsg(6) = "Wire/Cable P/N"
arMsg(7) = "(To) Connector REF DES"
arMsg(8) = "(To) Pin Location"
arMsg(9) = "Contact P/N or Supplied with Connector"
arMsg(10) = "Use Drop Down Arrow to Select Wire Color"
For n = 1 To 9
If Me.Controls("TextBox" & n).Value = "" Then
msg = msg & vbLf & n & ") " & arMsg(n)
End If
Next
If Me.Controls("ComboBox10").Value = "" Then
msg = msg & vbLf & arMsg(10)
End If
If Len(msg) > 0 Then
MsgBox "Please Fill " & msg, vbCritical
Exit Sub
End If
Dim c As Control
With sh.Range("A" & last_row + 1)
.Offset(0, 0).Value = "=Row()-" & HEADER
For n = 1 To 11
If n = 10 Then
Set c = Me.Controls("ComboBox" & n)
Else
Set c = Me.Controls("TextBox" & n)
End If
.Offset(0, n).Value = c.Value
c.Value = ""
Next
End With
Call Refresh_Data(sh)
End Sub
Sub Refresh_Data(sh As Worksheet)
Dim last_row As Long
last_row = sh.Cells(Rows.Count, "A").End(xlUp).Row
With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 12
.ColumnWidths = "30,100,100,70,100,100,50,100,50,50,120,200"
If last_row <= HEADER Then
last_row = HEADER + 1
End If
.RowSource = sh.Name & "!A" & HEADER + 1 & ":L" & last_row
End With
End Sub

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.

Looping through two cell ranges in two worksheets

The following code runs but, not getting the results. The information is there in the correct range.
Dim ID As Range
Dim SN As Range
Dim i As Integer
Set ID = Sheet6.Range("B2:B8")
Set SN = Sheet2.Range("C7:C184")
For i = 2 To ID.Cells.count
If ID.Cells(i) = SN.Cells(i) Then
MsgBox "do something"
ID.Cells.Offset(0, 2).Value = SN.Cells.Offset(0, -2).Value
Else
MsgBox "sorry"
End If
Next
i found another code and modified it to my work sheet. This one works great.
Dim i As Long
Dim j As Long
For i = 2 To 40
If Sheet6.Range("C" & i).Value = "" Then
Exit For
End If
For j = 7 To 1000
If Sheet2.Range("c" & j).Value = "" Then
Exit For
End If
If Sheet6.Range("C" & i).Text = Sheet2.Range("c" & j).Text Then
Sheet6.Range("C" & i).Offset(0, 1).Value = Sheet2.Range("c" & j).Offset(0, -2).Value
Sheet6.Range("C" & i).Offset(0, 2).Value = Sheet2.Range("c" & j).Offset(0, 2).Value
Exit For
End If
Next j
Next i

Trying to call a list of first and last names but returns a mismatch error

I created a form that recalls information from a sheet i.e. first and last names. In the name combobox, when typing the name out on the form, if you type a name that isn't located on the list, a mismatch occurs. How do I remedy this? If I remove the 0 value the function no longer matches correctly.
* Call previous fields *
Private Sub ComboBox4_Change()
If Me.ComboBox4.Text <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim i As String
i = Application.Match(Me.ComboBox4.Value, sh.Range("A:A"), 0)
Me.TextBox2.Value = sh.Range("B" & i).Value
If sh.Range("H" & i).Value = "Introduced" Then Me.OptionButton1.Value = True
If sh.Range("H" & i).Value = "Not Introduced" Then Me.OptionButton2.Value = True
Me.ComboBox1.Value = sh.Range("C" & i).Value
Me.ComboBox2.Value = sh.Range("D" & i).Value
Me.ComboBox3.Value = sh.Range("M" & i).Value
Me.TextBox11.Value = sh.Range("J" & i).Value
Me.TextBox10.Value = sh.Range("K" & i).Value
Me.TextBox9.Value = sh.Range("L" & i).Value
Me.TextBox12.Value = sh.Range("I" & i).Value
Me.TextBox5.Value = sh.Range("G" & i).Value
Me.TextBox7.Value = sh.Range("N" & i).Value
End If
End Sub
Sometimes using functions native to VBA makes things a little easier. You can use Range.Find instead of the worksheet function Match for your code.
Dim sh As Worksheet: Set sh = ThisWorkbook.Sheets("Database")
Dim xName As Range
Set xName = sh.Range("A:A").Find Me.ComboBox4.Value
If xName Is Nothing Then
'End sub if not found
MsgBox "Name Not Found - Existing Sub"
Else
'To access the row use xName.Row
Me.TextBox2.Value = sh.Range("B" & xName.Row).Value
'.... rest of code
End If

Run Time Error 13 Type Mismatch on combo box with alphanumeric data

I'm trying to setup an update data entry form. How can I transition from integer combo box with only numerical values to a data type using string or long combo box with alphanumeric values?
Private Sub UserForm_Activate()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Employee Details")
Dim i As Integer
Me.UsernameComboBox.Clear
Me.UsernameComboBox.AddItem ""
For i = 11 To sh.Range("B" & Application.Rows.Count).End(xlUp).Row
Me.UsernameComboBox.AddItem sh.Range("B" & i).Value
Next i
End Sub
Private Sub UsernameComboBox_Change()
If Me.UsernameComboBox <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Employee Details")
Dim i As Integer
i = Application.Match(VBA.CLng(Me.UsernameComboBox.Value), sh.Range("B:B"), 0)
Me.NameTextBox = sh.Range("A" & i).Value
Me.EmailTextBox = sh.Range("C" & i).Value
Me.BirthdateTextBox = sh.Range("D" & i).Value
Me.NationalIDTextBox = sh.Range("E" & i).Value
Me.EmpIDTextBox = sh.Range("R" & i).Value
Me.DeptTextBox = sh.Range("V" & i).Value
If sh.Range("Y" & i).Value = "Male" Then Me.MaleOptionButton.Value = True
If sh.Range("Y" & i).Value = "Female" Then Me.FemaleOptionButton.Value = True
Me.StatusComboBox = sh.Range("X" & i).Value
Me.CitizenshipComboBox = sh.Range("Z" & i).Value
Me.EthnicityComboBox = sh.Range("F" & i).Value
End If
End Sub
Welcome to SO. Assuming you do want not to change the numeric values in column B to Text or use another column with formula =Text(B11,"#"), it may be easy to use workaround Find. May try
Dim i As Integer
Dim FndRng As Range, c As Range
'i = Application.Match(Me.UsernameComboBox.Value, Sh.Range("B:B"), 0)
Set FndRng = Sh.Range("B11:B" & Sh.Range("B" & Application.Rows.Count).End(xlUp).Row)
Set c = FndRng.Find(Me.UsernameComboBox.Value, , LookIn:=xlValues)
If Not c Is Nothing Then
i = c.Row
Else
MsgBox "Not found"
Exit Sub
End If

Resources