+ Reply to Thread
Results 1 to 3 of 3

Text to columns VBA code to change delimiter if the cell value is in array

Hybrid View

dopple Text to columns VBA code to... 11-26-2008, 04:47 AM
dopple I have the array part working... 11-26-2008, 05:38 AM
dopple I've sorted it myself.... 11-26-2008, 09:25 AM
  1. #1
    Registered User
    Join Date
    11-13-2008
    Location
    Dunfermline, Scotland
    Posts
    25

    Text to columns VBA code to change delimiter if the cell value is in array

    Hi all. I'm currently trying to compare all of my companies data sources for staff, ie helpdesk, telephony, ldap etc. and I have made quite a bit of progress, mostly thanks to this forum, however one issue I am running into, is the fact that when I use the text to columns funtion (in a macro) it's splitting up names which are double barrelled into 3 columns. The names are in the format of...
    Jane Doe
    Michal Jackson
    ... so its space delimited.

    I was wondering if there is any way to incldue an exceptions file so that we can say, "If the value in A1 does not appear in the exceptions list then split the name using a space separator. Other wise use a comma.

    We would then have the exceptions text file looking along the lines of
    Pamela,Smyth Hodges
    Anne Marie,Pumpkin
    Billy Bob,Thornton
    My existing code is as follows which is just put straight into the macro by recording the text to columns function.
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlNone, ConsecutiveDelimiter:=True, Tab:=False, Semicolon _
            :=False, Comma:=False, Space:=True, Other:=False, FieldInfo:=Array( _
            Array(1, 2), Array(2, 2), Array(3, 2)), TrailingMinusNumbers:=True
    Last edited by dopple; 11-26-2008 at 09:29 AM. Reason: change title to something more meaningful

  2. #2
    Registered User
    Join Date
    11-13-2008
    Location
    Dunfermline, Scotland
    Posts
    25
    I have the array part working now. Now I just need to check if the value of a cell is in the array.

    I have used the function example from the following site.
    http://www.visualbasic.happycodings....es/code54.html
    Last edited by dopple; 11-26-2008 at 05:39 AM. Reason: added requirements

  3. #3
    Registered User
    Join Date
    11-13-2008
    Location
    Dunfermline, Scotland
    Posts
    25
    I've sorted it myself.
    Here's what I ended up doing. I read in the list of excelptions from a text file into an array and then here's the code.
        
        lb = LBound(asFileContents)
        ub = UBound(asFileContents)
        Range("A1").Select
        'loop through each name to check if it's in the exceptions array
        For i = 2 To rCount Step 1
            'select the cell being changed to make debugging easier.
            Cells(i, 1).Select
            fullname1 = Sheets("Sheet1").Cells(i, 1)
            For x = lb To ub Step 1
                arrayVal = asFileContents(x)
                arrayValRepl = Replace(arrayVal, ",", " ")
                If (fullname1 = arrayValRepl) Then
                    fullname1 = arrayVal
                    splitName = Split(fullname1, ",")
                    Cells(i, 1) = splitName(0)
                    Cells(i, 2) = splitName(1)
                    Exit For
                Else
                    splitName = Split(fullname1)
                    Cells(i, 1) = splitName(0)
                    Cells(i, 2) = splitName(1)
                End If
            Next
        Next

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1