How do I write a VBA that will give me the ranges for zip codes based on the following:
Column A Column B Column C Column D
Given Zip Code Given Zone Result Range Result Zone
00210 1 00210-00547 1
00544 1 00548-00839 2
00548 2 00840-01100 3
00840 3 01101-01199 1
01101 1
01200 2
A wonderful human gave me the below but it’s doing a new row for every zip and I need it to do only a new row if the zone number changes.
Sub try_this()
Dim y As Long, lastrow As Long, sht As Worksheet
Set sht = Worksheets(“put_your_sheet_name_here”) ‘change this
‘get last row
lastrow = sht.Cells(sht.Rows.Count, “A”).End(xlUp).Row
For y = 1 To lastrow – 1
With sht.Cells(y, 1)
‘create range in 3rd column
.Offset(0, 2).Value = .Value & “-” & .Offset(1, 0).Value – 1
‘copy range name to 4th column
.Offset(0, 3).Value = .Offset(0, 1).Value
End With
Next
End Sub
Nancy Sellers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.