VBA - Automation for replicating data under certain conditions

CleverUserName

New Member
Joined
Nov 25, 2018
Messages
11
view
Hello,
I have various spreadsheets that can contain mailing addresses with anywhere from 60,000 to 140,000 records. On certain records the street address, which is represented in one cell, is listed as a range of home address numbers, such as 145-150 Smith Rd. The City, State and Zip are also in individual cells.
I need some VBA code that will look at each target record in the spreadsheet, determine if it has a range of home address numbers within it and then duplicate the record "X" amount of times but list each home number separately contained in the original range.

EXAMPLE
Ad
view
dress
CityStateZip
100-103 Columbia AveABCNY123456


9-10 Congress StABCNY123457


1 N Cutler StABCNY123458


245- West StABCNY123459


25A-26B North StABCNY123460


-30 Center StABCNY123461
RESULT
AddressCityStateZipInformational Notes Only
100-103 Columbia AveABCNY123456
Original Record
100 Columbia AveABCNY123456Dupe Record - Split out
101 Columbia AveABCNY123456Dupe Record - Split out
102 Columbia AveABCNY123456Dupe Record - Split out
103 Columbia AveABCNY123456Dupe Record - Split out
9-10 Congress StABCNY123457
Original Record
9 Congress StABCNY123457Dupe Record - Split out
10 Congress StABCNY123457Dupe Record - Split out
1 N Cutler StABCNY123458Original Record - No Range
245- West StABCNY123459
Original Record - No Range
25A-26B North StABCNY123460
Original Record -Not Numeric Range
-30 Center StABCNY123461
Original Record - No Range

<tbody>
</tbody>

https://drive.google.com/file/d/14If_GHcwF-xZjn8eVArFOSVHegLsxZao/view?usp=sharing
 
Apologies, I just realized that you do handle the copying of CITY,STATE,ZIP. I just did not see it as that part of the code did not execute due to the error received
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Thank you. I tried this code on about 30,000 records and excel was indicating that it was not responding. I'll get this when I run large procedures, but it typically recovers. I let your code run for 10 minutes and then tried to [ctl]+[Break] to stop, but could not. So I had to end excel via the task manager.

I tried to re-write it do do more in arrays. I'm still learning them. It's not as concise as the previous code, but you can try this. I've created separate input and output sheets so that you can compare the results. You'll need to update those with your address input sheet name, and create a blank sheet called "Output". It could be modified to write over the original once you are sure it's working.

Code:
Sub FindReplace()
Dim Arr As Variant
Dim AddArr() As String, OutArr() As String, OutArrTrans As Variant
Dim sAdd As String, sNum As String, sStreet As String
Dim sCity As String, sState As String, sZip As String
Dim iNum1 As Long, iNum2 As Long
Dim i As Long, j As Long, n As Long, nRng As Integer
Dim wb As Workbook, wsIn As Worksheet, wsOut As Worksheet
Dim rOut As Range

Set wb = ActiveWorkbook
Set wsIn = wb.Worksheets("Input") 'input sheet
Set wsOut = wb.Worksheets("Output") 'output sheet


Arr = wsIn.UsedRange.Value2

ReDim OutArr(0)
OutArr(0) = "Address|City|State|Zip"

j = 0
    For i = 2 To UBound(Arr)
        
        sAdd = Arr(i, 1)
        AddArr = Split(sAdd, " ", 2)
        sNum = AddArr(0)
        sStreet = " " & AddArr(1)
        sCity = Arr(i, 2)
        sState = Arr(i, 3)
        sZip = Arr(i, 4)
        n = UBound(OutArr) + 1
        ReDim Preserve OutArr(n)
        OutArr(n) = sAdd & "|" & sCity & "|" & sState & "|" & sZip
        
        nRng = InStr(1, sNum, "-", 1)
        If nRng <> 0 Then
            If IsNumeric(Left(sNum, nRng - 1)) And IsNumeric(Mid(sNum, nRng + 1, Len(sNum))) Then
                j = n + 1
                iNum1 = Left(sNum, nRng - 1)
                iNum2 = Mid(sNum, nRng + 1, Len(sNum))
                    Do Until iNum1 = iNum2 + 1
                        ReDim Preserve OutArr(j)
                        OutArr(j) = iNum1 & sStreet & "|" & sCity & "|" & sState & "|" & sZip
                        
                        iNum1 = iNum1 + 1
                        j = j + 1
                    Loop
            End If
        End If
    Next i
