1004 Error on simple Sub - vba

I'm having a 1004 error in the code below. Its such as simple routine, I can't figure out what's wrong. This is only one sub of many in Module 1. Note that UserForm2's code works fine when I run it from the forms Sub's. But when I call it from here, I get the 1004 error. I don't really understand what causes that error. Help appreciated. Note that ChosenString and Report is a Public variable. You have several 1004 error entries, but I can't find one that fits this problem. Error is on UserForm2.Show.
Sub ChooseReport()
' Display a selection box of reports and run the report
'
ChoseCancel = 0
Sheets("Codes").Activate
UserForm2.Show ' Displays selection box of reports
'
' If the Cancel button was selected, exit the sub
If ChoseCancel = 1 Then
Sheets("Reports").Activate
Exit Sub
End If
'
' Trim returned value of the comma
StringLength = Len(ChosenString)
Report = Left(ChosenString, StringLength - 2)
'
End Sub
This is my UserForm2 Code. It works fine.
Sub UserForm_Initialize()
' Fill the list box with appropriate values
'
UserForm1.ListBox1.ListStyle = fmListStylePlain
Sheets("Codes").Activate
Range("O4").Select
'
' Fill List box with appropriate cells entries for Reports
With ListBox1
Do While ActiveCell.Value <> Empty
.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
End With
End Sub

Found it. I had copied the code from UserForm1, and hadn't changed a reference to UserForm1 to UserForm2. My error went away.

Related

VBA_Processing a value as 29160012040000TZ

I created a couple of user forms which operate a data in separate report workbook. My script can successfully proceed a value in digit type. Unfortunately the circumstances have changed and now it has to work with a Serial Numbers as: 29160012040000TZ. With that new value script after starting the Sub, open a report, but it never enter into a 'with' statement. It doesn't look for a value or doing something else. Just open a report workbook and freeze.
Below you can see the code lines where issue is present and a little description:
Single_PHA is a text window in User Form where user can enter a a value, proceeding value is 29160012040000TZ
Private Sub Wydaj_button_Click()
Workbooks.Open Filename:="N:\ENGINEERING\1. ENGINEERS\Mateusz Skorupka\PHA_Cleaning_report_path\PHA_CLEANING_REPORT.xlsm", ReadOnly:=False
Dim REPORT As Workbook
Set REPORT = Application.Workbooks("PHA_CLEANING_REPORT.xlsm")
Set TABLE = REPORT.Worksheets("Main_table")
...
With TABLE.Range("A1")
If Single_PHA = True Then
If Not IsError(Application.Match(Single_PHA.Value, .Range("A:A"), 0)) Then
Single_PHA_row = TABLE.Range("A:A").Find(What:=Single_PHA.Value, LookIn:=xlValues).Row
.Offset(Single_PHA_row - 1, 4).Value = Date
REPORT.Close SaveChanges:=True
Single_PHA.Value = ""
Exit Sub
Else
MsgBox "Numer seryjny głowicy nie istnieje w bazie"
REPORT.Close SaveChanges:=False
Exit Sub
End If
End If
End With
In VBA I don't know how to open something like debugger or make the print instruction which would show me how the variables look on specific steps.
I am not sure if VBA read the value as 29160012040000TZ as string. I tried to declare at the beginning a variable as Single_PHA_STR as String and the proceed it as just text, but no wins there:
Dim Single_PHA_STR As String
...
With TABLE.Range("A1")
If Single_PHA = True Then
Single_PHA_STR = Str(Single_PHA.Value)
If Not IsError(Application.Match(Single_PHA_STR, .Range("A:A"), 0)) Then
Single_PHA_row = TABLE.Range("A:A").Find(What:=Single_PHA_STR, LookIn:=xlValues).Row
.Offset(Single_PHA_row - 1, 4).Value = Date
REPORT.Close SaveChanges:=True
Single_PHA.Value = ""
Exit Sub
Else
MsgBox "Numer seryjny głowicy nie istnieje w bazie"
REPORT.Close SaveChanges:=False
Exit Sub
End If
End If
End With
I noticed that if in VBA IDE I write a bold value 29160012040000TZ, I get an error
Expected line number or label or statement or end of statement
and the value is highlighted in red.
Could someone help me in that field and explain the nature of issues:
To reproduce a situation you can create a simply user form with one TextBox and one CommandButton. In the same worksheet as user form in a column A put a values: 29160012040000 and 29160012042027IR
Then make a sub which execute after double click on command button with code:
Private Sub CommandButton1_Click()
With Worksheets("Sheet1").Range("A1")
If Text_box1 = True Then
If Not IsError(Application.Match(Text_box1.Value, .Range("A:A"), 0)) Then
Text_box1_row = Worksheets("Sheet1").Range("A:A").Find(What:=Text_box1.Value, LookIn:=xlValues).Row
.Offset(Text_box1_row - 1, 4).Value = Date
Text_box1.Value = ""
Exit Sub
Else
MsgBox "PHA SN not exist in a database"
Exit Sub
End If
End If
End With
End Sub
Then try to input in a UserForm's TextBox a value = 29160012040000 and you will see that script successfully filled a forth column in row with current date. Then try to input a value 29160012042027IR and you will see that nothing happened. Script don't proceed that value at all.
So that is my issue and question indeed. How to process a value with letters at the end like: 29160012042027IR : )
I also tried to focus a script statement on one specific cell in which is a text value "29160012042027IR" that which I input into a UserForm TextBox. Looking with a debugger both of variables in if statement have the same text value, but still script miss that statement and go to else instructions : (
I mean abut: If Range("A3").Text = Text_box1.Text Then
When I change a statement for "If Range("A3").Value = Text_box1.Value Then" the same thing happen.
Private Sub CommandButton1_Click()
With Worksheets("Sheet1").Range("A:A")
If Text_box1 = True Then
If Range("A3").Text = Text_box1.Text Then
Text_box1_row = Worksheets("Arkusz1").Range("A:A").Find(What:=Text_box1.Value, LookIn:=xlWhole).Row
.Offset(Text_box1_row - 1, 4).Value = Date
Text_box1.Value = ""
Exit Sub
Else
MsgBox "PHA SN not exist in a database"
Exit Sub
End If
Else
MsgBox "Other loop"
End If
End With
End Sub
IMPORTANT NOTICE:
I found the main issue. I made wrong if condition, it should be:
If Single_PHA <> "" Then previously I have got: If Single_PHA = True Then, and there the results is a value not the boolean type.
Everything works. Thank everyone very much for help.
Topic is ready to be closed.
PS: thank you Tom for suggestion and tip with debugger: )

