Help fixing run-time error 7

aquapowers

New Member
Joined
Jan 17, 2008
Messages
24
I use the macro below to transpose many rows of data into columns. I keep getting the run-time error '7' unless I break the data into ~10,000 rows increments. The count changes, but I'm currently attempting to run the macro with 58,000 rows. Is there anything that can be changed in the following macro to help fix this and get rid of the run-time error?

I don't know VBA and someone was kind enough to create this macro many years ago here on MrExcel.

VBA Code:
Sub Step1()
Dim a, b(), i As Long, n As Long, maxCol As Integer, w()
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 3).Value
ReDim b(1 To UBound(a, 1), 1 To Columns.Count)
With CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(a, 1)
        If Not IsEmpty(a(i, 1)) Then
            If Not .exists(a(i, 1)) Then
                n = n + 1: b(n, 1) = a(i, 1): b(n, 2) = a(i, 2): b(n, 3) = a(i, 3)
                .Add a(i, 1), Array(n, 3)
            Else
                w = .Item(a(i, 1)): w(1) = w(1) + 2
                b(w(0), w(1) - 1) = a(i, 2): b(w(0), w(1)) = a(i, 3)
                .Item(a(i, 1)) = w
                maxCol = WorksheetFunction.Max(maxCol, w(1))
            End If
        End If
    Next
End With
With Range("e2")
    .Resize(, maxCol).EntireColumn.ClearContents
    .Resize(, 3).Value = [{"Item","Qty1","Price1"}]
    With .Offset(, 1).Resize(, 2)
        .AutoFill .Resize(, maxCol - 1)
    End With
    .Resize(n, maxCol).Value = b
End With
End Sub
 
Last edited by a moderator:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Since the script your showing does not do what you want. And you have not explained what your trying to do. Would you please explain in detail what your wanting to accomplish
Transpose many rows to columns is not specific for me.
 
Upvote 0
VBA Code:
Sub Step1()
Dim a, b(), i As Long, n As Long, maxCol As Integer, w()
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 3).Value
ReDim b(1 To UBound(a, 1), 1 To Columns.Count)
With CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(a, 1)
        If Not IsEmpty(a(i, 1)) Then
            If Not .exists(a(i, 1)) Then
                n = n + 1: b(n, 1) = a(i, 1): b(n, 2) = a(i, 2): b(n, 3) = a(i, 3)
                .Add a(i, 1), Array(n, 3)
            Else
                w = .Item(a(i, 1)): w(1) = w(1) + 2
                b(w(0), w(1) - 1) = a(i, 2): b(w(0), w(1)) = a(i, 3)
                .Item(a(i, 1)) = w
                maxCol = WorksheetFunction.Max(maxCol, w(1))
            End If
        End If
    Next
End With
With Range("e2")
    .Resize(, maxCol).EntireColumn.ClearContents
    .Resize(, 3).Value = [{"Item","Qty1","Price1"}]
    With .Offset(, 1).Resize(, 2)
        .AutoFill .Resize(, maxCol - 1)
    End With
    .Resize(n, maxCol).Value = b
End With
End Sub
[QUOTE="My Aswer Is This, post: 5769193, member: 292216"]
Since the script your showin
g does not do what you want. And you have not explained what your trying to do. Would you please explain in detail what your wanting to accomplish
Transpose many rows to columns is not specific for me.
[/QUOTE]
The marco takes three columns of data and transposes it into one row with up to nine quantity and rate columns per unique material.

Raw data example:
MaterialScale QuantityRate
21 135355
10​
47.29​
21 135355
50​
37.83​
21 135355
250​
22.93​
21 135355
500​
20.33​
21 135355
1000​
19.48​
21 135355
3000​
17.98​
23 135355
500​
21.35​
23 135355
1000​
20.45​
23 135355
3000​
18.88​

Output of macro:
(I add the row headers)

MaterialScale1Rate1Scale2Rate2Scale3Rate3Scale4Rate4Scale5Rate5Scale6Rate6Scale7Rate7Scale8Rate8Scale9Rate9
21 135355
10​
47.29​
50​
37.83​
250​
22.93​
500​
20.33​
1000​
19.48​
3000​
17.98​
23 135355
500​
21.35​
1000​
20.45​
3000​
18.88​
 
Upvote 0
How many output columns do you normally get?
 
Upvote 0
In that case try this change
VBA Code:
ReDim b(1 To UBound(a, 1), 1 To 30)
 
Upvote 0
Solution
Yes, this worked with 58,000+ rows without getting the run-time error! Thank you!!
For learning purposes, what does changing the line from "1 To Columns.Count" to "1 to 30" do?
 
Upvote 0
It simply defines the size of the array, with your code you were making it 16,384 columns wide, but with the change I suggested it's only 30 columns wide. Therefore it doesn't need as much memory.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,737
Members
449,185
Latest member
hopkinsr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top