+ Reply to Thread
Results 1 to 10 of 10

How to use a global array correctly.

Hybrid View

Brad4444 How to use a global array... 04-08-2011, 11:43 AM
StephenR Re: How to use a global array... 04-08-2011, 11:47 AM
Brad4444 Re: How to use a global array... 04-08-2011, 11:51 AM
StephenR Re: How to use a global array... 04-08-2011, 12:03 PM
Brad4444 Re: How to use a global array... 04-08-2011, 12:04 PM
MarvinP Re: How to use a global array... 04-08-2011, 12:12 PM
shg Re: How to use a global array... 04-08-2011, 12:45 PM
Brad4444 Re: How to use a global array... 04-08-2011, 01:40 PM
shg Re: How to use a global array... 04-08-2011, 02:26 PM
Brad4444 Re: How to use a global array... 04-08-2011, 03:01 PM
  1. #1
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    How to use a global array correctly.

    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.
    Last edited by Brad4444; 04-08-2011 at 03:01 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: How to use a global array correctly.

    Not sure what you are doing. You assign strings to Description but then you go through and empty it. What are you trying to achieve?

  3. #3
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How to use a global array correctly.

    I am attempting to create a user form where the person enters in each value (on the user form) and then read that data into the last row of an excel sheet.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: How to use a global array correctly.

    OK, I see now what you were trying to do. Can you post a workbook?

  5. #5
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How to use a global array correctly.

    Sorry, I can't.

    EDIT:

    In the lines where the problem occurs

    For intCounter = 0 To 3
            ActiveCell.Offset(0, intCounter) = Descriptions.Value
    Next
    If I say Descriptions.Value would this call the text box value I am looking for? I guess I am asking "Am I calling the value syntactically correct?"
    Last edited by Brad4444; 04-08-2011 at 12:07 PM.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: How to use a global array correctly.

    What happens if you use
    ActiveCell.Offset(0, intCounter) = Descriptions.Text
    ?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to use a global array correctly.

    Is this supposed to clear controls?

    Descriptions = Array("txtDescription", "txtDescription2", "txtDescription3", "txtStatus")
    'Initialize the array elements to blank.
        For intCounter = 1 To 4
            Descriptions.Value = ""
        Next
    If so, it should look something like this:
        Dim vCtl        As Variant
    
        For Each vCtl In Array(txtDescription, txtDescription2, txtDescription3, txtStatus)
            vCtl.Value = vbNullString
        Next vCtl
    or maybe this:
        Dim vsCtl        As Variant
    
        For Each vsCtl In Array("txtDescription", "txtDescription2", "txtDescription3", "txtStatus")
            UserForm1.Controls(vsCtl).Value = vbNullString
        Next vsCtl
    In the first case, you are iterating through an array of controls. In the second, through an array of control names.
    Last edited by shg; 04-08-2011 at 01:05 PM.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How to use a global array correctly.

    Sorry Shg, your code didn't help but it got me thinking.

        For intCounter = 0 To 11
            ActiveCell.Offset(0, intCounter).Value = Descriptions(intCounter + 1).Value
        Next
    What I want to do is assign a cell on a worksheet a value. I am storing strings in my array but they are just placeholders to call upon specific spots from my userform.

    So what I need to know is where does the first element of an array in VBA begin? More importantly though... the code above gives me the error "type mismatch". Why? Once again thanks for all your guys help.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to use a global array correctly.

    (Many) Controls have a Value property, a Range has a Value property, Variants that represent objects have a Value property if the Object it represents does.

    Variants used as ordinary variables (strings, numbers, ..., or arrays of same) do not.

    Only Objects have Properties and Methods.

    That was the point of my two examples.
    Last edited by shg; 04-08-2011 at 02:29 PM.

  10. #10
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How to use a global array correctly.

    Point taken now. The idea is working. Weird bug though that it offsets to far.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1