Run time error 424 Object Required working with UserForm

I'm trying to link a user form I built in VBA editor for MS Excel 2010 to the data in an excel worksheet, but I'm getting a
run-time error 424: Object required.
I referred to the active worksheet explicitly in my code to try and remedy this, but it still comes up with the same error. My code is:
Private Sub GetData()
Dim r As Long
r = ActiveSheet.Range("B2").Value
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
Else
ClearData
MsgBox "Invalid row number"
Exit Sub
End If
If r > 1 And r <= LastRow Then
cboFilterResultId.Text = FormatNumber(Cells(r, 1), 0)
txtFolderPaths.Text = Cells(r, 2)
txtFileName.Text = Cells(r, 3)
txtDeletedDate.Text = Cells(r, 4)
txtReason.Text = Cells(r, 5)
txtcboAdd.Text = Cells(r, 6)
txtcboView.Text = Cells(r, 7)
txtcboChange.Text = Cells(r, 8)
DisableSave
ElseIf r = 1 Then
ClearData
Else
ClearData
MsgBox "Invalid row number"
End If
End Sub
Where RowNumber is a textbox where the user can enter the row number for the data they want.
Please help!
I rarely use ActiveSheet just in case that isn't the sheet I'm after. Generally better to be explicit which sheet you're referring to:
r=ThisWorkbook.WorkSheets("Sheet1").Range("B2")
Right, pulling data from a worksheet to a userform... as you haven't said which line your error occurs on and you haven't given us the code for ClearData or DisableSave I'll start from scratch.
Example Form Design
I create a blank userform and add three text boxes and a spin button to it:
txtRowNumber holds the row number that the data is pulled from.
TextBox1 and TextBox2 will hold my sample values.
In the Tag property of TextBox1 I enter 1 and in the Tag property of TextBox2 I enter 2. These are the column numbers that the data will be pulled from.
In reality I usually add extra stuff, for example, 8;CPER;REQD. I'd then use some code to pull it apart so it pastes in column 8, must have a percentage and is a required entry.
spnButton is used to quickly move up or down a row.
We'll need two procedures to populate the form from the given row number and to clear all controls on the form (ready for the next row to be brought in).
Any textbox or combobox that has something in it's Tag property is cleared:
Private Sub ClearForm()
Dim frmCtrl As Control
For Each frmCtrl In Me.Controls
If frmCtrl.Tag <> "" Then
Select Case TypeName(frmCtrl)
Case "TextBox", "ComboBox"
frmCtrl.Value = Null
Case Else
'Do nothing.
End Select
End If
Next frmCtrl
End Sub
Any control that has a Tag value (it's assumed the value is correct) is populated from the specified RowNumber and column (from the Tag value). The value is always taken from the sheet called MyDataSheet in the workbook containing the VBA code (ThisWorkbook) no matter which is currently active:
Private Sub PopulateForm(RowNumber As Long)
Dim frmCtrl As Control
For Each frmCtrl In Me.Controls
With frmCtrl
If .Tag <> "" Then
.Value = ThisWorkbook.Worksheets("MyDataSheet").Cells(RowNumber, CLng(.Tag))
End If
End With
Next frmCtrl
End Sub
Whenever txtRowNumber changes the form should update with values from the indicated row. To do this we'll need to clear the form of current data and then repopulate it:
Private Sub txtRowNumber_Change()
ClearForm
PopulateForm CLng(Me.txtRowNumber)
End Sub
The spin button should increase/decrease the value in .txtRowNumber. I've added checks that it doesn't go below 1. You should also add checks that it doesn't go higher than the last populated row.
Private Sub spnButton_SpinDown()
With Me
.txtRowNumber = CLng(.txtRowNumber) + 1
End With
End Sub
Private Sub spnButton_SpinUp()
With Me
If CLng(.txtRowNumber) > 1 Then
.txtRowNumber = CLng(.txtRowNumber) - 1
End If
End With
End Sub
Finally, the form should be populated when it is first opened:
Private Sub UserForm_Initialize()
With Me
.txtRowNumber = 2
.spnButton = .txtRowNumber
PopulateForm .txtRowNumber
End With
End Sub

Excel Userform VBA VLOOKUP

I am creating a userform where based on a drop down list of items called "ContractsList" I would like a Vlookup formula to return a text data in "TextBox 1".
I get an error message saying "Run-time error'1004': Unable to get the Vlookup property of the worksheetfunction class
Not sure what I am doing wrong, here is my code if anyone can spot the error.
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Sheet2.Range("A:A"),Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.TextBox1, ("B5:B72"), 2, 0)
End With
End Sub
Finally got it to work as below:
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Sheet2.Range("A5:E72"), 2, 0)
End With
End Sub
I just needed to add "Sheet2.Range("A5:E75")
Thank you all for your help.

