+ Reply to Thread
Results 1 to 5 of 5

String manipulation with exceptions

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2006
    Posts
    58

    String manipulation with exceptions

    I have a macro that loops through a column of text and removes all text after a hyphen. Example: AU9929-PK becomes AU9929. I need to set up some exceptions that when it processes certain text it does not truncate it. Example: AU9929-ASST stays as AU9929-ASST. So far there are twelve exceptions but this could increase.

    Note: The list which includes shop number, description, sku, sku description, quantity, unit price and more is sorted in a particular order as other operations are performed on it that requires it be in a specific order.
    Last edited by Apel; 07-23-2009 at 02:13 PM.

  2. #2
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481

    Re: String manipulation with exceptions

    Post the loop that you are currently using to truncate after the hyphen and we can give suggestions to modify your existing code to accomodate your exceptions?

  3. #3
    Registered User
    Join Date
    05-31-2006
    Posts
    58

    Re: String manipulation with exceptions

    Here is the code.

    Private Sub RemoveSkuIndexes()
    
    ' *********************************************************************
    ' Removes all indexes leaving base Sku
    ' *********************************************************************
    
    Dim LastRow As Integer
    Dim Pos As Integer
    Dim SKU As String
    Dim TrimSKU as String
        
        ' Find lastrow in worksheet
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        ' Cylce through each cell in column A until lastrow
        For Each i In Range("A2:A" & LastRow)
            
            SKU = i.Value
            
            ' Returns position of first hyphen from the left
            Pos = InStr(1, SKU, "-", vbTextCompare)
            
            ' Returns string to the left of the hyphen
            If Pos > 0 Then
                TrimSKU = Left(Text, Pos - 1)
            Else
                TrimSKU = SKU
            End If
            
            ' Overwrites cell content with truncated text
            i.Value = TrimSKU
        
        Next
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481

    Re: String manipulation with exceptions

    In this If statement you can test for your exceptions and then NOT trim your SKU accordingly.

            If Pos > 0 Then
              If SKU <> "AU9929-ASST" then
                TrimSKU = Left(Text, Pos - 1)
              End If
            Else
    You will need to add a check for each of your exceptions.

    If SKU <> "AU9929-ASST" or SKU <> "Exception2" or SKU <> "Exception3" Then
    which can get cumbersome as your number of excpetions increases. You may want to switch to using a Seclect Case statement to make this a little easier to manage as your exceptions change, etc.

    HTH

  5. #5
    Registered User
    Join Date
    05-31-2006
    Posts
    58

    Re: String manipulation with exceptions

    Ahhh. Select case. I had forgotten about that guy. I knew there was something better than a 300 line if statement but I couldn't think of it. Thanks.

+ 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