+ Reply to Thread
Results 1 to 5 of 5

Copy and buid a variable with a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    2

    Copy and buid a variable with a Macro

    Hi Guys,

    i've try to found a solution to this problem but unsuccessful.

    I need to create a Macro that copy a variable value and copu in a cell.
    From this Cell, i need to delete some parts and create a new Variable.

    This is:

    Insert a Value like a text in A1, ex:

    11 22 33 44 55 66 77 88

    Now i need to delete the fourth and the eighth pair of text and copy the new value in a new cell (A2 for example).

    So the final value will be "11 22 33 55 66 77".

    "11 22 33 44 55 66 77 88" must be a variable, so i can have

    22 22 33 44 55 55 66 77 -> 22 22 33 55 55 66

    or

    AA BB CC DD 11 22 33 44 -> AA BB CC 11 22 33

    I've try to make a macro but when i change the variable, i get ever the first value !

    Can you help me ?

    Thanks !!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Copy and buid a variable with a Macro

    Here's one method.

    Sub Remove_Some_Pairs()
        
        Dim strPairs As String, v As Variant, i As Long
        
        'Source
        strPairs = Range("A1").Text
        
        v = Split(strPairs)
        strPairs = v(0)
        For i = 1 To UBound(v)
            Select Case i + 1
                Case 4, 8   '<---Pairs to remove
                Case Else:  strPairs = strPairs & " " & v(i)
            End Select
        Next i
        
        'Destination
        Range("A2").Value = strPairs
                
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    09-28-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    2

    Re: Copy and buid a variable with a Macro

    Thank you very much mate

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Copy and buid a variable with a Macro

    Here is mine (not very impressive, but seems to work)

    Sub macro()
    Dim x
    Dim res As String
    
    x = Split(Range("A1").Value, " ")
    
    x(3) = ""
    x(7) = ""
    res = Join(x, " ")
    Range("A2").Value = res
    End Sub

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Copy and buid a variable with a Macro

    Quote Originally Posted by Sbarro79 View Post
    Here is mine (not very impressive, but seems to work)

    Sub macro()
    Dim x
    Dim res As String
    
    x = Split(Range("A1").Value, " ")
    
    x(3) = ""
    x(7) = ""
    res = Join(x, " ")
    Range("A2").Value = res
    End Sub
    Better than mine. Well done.

+ 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. Macro to copy and paste a variable range
    By richard_a in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-24-2013, 06:26 AM
  2. Macro to buid Pivot Table with Dynamic range
    By andyb16 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-11-2012, 06:02 AM
  3. Macro – If Equal to Variable, Copy and move to Next
    By melissadianna in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-12-2011, 02:50 PM
  4. How to specify the variable end of the range to copy in a macro.
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-28-2010, 04:56 PM
  5. Copy variable length column to clipboard within a macro
    By DanBlum in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-21-2009, 09:15 AM

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