Split address code not working

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello formula experts
I have this code which splits the address from 1 cell to 4 in the adjacent cells. It was working in an earlier application but when I tried to change and edit the range compatible for this application it is not splitting the data. I would really appreciate it, if someone could please help me to correct the code.
Split address.xlsm
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I doubt anyone will be overly keen to download a macro-enabled workbook, Office blocks macros by default requiring the user to go through various steps to unblock them, it's a bit of a faff. You might get a better response if you post the code along with an example of your inputs and outputs (both expected and actual).

You can try xl2bb to create HTML versions of your sheets - XL2BB - Excel Range to BBCode
VBA code tags are in the WYSIWYG editor.
 
Upvote 0
I doubt anyone will be overly keen to download a macro-enabled workbook, Office blocks macros by default requiring the user to go through various steps to unblock them, it's a bit of a faff. You might get a better response if you post the code along with an example of your inputs and outputs (both expected and actual).
@dave3009 you are not the first to make that recommendation to @RAJESH1960.

...
I have this code which splits the address from 1 cell to 4 in the adjacent cells. It was working in an earlier application but when I tried to change and edit the range compatible for this application it is not splitting the data. I would really appreciate it, if someone could please help me to correct the code.
@RAJESH1960 I think you need to be more specific about what happens when you try to run the code that you altered.
 
