Importing only the first 3 characters

Blank

New Member
Joined
Jun 19, 2009
Messages
14
Hi everyone,

I'm stuck trying to import only the first 3 characters from a .csv file. Right now the code does import that certain column like in the picture below but in fact i want to import only the first 3 characters from those postal codes. After importing, i would like to know how many times is repeating, showing me that in the next column, and finally, in the third column to paste all postal codes without duplicates.


Screenshot 2021-02-10 115327.jpg


This is the code i am using it right now:

VBA Code:
Sub ImportCSV()

    Dim ChrArray    As Variant
    Dim Data()      As Byte
    Dim FileFilter  As String
    Dim FileName    As Variant
    Dim n           As Long
    Dim Rng         As Range
    Dim Text        As String
    
        Set Rng = Range("B2:B2")
        
        FileFilter = "Comma Separated Values (*.csv),*.csv"
        FileName = Application.GetOpenFilename(FileFilter)
        
        If FileName = False Then Exit Sub
        
            Open FileName For Binary Access Read As #1
                ReDim Data(LOF(1))
                Get #1, , Data
            Close #1
            
            Text = StrConv(Data, vbUnicode)
            Text = Replace(Text, vbCrLf, ",")
            ChrArray = Split(Text, ",")

                For n = 29 To UBound(ChrArray) - 1 Step 32
                    Rng.Cells(1, 0) = ChrArray(n)
                    Set Rng = Rng.Offset(1, 0)
                Next n
                
End Sub

I'll be very greatful to anyone who can help me with this since my boss needs it ASAP :/

Regards,
Alex
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try changing this,
VBA Code:
Rng.Cells(1, 0) = ChrArray(n)
to this.
VBA Code:
Rng.Cells(1, 0) = Left(ChrArray(n), 3)
 
Upvote 0
Try changing this,
VBA Code:
Rng.Cells(1, 0) = ChrArray(n)
to this.
VBA Code:
Rng.Cells(1, 0) = Left(ChrArray(n), 3)
Works like a charm. Thank you very much Norie.

Any ideas if i can use the same button to show me in the next column how many times the partially postal codes repeats?
 
Upvote 0
You could add a COUNTIF formula.

It's not clear which column that should go in - your code appears to be putting the postcodes in column B but in the image you posted they appear to be in column A.

Anyway, adjust B here to suit - the formula will refer to the column to the left.
VBA Code:
Range("B2:B" & rng.Row-1).FormulaR1C1 = "=COUNTIF(R2C1:R" & rng.Row-1 & "C1, RC[-1])}"
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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