OutArrTrans = Application.Transpose(OutArr)
n = UBound(OutArr) + 1
With wsOut
    Set rOut = .Range(.Cells(1, 1), .Cells(n, 1))
End With
rOut = OutArrTrans
rOut.TextToColumns Destination:=Range("A1"), Other:=True, OtherChar:="|"
End Sub
 
Upvote 0
Thank you for this code. Very elegant. I did try this on about 30,000 records and keep getting an error when R = 5697. Error reads Subscript out of range. I think it is referring to "Nums(1)".
When the error occurs at R = 5697, what does the Txt variable have in it?

Also, what version of Excel are you using?
 
Upvote 0
At the error point Txt = "128 Norman Rd 1-4"
I am using Excel 365.

Most likely because of the "1-4"

I was leary of hyphens other places in the street address which is why I went a different route and split after the first space. My new code should work, but maybe Rick can fix his as well. That code is a bit above my head. :)
 
Upvote 0
While I was trying to run the new code that BAlGaInTl posted, I kept running into the same issue with Exel not responding. After looking at the 30K line data set I was testing with I found 2 additional scenarios.
1. "#-#-"
2. "#,
#" In this scenario, the second number in red was smaller than the first.

After removing these scenarios, both codes executed. I can certainly try and modify these manually, but if these scenarios could be included, I would be most grateful.

Mr. Rothstein, I believe your code stops executing when "R" reaches Cells(Rows.Count, "A").End(xlUp).Row. However, due to the fact that we are expanding the number of rows by inserting the values after we split up an address with a range, the spreadsheet row numbers continue to grow and therefore only part of the data gets reviewed and modified. Am I right in this interpretation?
 
Upvote 0
While I was trying to run the new code that BAlGaInTl posted, I kept running into the same issue with Exel not responding. After looking at the 30K line data set I was testing with I found 2 additional scenarios.
1. "#-#-"
2. "#,
#" In this scenario, the second number in red was smaller than the first.

After removing these scenarios, both codes executed. I can certainly try and modify these manually, but if these scenarios could be included, I would be most grateful.

Mr. Rothstein, I believe your code stops executing when "R" reaches Cells(Rows.Count, "A").End(xlUp).Row. However, due to the fact that we are expanding the number of rows by inserting the values after we split up an address with a range, the spreadsheet row numbers continue to grow and therefore only part of the data gets reviewed and modified. Am I right in this interpretation?

Scenario 1 above should be included and split up
Scenario 2 should be ignored
Thank you.
 
Upvote 0
While I was trying to run the new code that BAlGaInTl posted, I kept running into the same issue with Exel not responding. After looking at the 30K line data set I was testing with I found 2 additional scenarios.
1. "#-#-"
2. "#,
#" In this scenario, the second number in red was smaller than the first.

After removing these scenarios, both codes executed. I can certainly try and modify these manually, but if these scenarios could be included, I would be most grateful.

Mr. Rothstein, I believe your code stops executing when "R" reaches Cells(Rows.Count, "A").End(xlUp).Row. However, due to the fact that we are expanding the number of rows by inserting the values after we split up an address with a range, the spreadsheet row numbers continue to grow and therefore only part of the data gets reviewed and modified. Am I right in this interpretation?

On Rick's code, try changing

Code:
For R = 2 To Cells(Rows.Count, "A").End(xlUp).Row

to

Code:
For R = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1

I don't know if that will work or not, but it will add the rows backwards.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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