+ Reply to Thread
Results 1 to 3 of 3

If text string BEGINS with 5 spaces then shift cell to the right

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    If text string BEGINS with 5 spaces then shift cell to the right

    Hi everyone,

    Have real trouble with this one. Column B has a few thousand cells which contains numbers, letters and other characters. Some of these strings begin with 5 spaces, others begin with 10 spaces.

    ie. ' SomethingSomethingSomething
    ie.' Darkside

    What I'm trying to do is write a macro which will go through column B, find any cells with strings that BEGIN with 5 spaces and move them over to the right 1 column. If the string BEGINS with 10 spaces, then move it over to the right 2 columns.

    Any ideas?

    Thanks!!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,941

    Re: If text string BEGINS with 5 spaces then shift cell to the right

    Try:

    Option Explicit
    
    Sub MoveCells()
    
    Dim lLR As Long, i As Long
    lLR = Range("B" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For i = 2 To lLR
        If Left(Range("B" & i).Value, 10) = Space(10) Then
            Range("B" & i).Cut Range("B" & i).Offset(, 2)
        ElseIf Left(Range("B" & i).Value, 5) = Space(5) Then
            Range("B" & i).Cut Range("B" & i).Offset(, 1)
        End If
    Next 'i
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by TMS; 03-14-2017 at 09:41 AM. Reason: Fix type A => B
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,941

    Re: If text string BEGINS with 5 spaces then shift cell to the right

    A bit neater:

    Option Explicit
    
    Sub MoveCells()
    
    Dim lLR As Long, i As Long
    lLR = Range("B" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For i = 2 To lLR
        With Range("B" & i)
            If Left(.Value, 10) = Space(10) Then
                .Cut .Offset(, 2)
            ElseIf Left(.Value, 5) = Space(5) Then
                .Cut .Offset(, 1)
            End If
        End With
    Next 'i
    Application.ScreenUpdating = True
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 06-30-2015, 10:28 AM
  2. [SOLVED] Add spaces between numbers and text in string
    By c3po in forum Excel General
    Replies: 5
    Last Post: 09-16-2012, 07:30 PM
  3. Check if a string begins with substring and then change text
    By booo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2010, 03:39 PM
  4. Add 3 spaces before last word in string of text
    By HMM in forum Excel General
    Replies: 7
    Last Post: 11-09-2010, 10:24 AM
  5. insert spaces into text string
    By mmwltd in forum Excel General
    Replies: 6
    Last Post: 04-27-2010, 01:05 PM
  6. Adding spaces between certain text string
    By g_hadgraft in forum Excel General
    Replies: 1
    Last Post: 06-16-2009, 07:50 AM
  7. Text string with unwanted spaces
    By bigskyhy in forum Excel General
    Replies: 2
    Last Post: 11-13-2008, 08:57 PM

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