Starbuck1975
New Member
- Joined
- Sep 10, 2014
- Messages
- 5
My VB skills are basic at best, but I am trying to code a fairly straightforward iterative find and replace macro. The macro I wrote actually works, but crashes with an Overflow error after 30,000 iterations.
I have a data report that shows item usage for a group of users. This report resides on "Sheet1" and consists of over 100,000 rows of data. One column in this report provides an alphanumeric code that represents the item being used. I run a variety of pivot tables from this report and would like to pivot on item usage by the actual item's name instead of the alphanumeric code.
I have a second Sheet, "Sheet2", that translates the alphanumeric codes into the actual item names. There are 250 possibilities, which is why I am not doing a simple Find and Replace. This is a simple two column look-up table, but the data refreshes weekly, so it is not always 250 potential matches.
Ideally I want to dump the latest data into Sheet 1 every week, run the macro so I can pivot on the Item Name and then refresh all of my Pivot Tables with the new data.
I wrote a macro that takes the alphanumeric code from "Sheet1", compares it to every lookup code on "Sheet2", and if it finds a match, it writes the actual item name in a new column on "Sheet1". If it doesn't find a match, which is a possibility, it simply assigns a value of "Other". It keeps iterating until it hits a null cell, which means it has run out of data to check. The total index changes week to week, so I can't set the total iterations to a constant value.
Here is a basic example of how it should work:
Sheet 1 (Data)
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Other Data
[/TD]
[TD]Other Data
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]100,000
[/TD]
[TD]AYZ321
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2 (Lookup)
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]XYZ123
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]250
[/TD]
[TD]MYZ123
[/TD]
[TD]Burgers
[/TD]
[/TR]
</tbody>[/TABLE]
After running the macro, Sheet 1 would now show:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Other Data
[/TD]
[TD]Other Data
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]100,000
[/TD]
[TD]AYZ321
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Other
[/TD]
[/TR]
</tbody>[/TABLE]
The code I wrote does what I need it to do even if it probably isn't the best way to do this, but gives an overflow error after 30,000 iterations, which I wrote as follows:
Dim i As Integer
Dim j As Integer
Dim App As String
Dim Code As String
Dim Family As String
Dim Match As Boolean
i = 2
j = 2
Match = False
Do
App = ActiveWorkbook.Worksheets("RawData").Cells(i, 6).Value
Do
Code = ActiveWorkbook.Worksheets("Applications").Cells(j, 1).Value
Family = ActiveWorkbook.Worksheets("Applications").Cells(j, 2).Value
If App = Code Then
Match = True
ActiveWorkbook.Worksheets("RawData").Cells(i, 20).Value = Family
ElseIf Code = vbNullString Then
Match = True
ActiveWorkbook.Worksheets("RawData").Cells(i, 20).Value = "Other"
Else
j = j + 1
End If
Loop Until Match = True
j = 2
Match = False
i = i + 1
Loop Until App = vbNullString
I have a data report that shows item usage for a group of users. This report resides on "Sheet1" and consists of over 100,000 rows of data. One column in this report provides an alphanumeric code that represents the item being used. I run a variety of pivot tables from this report and would like to pivot on item usage by the actual item's name instead of the alphanumeric code.
I have a second Sheet, "Sheet2", that translates the alphanumeric codes into the actual item names. There are 250 possibilities, which is why I am not doing a simple Find and Replace. This is a simple two column look-up table, but the data refreshes weekly, so it is not always 250 potential matches.
Ideally I want to dump the latest data into Sheet 1 every week, run the macro so I can pivot on the Item Name and then refresh all of my Pivot Tables with the new data.
I wrote a macro that takes the alphanumeric code from "Sheet1", compares it to every lookup code on "Sheet2", and if it finds a match, it writes the actual item name in a new column on "Sheet1". If it doesn't find a match, which is a possibility, it simply assigns a value of "Other". It keeps iterating until it hits a null cell, which means it has run out of data to check. The total index changes week to week, so I can't set the total iterations to a constant value.
Here is a basic example of how it should work:
Sheet 1 (Data)
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Other Data
[/TD]
[TD]Other Data
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]100,000
[/TD]
[TD]AYZ321
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Initially blank
[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2 (Lookup)
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]XYZ123
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]250
[/TD]
[TD]MYZ123
[/TD]
[TD]Burgers
[/TD]
[/TR]
</tbody>[/TABLE]
After running the macro, Sheet 1 would now show:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Index
[/TD]
[TD]Item Code
[/TD]
[TD]Other Data
[/TD]
[TD]Other Data
[/TD]
[TD]Actual Item Name
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]ABC123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Beer
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]XYZ123
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Pizza
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[TD].
[/TD]
[/TR]
[TR]
[TD]100,000
[/TD]
[TD]AYZ321
[/TD]
[TD]Other data
[/TD]
[TD]Other data
[/TD]
[TD]Other
[/TD]
[/TR]
</tbody>[/TABLE]
The code I wrote does what I need it to do even if it probably isn't the best way to do this, but gives an overflow error after 30,000 iterations, which I wrote as follows:
Dim i As Integer
Dim j As Integer
Dim App As String
Dim Code As String
Dim Family As String
Dim Match As Boolean
i = 2
j = 2
Match = False
Do
App = ActiveWorkbook.Worksheets("RawData").Cells(i, 6).Value
Do
Code = ActiveWorkbook.Worksheets("Applications").Cells(j, 1).Value
Family = ActiveWorkbook.Worksheets("Applications").Cells(j, 2).Value
If App = Code Then
Match = True
ActiveWorkbook.Worksheets("RawData").Cells(i, 20).Value = Family
ElseIf Code = vbNullString Then
Match = True
ActiveWorkbook.Worksheets("RawData").Cells(i, 20).Value = "Other"
Else
j = j + 1
End If
Loop Until Match = True
j = 2
Match = False
i = i + 1
Loop Until App = vbNullString
Last edited: