Consulting

Results 1 to 7 of 7

Thread: Array Type Mismatch

  1. #1

    Array Type Mismatch

    I have tried to resolve the issue with no luck. Any ideas why I would get a type mismatch. Here are the facts.

    UserForm TextBox Controls and Values

    txtID = 225
    txtDate = 09/09/2001
    txtName = John Doe

    [vba]
    Dim MyData as Range
    Dim VaInfo as Variant


    Public Sub ChangeInfo_Click()

    Call MyChanges

    With Range("MyInfo")
    Set MyData = .Rows(2)
    End With

    End Sub


    Public Sub MyChanges()

    VaInfo(1, 1) = txtID.Value
    VaInfo(1, 2) = txtDate.Value
    VaInfo(1, 3) = txtName.Value

    MyData.Value = VaInfo
    End Sub
    [/vba]
    When step through the code I notice the data is correct after the equal sign under the Public Sub MyChanges(). But I get the following error when I point on the left side of the equal sign.

    VaInfo(1,1) = <Type Mismatch>


    Does anyone have any ideas?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Brorick,

    As to the jam-up, I believe it is because you did not dimension the array. Something like: Dim VaInfo(1 to 1, 1 to 3)

    MyData.Value = VaInfo will fail here, as it is not yet Set.

    I didn't see the need (as of the supplied info leastwise) for a two-dimensional array, so this example just uses a single-dimension array. Assumes the named range is three columns by xx rows.

    [vba]Option Explicit
    Dim MyData As Range
    Dim VaInfo(1 To 3) As Variant

    Public Sub cmdChangeInfo_Click()

    Call MyChanges

    With Range("MyInfo")
    Set MyData = .Rows(2)
    MyData.Value = VaInfo
    End With

    Unload Me
    End Sub

    Public Sub MyChanges()
    VaInfo(1) = txtID.Value
    VaInfo(2) = txtDate.Value
    VaInfo(3) = txtName.Value
    End Sub
    [/vba]

    Hope this helps,

    Mark

    PS - Is there a reason for the subs to be public? I may well be guessing wrong, but these are in the UFs module I presume.

  3. #3
    GTO, thank you very much for your reply. The example I provided was a slim down example of the real thing, but it did produce the same results. I am looking forward to applying your example to see if I can get it to work. I will let you know. Thanks again.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by brorick
    GTO, thank you very much for your reply. The example I provided was a slim down example of the real thing, but it did produce the same results. I am looking forward to applying your example to see if I can get it to work. I will let you know. Thanks again.
    Very happy to help.

    Happy coding

    Mark

  5. #5
    GTO. Perfection. It worked. Thank you. The layout you provided makes sense. I do have a quick question. I haven't worked with Unload Me before. How does it work within this code?

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    'Me' in this case is the userform. Check the help topic, but in short, Unload literally unloads the userform, which means not only is it hidden/dismissed, but any variable local to the userform will be dropped from memory. Thus, if you did another Userform1.Show, any textboxes that you had filled in would be empty, etc.

    If Userform1.Hide was used, any values stored under the userform remain in memory.

    Not my best explanation, but hopefully of some help...

    Mark

  7. #7
    Great. I get it. Thanks once again for helping me with my issue.

Posting Permissions

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