Consulting

Results 1 to 8 of 8

Thread: Solved: add - when merge data on cell of column

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Question Solved: add - when merge data on cell of column

    I need a help , I have this code .Now this code will merge all of data on column G to column M1 and my problem when i merge data on column M1 i want to add - between each data . that mean i want :

    data1-dat2-data3

    Now this code make

    data1data2data2




    PHP Code:
     
    Dim v 
    As Variant
    Dim s 
    As String
    Dim i 
    As Long

    ' Fetch from sheet
    v = Range("G1:G2000").Value
    Concatenate into a single string
    ""
    For LBound(v1To UBound(v1)
        
    v(i1)
    Next i
    ' Put back on sheet
    Range("M1").Value = s
    End With 

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]


    Dim v As Variant

    ' Fetch from sheet
    v = Application.Transpose(Range("G1:G2000").Value)
    ' Concatenate into a single string and put back on sheet
    Range("M1").Value = Join(v, "-")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you very much for your help but when i run your code add - to empty cell also

    PHP Code:
    -------------------------------------------------11039409-11039247----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    I want just add - to filed with data

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim v As Variant

    ' Fetch from sheet
    v = Application.Transpose(Range("G1:G2000").Value)
    ' Concatenate into a single string and put back on sheet
    With Range("M1")

    .Value = Join(v, "-")
    Do While InStr(.Value, "--") > 0

    .Value = Replace(.Value, "--", "-")
    Loop
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you again , just i have a question . can you add a code to delete the first - and also the latest - ? now after run your code my data will be like this :

    PHP Code:
     
    -11039409-11039247-112373809

    and for 1 cell show me : -11039193-


    Please help me for this also .

  6. #6
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    I did

    PHP Code:
     
    Option Explicit
    Sub RemoveSquareBrackets
    ()
    Dim c As Range
    Application
    .ScreenUpdating False
    For Each c In Range("G1"Range("G" Rows.Count).End(xlUp))
    If 
    Left(c1) = "-" Then c Mid(c2Len(c) - 1)
    If 
    Right(c1) = "-" Then c Left(cLen(c) - 1)
    Next c
    Application
    .ScreenUpdating True
    End Sub 
    THank you .

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    There is no need to use that loop

    [vba]

    Dim v As Variant

    ' Fetch from sheet
    v = Application.Transpose(Range("G1:G2000").Value)
    ' Concatenate into a single string and put back on sheet
    With Range("M1")

    .Value = Join(v, "-")
    Do While InStr(.Value, "--") > 0

    .Value = Replace(.Value, "--", "-")
    Loop
    .Value = Replace("-" & .Value & "-", "--", "")
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you so much

Posting Permissions

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