My code is as follows and then I will explain.
'Option Explicit means all variables must be defined as something.
'Two variables are public as they are both used in the subs below.
Option Explicit
Public Descriptions As Variant
Public intCounter As Integer
'This user form lets a user enter a new part for the sheet. The sub
'defines the data, initializes the data, and finally lets the user begin entering data.
Private Sub frmUserForm_Initialize()
'Defining an array of information to be filled in by the user.
Descriptions = Array("txtDescription", "txtDescription2", "txtDescription3", "txtStatus")
'Initialize the array elements to blank.
For intCounter = 1 To 4
Descriptions.Value = ""
Next
'Initialize all the checkboxes to False.
For intCounter = 1 To 30
Me.Controls("chkCheckBox" & intCounter) = True
Next
'Setting focus to a specific spot so entering data is easy for the user.
txtDescription.SetFocus
End Sub
'CommanButton1_Click() fills in the data entered by the user in the Sub frmUserForm_Initialize().
'CommandButton1 finds the last row and puts the data in the corresponding columns.
'Note: Changing the column order means frmUserForm_Initialize will have to be updated and vice-versa.
Private Sub CommandButton1_Click()
Dim intCounter As Integer
'Make sure the correct worksheet and cell is up.
ActiveWorkbook.Sheets("Data").Activate
Range("A7").Select
'Locate the last row.
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
'Loop places user input in the corect columns. Offset from column A to the value of intCounter.
For intCounter = 0 To 3
ActiveCell.Offset(0, intCounter) = Descriptions.Value
Next
'Loop places user input for the check boxes in the correct columns. Also assigns that spot a value.
For intCounter = 12 To 41
If chkCheckBox1 = True Then
ActiveCell.Offset(0, intCounter).Value = "Yes"
Else
ActiveCell.Offset(0, intCounter).Value = "No"
End If
Next
'Go back to Cell A7 incase the user wants to enter a new description or search for a description.
Range("A7").Select
End Sub
So basically I am trying to save lines of code by using For-Next loops and then working on this array. My problem appears in the second sub
For intCounter = 0 To 3
ActiveCell.Offset(0, intCounter) = Descriptions.Value
Next
It says Object required which I am assuming is because I have declared my array wrong. Can somebody confirm or deny this for me? You make something global by putting it outside the subs like the following...
Option Explicit
Public Descriptions As Variant
Public intCounter As Integer
Private Sub frmUserForm_Initialize()
Anyways any help is much appreciated. Thanks for your guys help.
Bookmarks