Consulting

Results 1 to 8 of 8

Thread: Need help const a VBA Macro to record 3rd and 4th digits, place in to rght in col B

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Need help const a VBA Macro to record 3rd and 4th digits, place in to rght in col B

    I have numerical data in column A which consits of 4 digits that make up a combination.
    I need to have a macro when ran it records the 3rd and 4th digits of the combinations of column A and copy then to Column B.
    Please can any one help?

    in the axample i am attaching I did the first handful of combonations to show what it should look like.
    Thank you very Much in advance.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    A function that does that.

    Option Explicit
    
    Function Right2(I As Variant) As Variant
        Right2 = CDbl(Right(I, 2))
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I opened the devolper to view code added code and its requested a name of macro I tried everything and I think Im doing sometoing wrong

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Quote Originally Posted by estatefinds View Post
    I opened the devolper to view code added code and its requested a name of macro I tried everything and I think Im doing sometoing wrong

    In the VBE, go to [Tools] and [References] and see if any are #Missing. Sometimes I've had the string functions (i.e. Right() ) cause the 'Missing Function' error

    Either 1) fix reference or 2) restart computer works

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    i did what you said above and it still dosnt work I click run and it asks dfor a macro name.
    What do I name this so it works. Whhen i Do Name is it puts Sub in front of it

    is there a nother way to accomplish this usng VBA?

  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    with the Sub it says"expected sub error"

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Right2() is a function and can be used on a worksheet, or called by a VBA Sub

    If you're looking to do all the numbers at once, try something like the Sub Right2All

    Since the Function only had one line, it was just as easy to include the code in the Sub directly


    Option Explicit
    Function Right2(I As Variant) As Variant
        Right2 = CDbl(Right(I, 2))
    End Function
    
    
    
    Sub Right2All()
        Dim c As Range
        
        For Each c In Range(Range("A20"), Range("A20").End(xlDown)).Cells
            c.Value = CDbl(Right(c.Value, 2))
        Next
    End Sub
    Look at the 2 sheets in the attachment
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    it works great thank you!!!

Posting Permissions

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