+ Reply to Thread
Results 1 to 2 of 2

Storing Range Values into Array

Hybrid View

thewizz Storing Range Values into... 08-07-2012, 12:54 PM
TMS Re: Storing Range Values into... 08-07-2012, 01:18 PM
  1. #1
    Registered User
    Join Date
    08-07-2012
    Location
    Ontario
    MS-Off Ver
    Excel 2003
    Posts
    4

    Storing Range Values into Array

    Hello,

    I am having an issue when trying to store values from a user selected range into a data array. I want to do this so that I can pass this array into another subroutine and create a histogram.

    The error given is mismatch between range and array type but don't know how to fix this.

    Code:

    Sub UserInput()
    
    Dim dataRange As Range
    Dim dataArray() As Variant
    Dim i As Integer
    Dim cell As Range
    Dim row, col As Variant
    
    Set dataRange = Application.InputBox(prompt:="Select a Range of Data", Title:="Range Collection", Type:=8)
    
    If dataRange Is Nothing Then
       dataRange = MsgBox("Invalid Range, please retry", vbOKCancel + vbQuestion)
          If dataRange = vbCancel Then 'no retry
               Exit Sub
           Else
                Run "RangeAsObject"
           End If
    Else
    
    For Each row In dataRange.Rows
        For Each col In dataRange.Columns
        
                dataArray(row, col) = Cells(row, col).Value '***?????
                                  'Store values from selection into Array
                                'Array will need to be passed into histogram subroutine
        Next col
    Next row
    
    
    End If
    
    End Sub
    Thanks!
    Last edited by thewizz; 08-07-2012 at 01:29 PM. Reason: code tags

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,596

    Re: Storing Range Values into Array

    To comply with forum rules you should add code tags to your first post.

    An example of populating arrays from ranges:

    Sub PopulateArray()
    
    Dim Array1
    Dim Array2
    Dim i As Long
    
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    Array1 = awf.Transpose(Range("A1:A10"))
    Array2 = awf.Transpose(awf.Transpose(Range("A1:G1")))
    
    For i = LBound(Array1) To UBound(Array1)
        Debug.Print Array1(i)
    Next i
    
    For i = LBound(Array2) To UBound(Array2)
        Debug.Print Array2(i)
    Next i
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ 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