VBA Help

ExceLoki

Well-known Member
Joined
Dec 13, 2021
Messages
538
Office Version
  1. 365
Platform
  1. Windows
Going to start off by stating, VBA is nto my strong suit. What I'm trying to do is have a script that will edit the values in the "ZIP" column of this table i'm working in. My data range can be anywhere from 200-6000 lines. I would like it to edit the first cell and then go to the next until there are no more.
The formula I came up with to get what I am looking for is below:
Excel Formula:
=IFS(LEN(create_users_20221122_1134323[@Zip])=5,create_users_20221122_1134323[@Zip],RIGHT(create_users_20221122_1134323[@Zip],4)="0000",LEFT(create_users_20221122_1134323[@Zip],5),LEN(create_users_20221122_1134323[@Zip])=10,TEXTJOIN("-",TRUE,LEFT(create_users_20221122_1134323[@Zip],5),RIGHT(create_users_20221122_1134323[@Zip],4)))

My input is in column A and desired results in column B
-------------------
fix zip codes.xlsm
AB
1Zipformula
23050630506
30#N/A
40457104571
529926 227229926-2272
622201 170122201-1701
719072 000019072
822027 000022027
932163 000032163
1021045 252921045-2529
1133414 624533414-6245
121176811768
131002510025
Sheet1
Cell Formulas
RangeFormula
B2:B13B2=IFS(LEN(Table3[@Zip])=5,Table3[@Zip],RIGHT(Table3[@Zip],4)="0000",LEFT(Table3[@Zip],5),LEN(Table3[@Zip])=10,TEXTJOIN("-",TRUE,LEFT(Table3[@Zip],5),RIGHT(Table3[@Zip],4)))
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about this?

VBA Code:
Sub LOKI()
Dim ws As Worksheet:        Set ws = ActiveSheet
Dim r As Range:             Set r = ws.Range("Table3")
Dim AR() As Variant:        AR = r.Value2
Dim tmp As String
Dim sp() As String

For i = 1 To UBound(AR)
    tmp = AR(i, 1)
    If InStr(tmp, " ") > 0 Then
        sp = Split(tmp, " ")
        If sp(1) = "0000" Then
            AR(i, 1) = sp(0)
        Else
            AR(i, 1) = Replace(tmp, " ", "-")
        End If
    ElseIf Len(tmp) = 1 Then
        AR(i, 1) = "#N/A"
    End If
Next i

r.Value2 = AR
End Sub
 
Upvote 0
Thanks for the quick reply. It worked great, except in row 4 it returned 4571 instead of 04571. Is there a way to fix that?
-----------------
fix zip codes.xlsm
A
1Zip
230506
3#N/A
44571
529926-2272
622201-1701
719072
822027
932163
1021045-2529
1133414-6245
1211768
1310025
Sheet1
 
Upvote 0
Try this one.

VBA Code:
Sub LOKI2()
Dim ws As Worksheet:        Set ws = ActiveSheet
Dim r As Range:             Set r = ws.Range("Table3")
Dim AR() As Variant:        AR = r.Value2
Dim tmp As String
Dim sp() As String

For i = 1 To UBound(AR)
    tmp = AR(i, 1)
    Select Case Len(tmp)
        Case 1
            AR(i, 1) = "#N/A"
        Case Is > 5
            sp = Split(tmp, " ")
            If sp(1) = "0000" Then
                AR(i, 1) = sp(0)
            Else
                AR(i, 1) = Replace(tmp, " ", "-")
            End If
        Case Else
            AR(i, 1) = "'" & CStr(tmp)
    End Select
Next i

r.Value2 = AR
End Sub
 
Upvote 0
getting an error
1669664444416.png
 
Upvote 0
and here is a more accurate version of the data
-----------------
fix zip codes.xlsm
ABCDEFGHIJKLM
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Zip
260525
360657
429483
560047
628604
760471
887004
961615
1060613
1185225
1234266
1320816
1430506
1554634
1660516
1772653
1825425
1960098
2060482
2185266
2220910
2346321
2491977
2547203
2633947
2760033
2860457
2960452
3060451
3160056
3221230
3360473
3460615
3560477
3660515
3760463
3821108
3908051
4055811
4189081
4246374
4333487
4460126
4561353
4637923
4760402
4861821
4933761
5078624
5160657
5260409
5360453
5460164
5560189
5633434
5789144
5834238
5934231
6063011
6160640
6260137
6360201
6449504
6553188
6692637
6760188
6860403
6960441
7029414
7104571
7275013
7360613
7460448
7533763
7660008
7760103
7860193
7960610
8085250
8160093
8260619
8360477
8450266
8560163
8660035
8760525
8897330
8960611
9033626
9160441
9283704
9397201
9460098
9560098
9660045
9792108
9860477
9960014
10060304 1356
10180526 1557
10260657 0000
10360610 7898
10460005 1849
10560657 4222
10660614 2510
10760525 2352
10860302 2025
10960657 2024
11029926 2272
11176712 0000
11260612 1142
11322201 1701
11419072 0000
11522027 0000
11660657 2980
11760093 1721
11862656 3127
11960025 4022
12077024 4502
12160047 7694
12285718 0000
12353217 0000
12460010 0000
12560124 3801
12660069 3315
12732163 0000
12860558 1051
12985020 4731
13020853 2536
13121045 2529
13260010 0000
13360177 2738
13460707 3942
13560563 2678
13660201 2404
13760044 2036
13898004 1336
13933414 6245
14060302 0000
14160643 4807
14260640 5301
14360646 2836
14460521
14560706 2186
14660022 2027
1470
14846534
14960148
15060035
New Sheet
 
