Consulting

Results 1 to 4 of 4

Thread: Simple Macro

  1. #1
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location

    Simple Macro

    I know I am getting thicker here but,

    I am looking for a macro that will check bolumn B and if it starts with apostrophe zero then do nothing, if it doesn't start with a apostophe zero then add it.

    Should be straighforward, I have put
    Sub Add_An_Apostrophe()
        ' Add_An_Apostophe Macro
        ' Macro recorded 03/10/2008 by Gerry McNally
        If (Range("B1").Value = "0") Then
            For i = 1 To Cells(Rows.Count, "B").End(xlUp).Row
                Cells(i, "B").Value = "'" & Cells(i, "B").Text
            Next i
        Else
            For i = 1 To Cells(Rows.Count, "B").End(xlUp).Row
                Cells(i, "B").Value = "'0" & Cells(i, "B").Text
            Next i
        End If
    End Sub
    But that adds a zero even if there is one there!!

    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sub AZero()
        Dim r As Range
        For Each r In Range("B1", Cells(Rows.Count, "B").End(xlUp))
            If Not (VarType(r) = vbString And Left(r, 1) = "0") Then
                r = "'0" & r
            End If
        Next r
    End Sub

  4. #4
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Fantastic.

    Thanks Ken

    Thanks MD too - decent link

    Cheers Guys
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

Tags for this Thread

Posting Permissions

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