Excel VBA UserForm Error

I'm very new to Excel VBA and can't seem to figure out why I keep getting error 80010108 "Method 'Value' of object 'Range' failed." If anyone can help me out it would be very much appreciated!!
I created a UserForm ("MainView") with a command button ("Add New"), which brings up another UserForm ("AddNewWorkOrder") that contains another command button ("Enter"). I want this Enter command button to enter all data typed in this userform's textboxes into the corresponding columns in the active worksheet.
**I need to add rows to a table in the worksheet
Now when I run MainView and then click "Add New" to bring up AddNewWorkOrder, I'm then unable to enter the data because clicking "Enter" will throw up the error 80010108 and crash Excel (prompted to restart Excel).
HOWEVER if I run the AddNewWorkOrder UserForm directly, without going through MainView, it will enter the data exactly as needed.
This is the code I have for MainView's "Add New":
Private Sub AddNew_Click()
Me.Hide
AddNewWorkOrder.Show
End Sub
This is the code I have for "Enter" (including just one textbox for simplicity):
Private Sub EnterCommandButton_Click()
Range("A" & Rows.Count).End(xlUp).Select
HoldVal = ActiveCell.Value + 1
MsgBox HoldVal
ActiveCell.Offset(1, 0).Value = HoldVal
ActiveCell.Offset(1, 1).Select
ActiveCell.Value = Me.ItemSizeTextBox
Me.Hide
MainView.Show
End Sub
Edit: the line "ActiveCell.Offset(1, 0).Value = HoldVal" is where I'm getting an error

Continuing a function

I'm trying to write a VBA macro where it analyzes multiple sheets of data in the same excel file. I currently have it set so that when it's analyzing all the various sheets, when it finds an error, it stops the program completely. I have figured out how to get it to go to the next sheet but cannot get the program to continue running. Here is what I have so far:
`If Counter > 480 Then
Worksheets(ActiveSheet.Index + 1).Select
End
End If`
I know the End function ends running the program but when I take it out it does not work, how can I get the program to continue running as it was before it reaches the sheet with the error?
Put the loop that you want to end in a separate function or sub, then when the error is found, use
Exit Function
or
Exit Sub
if it's a sub.
Simple example using your code:
Sub Main
OtherSub
'Continue here
End Sub
Sub OtherSub
If Counter > 480 Then
Worksheets(ActiveSheet.Index + 1).Select
Exit Sub
End If
End Sub