Last edited:
Upvote 0
i thought it'd be easier to change from original post to the table i need it in, but again... " VBA is nto my strong suit"
 
Upvote 0
This code worked when I tested it. The only change I made was on the second line where I am assigning the 'r' range variable to the specific 'Zip' column in your table.

VBA Code:
Sub LOKI2()
Dim ws As Worksheet:        Set ws = ActiveSheet
Dim r As Range:             Set r = ws.Range("Table3[Zip]")
Dim AR() As Variant:        AR = r.Value2
Dim tmp As String
Dim sp() As String


For i = 1 To UBound(AR)
    tmp = AR(i, 1)
    Select Case Len(tmp)
        Case 1
            AR(i, 1) = "#N/A"
        Case Is > 5
            sp = Split(tmp, " ")
            If sp(1) = "0000" Then
                AR(i, 1) = sp(0)
            Else
                AR(i, 1) = Replace(tmp, " ", "-")
            End If
        Case Else
            AR(i, 1) = "'" & CStr(tmp)
    End Select
Next i

r.Value2 = AR
End Sub
 
Upvote 0
I don't know why you would be getting that error. I copied the data from your post and it ran fine.

What is the value of i when you get the error?

Another thing to possibly try... I did the same thing in Power Query. Not sure if your very familiar with that.

PQ
A
1Zip
260525
360657
429483
560047
628604
760471
887004
961615
1060613
1185225
1234266
1320816
1430506
1554634
1660516
1772653
1825425
1960098
2060482
2185266
2220910
2346321
2491977
2547203
2633947
2760033
2860457
2960452
3060451
3160056
3221230
3360473
3460615
3560477
3660515
3760463
3821108
398051
4055811
4189081
4246374
4333487
4460126
4561353
4637923
4760402
4861821
4933761
5078624
5160657
5260409
5360453
5460164
5560189
5633434
5789144
5834238
5934231
6063011
6160640
6260137
6360201
6449504
6553188
6692637
6760188
6860403
6960441
7029414
714571
7275013
7360613
7460448
7533763
7660008
7760103
7860193
7960610
8085250
8160093
8260619
8360477
8450266
8560163
8660035
8760525
8897330
8960611
9033626
9160441
9283704
9397201
9460098
9560098
9660045
9792108
9860477
9960014
10060304-1356
10180526-1557
10260657
10360610-7898
10460005-1849
10560657-4222
10660614-2510
10760525-2352
10860302-2025
10960657-2024
11029926-2272
11176712
11260612-1142
11322201-1701
11419072
11522027
11660657-2980
11760093-1721
11862656-3127
11960025-4022
12077024-4502
12160047-7694
12285718
12353217
12460010
12560124-3801
12660069-3315
12732163
12860558-1051
12985020-4731
13020853-2536
13121045-2529
13260010
13360177-2738
13460707-3942
13560563-2678
13660201-2404
13760044-2036
13898004-1336
13933414-6245
14060302
14160643-4807
14260640-5301
14360646-2836
14460521
14560706-2186
14660022-2027
147#N/A
14846534
14960148
15060035
Table3


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    ROC = Table.SelectColumns(Source,{"Zip"}),
    Custom1 = Table.TransformColumns(ROC,{{"Zip", each 
        let 
            SP = try Text.Split(_," ") otherwise {_},
            X = if List.Count(SP) = 2 then List.Transform(SP, each if _ ="0000" then null else _) else {Text.From(_)} & {null},
            C = Text.Combine(X,"-")
        in 
            C
    }}),
    Custom2 = Table.TransformColumns(Custom1,{{"Zip", each if _ = "0" then "#N/A" else _}})
in
    Custom2
 
Upvote 0

Forum statistics

Threads
1,215,038
Messages
6,122,798
Members
449,095
Latest member
m_smith_solihull

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