Consulting

Results 1 to 4 of 4

Thread: Array declaration question - "myArray()" versus "myArray"

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    3
    Location

    Array declaration question - "myArray()" versus "myArray"

    I am trying to assign a dynamic range of cell values to an array. In that effort, I discovered one of my errors was how I dim the array, but I do not understand the reason why my original method does not work.

    The pertinent portion of my original code was as below but I received a type mismatch.

    Sub DataConversion()
    
    Dim rngInput, rngOutput As Range
    Dim LastInputRow, LastInputColumn, i, j, counter As Integer
    Dim arrayInput()
    
        With Sheets("Copy of Source Data")
        
            LastInputRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            LastInputColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    ...        
            Set rngInput = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    ...              
            arrayInput = rngInput
    ...
        End With
    ...
    end sub

    However, both of the following work:

    Sub DataConversion()
    
    Dim rngInput, rngOutput As Range
    Dim LastInputRow, LastInputColumn, i, j, counter As Integer
    Dim arrayInput()
    
        With Sheets("Copy of Source Data")
        
            LastInputRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            LastInputColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    ...        
            Set rngInput = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    ...           
            arrayInput = Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    ...
        End With
    ...
    end sub
    AND

    Sub DataConversion()
    
    Dim rngInput, rngOutput As Range
    Dim LastInputRow, LastInputColumn, i, j, counter As Integer
    Dim arrayInput
    
        With Sheets("Copy of Source Data")
        
            LastInputRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            LastInputColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    ...        
            Set rngInput = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    ...               
            arrayInput = rngInput
    ...
        End With
    ...
    end sub
    So, what gives? If I dim arrayInput with "()" AND I set the array using the .Range method, it works but by storing the range to a variable first does not? But, if I dim without "()" it works? I obviously am not understanding the declaration of the array fully.

    Can someone help me understand why dim arrayName()... arrayName = Range(x:y) works but arrayName = rngVariable does not? Thank you!

    CubsFan

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,715
    Location
    Assuming that the last 'As ...' applies to all the variables on the line is a carry over from other languages

    Each Dim variable needs a 'As <something>' otherwise it's Variant

    inputArray is a Variant containing an array of Variants

    rngInput is a Variant containing an Object of type Range


    Capture.JPG



    Look this over

    Option Explicit
    
    Sub DataConversion_1()
         
        Dim rngInput As Range, rngOutput As Range   '<<<<<<<<<<<<<<<<<<<<<<<
        Dim LastInputRow As Long, LastInputColumn As Long, i As Long, j As Long, counter As Long
        Dim arrayInput As Variant    '<<<<<<<<<<<<<<<<<<<<<<<<<
         
        MsgBox VarType(rngInput)
         
        With Sheets("Copy of Source Data")
             
            LastInputRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            LastInputColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
            Set rngInput = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
            arrayInput = rngInput
        
            MsgBox IsObject(rngInput)
            MsgBox IsObject(arrayInput)
            MsgBox IsArray(arrayInput)
        
            For i = LBound(arrayInput, 1) To UBound(arrayInput, 1)
                For j = LBound(arrayInput, 2) To UBound(arrayInput, 2)
                    MsgBox i & " --- " & j
                Next j
            Next I
        
        
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Banned VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,648
    Or

    Sub M_snb() 
      sn=Sheets("Copy of Source Data").cells(1).currentregion
    
      msgbox typename(sn)
    End Sub

  4. #4
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    3
    Location
    I thank you both for the education! :-) Now I can go about my business again without the two-year-old in my brain saying "but why?"

Posting Permissions

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