I have a program that takes user input and places it in a sheet, based on job type. For the purposees of this we're only concerned with Sheet("Active Jobs").
I have a function which takes information from a UserForm, and places it into Cells, based on jobID. A simple count keeps track of the JobID, and places the job into the sheet, based on its ID, as such:
Public btn As Button
Public t As Range
Function GetActiveJob()
Dim i1 As Integer
i1 = Worksheets("Sheet1").Range("C4").Value '//Keeps count of jobs, gives the JobID
With Worksheets("Active Jobs")
.Range("A" & (5 * i1)) = UserForm1.TextBox1
ActiveSheet.Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).Merge
.Range("B" & (5 * i1)) = UserForm1.TextBox2 '//Handles a decent bit of text
.Range("B" & (5 * i1) & ":" & "E" & (3 + (5 * i1))).VerticalAlignment = xlTop
Set t = ActiveSheet.Range("G" & (5 * i1))
Set btn = Worksheets("Active Jobs").Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.Name = "RemovetoArchive" & i1
.Caption = "Archive"
.OnAction = "RemovetoArchive"
End With
End With
UserForm1.TextBox1.Value = ""
UserForm1.TextBox2.Value = ""
i1 = i1 + 1
Worksheets("Sheet1").Range("C4").Value = i1
MsgBox ("I1 = " & i1) 'For testing purposes
End Function
(The job ID needn't be unique)
As you can see, the userform creates a button with a name (Name of the function) followed by the jobID.
When a job is complete, & the archive button is pressed, I want to move it into a new sheet, called Archive.
To do this I take the string and manipulate it to get the ID Number, then convert the string to integer:
Function RemovetoArchive()
Dim ButtonID As Integer
Dim ButtonString As String
ButtonString = Mid(btn.Name, 16, 2) '<-- Error here, (see below)
ButtonID = CInt(ButtonString)
Worksheets("Active Jobs").Range("A" & (5 * ButtonID) & ":R" & (3 + (5 * ButtonID))).Select
MsgBox ("ButtonID is " & ButtonID) 'Testing
The ID Number is used as a variable to determine which range needs to be selected, and moved to archive.
Running this however, I get
Object variable or With Block Variable not set
I'm having issues with setting the btn.Name object. i.e.
I enter a job with JobID '5'
The information is created along with a button.
I press Button 5, a.k.a "RemovetoArchive5"
My code should take just the numeric character from the string "RemovetoArchive5" and convert it to integer, to be used as ButtonID.
As it is now, btn.Name, doesn't have the value
btn.Name = "RemovetoArchive5"
Will this have to be done in the GetActiveJobs Function? How can I pass the btn.Name Object, after Clicking the button it refers to?
Any help would be greatly appreciated.
btnseems to be a module-level variable, aka "public field" or "global variable", which is the opposite of "passing parameters"Nameproperty that has no value, it's thebtnreference itself;btnisNothing. Which type of module isbtndeclared in? A standard/procedural module? A worksheet/class module? A userform's code-behind? See Variables and Scopes in the VBA topics of Documentation.SO.Option Explicitat the top of every module - I suspectbtnis undeclared/inaccessible from the module you're trying to access it from.