Upvote 0
With the help of the code I am trying to split the address into 4 rows.
Split address.xlsm
PQRST
1AddressLine1Line2Line3Line4
2PLOT NO 477-78, SECTOR 29 PART 2, ,, HUDA, PANIPAT
3Flat No. 2,1 to 2,8, ,, Vikrant 4, RAJENDRA PLACE, Opposite Padam Tower, RAJENDRA PLACE
4FARM NO. 79A, KH-86, ,, ,, GREEN DRIVE, SILVER OAK FARM, NEW DELHI
5JAIPUR AIR PORT,SANGANER, ,, ,, JAIPUR,14,RAJASTHAN, Jaipur
6CAS, ,, CIVIL AIRPORT SANGENAR, SANGENAR, JAIPUR
7JAIPUR AIRPORT, ,, ,, TERMINAL 2, JAIPUR
81489, ,, ,, KAPTAN JI KI GALI 12 GANGOUR KA RASTA, JOHARI BAZAR
9B-121, ,, ,, SECTOR-5, NOIDA
10VIJAY MANDI, ,, MURADNAGAR, GHAZIABAD, GHAZIABAD
1113 - PLOT NO. 63-69, 2ND FLOOR, ,, NAGDEVI CROSS ROAD, MUMBAI
1265/A, GROUND FLOOR, KANCHAWALA BUILDING, DHANJI STREET, MUMBADEVI
1317, ,, MAHAVIR INDS. BLDG.,, SUN MILL COMPOUND,, LOWER PAREL,
14PLOT NO 61, ,, MAROL CO-OPERATIVE INDL ESTATE, SIR MAKVANA ROD, MAROL, ANDHERI EAST
15A-216, ,, SAGAR TECH PLAZA CHS LTD, ANDHERI KURLA ROAD SAKINAKA JUNCTION, MUMBAI
1639, 3rd FLOOR, ROOM NO 3,3, ,, OLD HANUMAN LANE, KALBADEVI
17GALA NO. 126, 1ST FLOOR, SONAL LINK INDUSTRIAL PREMISES CS LTD, LINK ROAD, OPP. MOVIE TIMES THEATRE, MALAD WEST
18NO.182, ,, ,, KAMARAJ ROAD, BHARATHINAGAR
19SHED NO 37/1, ,, NADAKERAPPA INDUSTRIAL ESTATE, ANDRAHALLI MAIN ROAD PEENYA 2ND STAGE, VISWANEEDAM POST
20NO 1, 2ND FLOOR, VST VISTAS, PALACE CROSS ROAD, OPP PALACE GROUND
2122/1, ,, ,, GODOWN STREET, K.R.MARKET
22NO.14, ,, ,, S J P ROAD, BENGALURU
23Plot No. 285, ,, Rajadhani Industrial Park, Road no. 7, KIADB 4th Phase Dabaspet Industrial Area Billenkote Village
24No.1, 1st and 2nd Floor, ,, 14th Cross, Sampige Road, Malleshwaram, Bangalore
25ROOM NO. 32 and 38, A25, ,, AIRSIDE BUILDING DEVENAHALLI, BANGALORE INTERNATIONAL AIRPORT, Bangalore
26REGD AND HEAD OFFICE, ,, POST BOX- 599, MAHAVEERA CIRCLE, KANKANADY, MANGALORE
27BENGALURU INTERNATIONAL AIRPORT, ,4-181, ,, DEVANHALLI, BANGLORE
28NO.12, GROUND AND FIRST FLOOR, ,, 3RD CROSS,, H.SIDDAIAH ROAD
2921/511/B 8,/4B, ,, ,, VALAGEREHALLI, KENGERI HOBLI, MYSORE ROAD
30NO16/2/3, ,, ,, ARAKERE GATE, B G ROAD
31NO.71/1,, ,, ,, 1ST MAIN,, NEWTIMBER YARD LAYOUT
32no.57/3, ,, ,, SADAR PATRAPPA ROAD, BEHIND S J PARK ROAD
33NO. 339, ,, ,, 1,TH CROSS,, MAHALAKSHMI LAYOUT
34No 1618, ,, ,, East End Main Road, Jayanagar 9th Block
35NO. 12,6, FIRST FLOOR, ,, 26TH MAIN, JAYANAGAR 9TH BLOCK
36No. 148, 2nd floor, Om Shree Plaza, Dr. DVG Road, BASAVANAGUDI
37NO.1/1, LNP CORNER, ,, WEST OF CHORD ROAD MANJUNATHNAGAR, RAJAJINAGAR BENGALURU
38163/A, GROUND AND 1ST FLOOR, -, S KARIYAPPA ROAD, TATA SILK FARM LAYOUT, BASAVANGUDI
39NO 42 43 44 45, ., ., SAJJANI RAO ROAD OLD WARD NO 5,, VV PURAM
40NO 11, 6TH AND 7TH FLOOR, DIVYASREE CHAMBERS, O SHAUGHNESSY ROAD, Akkithimanahalli
41OLD NO 19 NEW NO 27, ,, ,, S J P ROAD, S J P ROAD
42NO 47, ,, ,, DEVINAGAR MAIN ROAD OUTER RING ROAD, MARUTHI NAGAR
43No.24, ,, ,, 9th Cross H Siddaiah Road, Bangalore
44NO 134/2, GROUND FLOOR, ,, KILARI ROAD, BANGALORE
45NO 14, ,, ,, RT STREET, CHICKPET
4619/1, 3RD CROSS, CHAWLA MOTORS, JOURNALIST COLONY, BANGALORE
472, 2nd floor, ,, 1st A Main Vigna Nagar, BANGALORE
48NO 16/2, ,, ,, AREKERE, BANNERGHATTA MAIN ROAD
49339/1, Industrial Shed No. 2 Ground Floor, Anantharamiah Woolen Factory Compound, Mysore Road, Bapujinagar
501, ,, SRI MAHALAKSHMI FRAME WORKS, CHICKPET, CHITRADURGA
51NO.26/A,, GROUND,FIRST,SECOND FLOOR,, ,, 15 CROSS,1,, FEET ROAD,, SARAKKI 4TH PHASE EXTENSION,JP NAGAR 4TH PHASE,
5226/B, GROUND,FIRST AND SECOND FLOOR, ,, 15 CROSS 1,, FEET ROAD SAARAKKI 4TH PHASE EXTENTION, J P NAGAR-4
53SY NO 127,, Ground floor, Near Prakurthi Chandana Apartment, Kashi nagar ,, Amruthalli
54NO.16, GROUND FLOOR, R.D.COMPLEX, VICTORY AVENUE , HOSPITAL ROAD, AVENUE ROAD CROSS
55321, ,, ,, 3RD MAIN, 4TH CROSS, JP NAGAR 3RD PHASE, BANGALORE
56No 19/1, Ground Floor and First Fllor, ,, 1st cross, 2nd Main, Nagendra Garden, Srirampuram, Bengaluru
57NO.126, ,, ,, NEAR CHURCH, R T NAGAR POST, BANGALORE
58No.712/66, ,, ,, 19th Cross, Srirampura, Dr. Shivaramkaranth Nagar, MCECHS Layout, Yelahanka Hobli
59NO 173, GROUND FLOOR, ,, 1ST MAIN 2ND CROSS OPP SAR CONVENTION HALL, CHANDANA LAYOUT SUNKADAKATTE
603, 45/1,, ,, ,, HESARGHATTA MAIN ROAD, NEXT TO BANGALORE PATIDAR SAMAJ, RAJA REDDY LAYOUT, BAGALAKUNTE, BANGALORE
61no.61, 16th cross, ,, nagarbhavi 2nd stage, bangalore
62SY.NO.177/6A, ,, ,, BEGUR HOBLI, BILEKAHALLI VILLAGE
63Sy No 6/3B, ,, ,, 2nd Cross Begur Road, Bengaluru
6444/M, GROUND FLOOR, ,, SAI BABA TEMPLE ROAD, AREKERE
65NO.71/1,, ,, ,, NEW TIMBER YARD LAYOUT,, MYSORE ROAD,
66NO 3,,/3B, GROUND FLOOR, SWETHA NILAYA NEXT TO BSNL EXCHANGE BUILDING, BEGUR MAIN ROAD, BEGUR
67No 1364, Shop No ,1, Ground Floor, Susana Complex, South End Main Road, 28th Main, 9th Block, Jayanagar, Bengaluru
68NO.12/1, GROUND FLOOR, ,, 3RD CROSS, SEPPINGS ROAD, NEAR OM SHAKTHI TEMPLE, SHIVAJINAGAR
6957, ,, PUTTENAHALLI PALYA, PUTTENAHALLI MAIN ROAD, J.P NAGAR 7TH PHASE
70Site No.13/14, Katha No.62/2,, ,, ,, 1st Floor, 2nd Cross, Kodichikkanahalli Main Road,, Bommanahalli, Begur Hobli,
71SY NO 9/5 AND 9/6, ,, ,, VITTASANDRA BEGUR POST BEGUR HOBLI, BANGALORE
72SY NO OLD.6,,NEW NO 6,/4,, ,, ,, KACHOHALLI, MAGADI MAIN ROAD
73NO.159,NEW NO.144, ,, ,, EAST END,A MAIN ROAD,9TH BLOCK, JAYANAGAR
74No 1, GROUND FLOOR, ,, BIC SARRAKI GATE KANAKAPURA MAIN ROAD, J P NAGAR 1ST PHASE NEAR METRO STATION
75New No. 51, 9th Floor, Exotica, Venkatanarayana Road, Chennai
7658, ,, ,, PARK ROAD, ERODE
7715-8-441, ,, ,, FEELKHANA, BEGUMBAZAR
MasterData


