+ Reply to Thread
Results 1 to 6 of 6

Move cell based on contents (VBA)

Hybrid View

BaronDJB Move cell based on contents... 09-02-2010, 10:26 AM
sweep Re: Move cell based on... 09-02-2010, 11:24 AM
BaronDJB Re: Move cell based on... 09-02-2010, 11:36 AM
ghynes Re: Move cell based on... 12-21-2014, 10:37 AM
Pepe Le Mokko Re: Move cell based on... 12-21-2014, 10:42 AM
ghynes Re: Move cell based on... 12-21-2014, 11:24 AM
  1. #1
    Registered User
    Join Date
    08-25-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Move cell based on contents (VBA)

    Hi All,

    I'm trying to use VBA to move a cell one column to the right if it contains a comma, below is the code I'm using.

    My problem is that the code only looks to see if the whole cell value is a comma - I need it to work even if there is other text in the cell with the comma; I tried putting an asterisk either side of the comma in the code but it flagged an error...

    Any tips would be appreciated, I'm probably missing something really basic!


    Sub MoveComma()
        Dim myrange, cell As Range
        Set myrange = ActiveSheet.Range("K1", Range("K65536").End(xlUp))
        For Each cell In myrange
            If cell.Value = "," Then
                cell.Cut
                Range("K" & Application.WorksheetFunction.Max(2, cell.Row)).Offset(0, 1).Select
                ActiveSheet.Paste
            End If
        Next cell
    End Sub
    Last edited by BaronDJB; 09-02-2010 at 11:33 AM. Reason: Solved and inserted code tags

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Move cell based on contents (VBA)

    Hi,

    Please edit your post to include code tags.

    Sub MoveComma()
    Dim myrange, cell As Range
    Set myrange = ActiveSheet.Range("K1", Range("K65536").End(xlUp))
    For Each cell In myrange
    If InStr(cell.Value, ",") > 0 Then
    cell.Offset(0, 1).Value = cell.Value
    cell.ClearContents
    End If
    Next cell
    End Sub
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    08-25-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Move cell based on contents (VBA)

    Many thanks for your help, being new to VBA and learning on the hoof isn't the best combination!

  4. #4
    Registered User
    Join Date
    08-24-2005
    Location
    Ireland
    MS-Off Ver
    O365
    Posts
    75

    Re: Move cell based on contents (VBA)

    hello. i am trying to use this code, but using : instead of ,.. but its not recognizing the : because its reading the cell as text format. anyone help?

  5. #5
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,622

    Re: Move cell based on contents (VBA)

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

  6. #6
    Registered User
    Join Date
    08-24-2005
    Location
    Ireland
    MS-Off Ver
    O365
    Posts
    75

    Re: Move cell based on contents (VBA)

    ok. understood. 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