Solved

How to pass an array a subroutine Excel VBA

Posted on 2010-09-13
3
767 Views
Last Modified: 2012-08-14
Hi,

I am trying to write a script that scans the contents of the first cell in the first 52 columns, then askes the user which columns they have data in that they would like merged. Then I would like to take their response and pass it to a subroutine to merge the cells appropriately. However, I am having trouble passing an array to my subroutine. I keep getting "Run-time error '1004': Application-defined or object defined error" Below is the snippet of code i am referring to. The error highlights the portion of code where i am merging 2 cells together. I think there is some sort of type mismatch, because when I change "Column" to a specific number...say 3, then the code runs without errors and merges the cells properly in column 3.

Private Sub cmdOkay_Click()
Dim i As Long, msg As String, Check As String, boxa As String, boxb As String
Dim Arr(52, 1)

Dim j, k As Integer
j = 0
k = 0

'Generate a list of the selected items
With ListBox1
    For i = 0 To .ListCount - 1
        k = k + 1
        If .Selected(i) Then
            msg = msg & .List(i) & vbNewLine
            Arr(j, 1) = k + 1
            j = j + 1
        End If
    Next i
End With
...
...
If Check = vbYes Then
    'CheckBox3
    If UserForm1.CheckBox3.Value = True Then
        Module4.Carrier_Merge Arr
    End If
    
    'Unload the userform since user is happy with selection(s)
    Unload Me
Else
    'User wants to try again, so clear listbox selections and
    'return user to the userform
    For i = 0 To ListBox1.ListCount - 1
        ListBox1.Selected(i) = False
    Next
End If
...
...
...
End Sub

Sub Carrier_Merge(ArrValues())

Dim i
Dim row1 As Integer
Dim row2 As Integer
Dim Column

Column = 0
row1 = 2
row2 = 3

Do Until IsEmpty(Range("A" & row1))
    If Right(Range("A" & row1), 2) <> "_3" And Right(Range("A" & row2), 2) = "_3" Then
        
        For i = 0 To UBound(ArrValues)
            Column = ArrValues(i, 1)
            Worksheets("Cell Site Demand").Range(Cells(row1, Column), Cells(row2, Column)).Merge
        Next i
        
          
       row1 = row1 + 2
       row2 = row2 + 2
       
    Else
  
    row1 = row1 + 1
    row2 = row2 + 1
      
    End If
    
Loop

...
...
end sub

Open in new window

0
Comment
Question by:towster2
3 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33666670
Pass it as a type Variant

Sub Carrier_Merge(ByVal ArrValues as Variant)

And just use it normally as you would an array.
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 33674006
Are you sure the error is caused by the array?

Sounds to me that it's being passed OK, if it wasn't then an error would have been raised earlier and the message wouldn't be the one you describe.

One thing you might want to consider is that you only reference a worksheet once in the sub that does the merging.

That means that Range and Cells will be looking at the active sheet.

It might be worth fixing that.

Then I suggest you put a watch on the variable column to check if it's getting the right value from the array.

PS You might want to consider using another name for that variable - Column is a VBA property of ranges etc
0
 

Author Closing Comment

by:towster2
ID: 33674612
Thanks for your help. You were dead on!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now