The code I am trying to use but the range seems wrong.

Rich (BB code):
Option Explicit

Sub Split_Address()
    Dim c, a, l&
    Dim Data, Ledger, Chk, i As Long
    Dim J, k, n, ar, nChar, xstr
    Dim t()     As String
    Dim arr()
    Dim ws1     As Worksheet
'
    Set ws1 = Worksheets("MasterData")
'
    With ws1
        ar = .[A1].CurrentRegion    'row number..?
    End With
'
    ReDim Preserve arr(1 To UBound(ar, 1), 1 To 16)
'
    k = 1
    nChar = 30              'Restricts the number of characters in a cell up to total 120 characters, can edit if required in future
'
    For i = 2 To UBound(ar, 1)
        If ar(i, 16) = "" Then GoTo nexti  ' 5 is the full address in column P
        t = Split(ar(i, 16), ",")
        xstr = t(0)
        n = 1
        nChar = 20
'
        For J = 1 To UBound(t)
            t(J) = Trim(t(J))
'
            If t(J) <> "" Then
                If Len(xstr & t(J)) <= nChar Then
                    xstr = xstr & " " & t(J)
                Else
'                   ReDim Preserve arr(1 To 4, 1 To n)
                    arr(k, n) = Trim(xstr)
                    xstr = t(J)
                    n = n + 1
'
                    If n = 4 Then nChar = 100
                End If
            End If
        Next J
'
        If arr(k, n) = "" Then arr(k, n) = Trim(xstr)   'removes special characters and trims to fit 30 characters in each column
nexti:
        k = k + 1
    Next i
'
    ws1.[F2].Resize(UBound(arr, 1), 6) = arr        'destination first cell where data is split
'
    ws1.UsedRange.EntireColumn.AutoFit                                                          '   Set all used columns on sheet wide enough for data
End Sub
 
Upvote 0
I doubt anyone will be overly keen to download a macro-enabled workbook, Office blocks macros by default requiring the user to go through various steps to unblock them, it's a bit of a faff. You might get a better response if you post the code along with an example of your inputs and outputs (both expected and actual).

You can try xl2bb to create HTML versions of your sheets - XL2BB - Excel Range to BBCode
VBA code tags are in the WYSIWYG editor.
I was also facing the same issue. You have to just right click on the excel sheet name and unblock to enable the macro. Sometimes there are so many sheets involved, XL2BB does not accept as it is limited to some bytes only.
 
Upvote 0
With the help of the code I am trying to split the address into 4 rows.
We have had this discussion before also ...
rows are vertical
columns are horizontal

The code I am trying to use but the range seems wrong.

Explain, what range you are referring to & why you feel it seems wrong.

When you post questions, please remember that we can't see what you are looking at, so please be as specific as possible about what sheet you are talking about, what address, etc.

You might get a better response if you post the code along with an example of your inputs and outputs (both expected and actual).
@RAJESH1960 Surely that should sound familiar. I know we have had that same discussion previously.
 
Upvote 0
Rich (BB code):
' Split_Address
'
    Set ws1 = Worksheets("MasterData")
'
    With ws1
        ar = .[A1].CurrentRegion    'row number..?
    End With
'
    ReDim Preserve arr(1 To UBound(ar, 1), 1 To 6)
'
    k = 1
    nChar = 30              'Restricts the number of characters in a cell up to total 120 characters, can edit if required in future
'
    For i = 2 To UBound(ar, 1)
        If ar(i, 5) = "" Then GoTo nexti  ' 5 is the full address in column E
        t = Split(ar(i, 5), ",")
        xstr = t(0)
        n = 1
        nChar = 20
'
        For J = 1 To UBound(t)
            t(J) = Trim(t(J))
'
            If t(J) <> "" Then
                If Len(xstr & t(J)) <= nChar Then
                    xstr = xstr & " " & t(J)
                Else
'                   ReDim Preserve arr(1 To 4, 1 To n)
                    arr(k, n) = Trim(xstr)
                    xstr = t(J)
                    n = n + 1
'
                    If n = 4 Then nChar = 100
                End If
            End If
        Next J
'
        If arr(k, n) = "" Then arr(k, n) = Trim(xstr)   'removes special characters and trims to fit 30 characters in each column
nexti:
        k = k + 1
    Next i
'
    ws1.[F2].Resize(UBound(arr, 1), 6) = arr        'destination first cell where data is split
'
    ws1.UsedRange.EntireColumn.AutoFit                                                          '   Set all used columns on sheet wide enough for data
End Sub
This is the working code of the previous application. This time the address is in a different column and the count of columns is also different. So, I tried editing the ar and arr but .... as there are no comments it is very difficult to understand and edit...
 
Upvote 0
04. SPD Import Purchases with masters 27072022.xlsm
EFGHI
1ADDRESSSplit 1Split 2Split 3Split 4
2263 Kamraj Road, Bangalore - 560042263 Kamraj RoadBangalore - 560042
3318/7 Near Ram Mandir, Sethi Chowk, Panipat318/7 Near Ram MandirSethi Chowk Panipat
4UNIT NO 242, 2 ND FLOOR, BUILDING NO 5B, MITTAL ESTATE, ANDHERI(E)UNIT NO 2422 ND FLOORBUILDING NO 5BMITTAL ESTATE ANDHERI(E)
MasterData

Solution was correct. This is the result of the old app.
 
Upvote 0
Previous results don't hold much weight. It would be easier if you post the expected results from the script code in this thread.
 
Upvote 0
Like this -The trim is very important as there are multiple ,, in the characters.
Split address.xlsm
PQRST
1AddressLine1Line2Line3Line4
2PLOT NO 477-78, SECTOR 29 PART 2, ,, HUDA, PANIPATPLOT NO 477-78SECTOR 29 PART 2HUDA, PANIPAT
3Flat No. 2,1 to 2,8, ,, Vikrant 4, RAJENDRA PLACE, Opposite Padam Tower, RAJENDRA PLACEFlat No. 2,1 to 2,8Vikrant 4, RAJENDRA PLACEOpposite Padam TowerRAJENDRA PLACE
4FARM NO. 79A, KH-86, ,, ,, GREEN DRIVE, SILVER OAK FARM, NEW DELHIFARM NO. 79AKH-86, GREEN DRIVESILVER OAK FARMNEW DELHI
MasterData
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,400
Members
449,156
Latest member
LSchleppi

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