+ Reply to Thread
Results 1 to 8 of 8

Code correction in replacing dash sign while concatnet

Hybrid View

mukeshbaviskar Code correction in replacing... 02-18-2014, 02:16 AM
:) Sixthsense :) Re: Code correction in... 02-18-2014, 02:33 AM
HaHoBe Re: Code correction in... 02-18-2014, 02:43 AM
mukeshbaviskar Re: Code correction in... 02-18-2014, 10:26 AM
mukeshbaviskar Re: Code correction in... 02-18-2014, 10:27 AM
mukeshbaviskar Re: Code correction in... 02-18-2014, 10:24 AM
HaHoBe Re: Code correction in... 02-18-2014, 10:32 AM
mukeshbaviskar Re: Code correction in... 02-18-2014, 11:44 AM
  1. #1
    Forum Contributor
    Join Date
    12-15-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    672

    Code correction in replacing dash sign while concatnet

    Hi friends, I want to replace the dash sign to blank while concatenate. If I enter dash sign either in column 'c, d, e, or f' then the dash sign should be replace with blank '""' in output in column 'g'.

    Now I'm getting syntax error. please suggest me a correction.

    Code is:

    Option explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
     If Target.Row < 7 Then Exit Sub
                    Application.EnableEvents = False
                    If Target.Row > 1 And Not Range(Cells(Target.Row, "c"), Cells(Target.Row, "f")) Is Nothing Then
        Set rng = Range(Cells(Target.Row, "c"), Cells(Target.Row, "f"))
        Replace.value = ("-"), ("")
        With rng
            Cells(Target.Row, 7).Value = .Cells(1) & " " & .Cells(2) & " " & .Cells(3) & " " & .Cells(4)
        End With
    End If
                    Application.EnableEvents = True
    End Sub
    Thanking you in anticipation.

    Mukesh

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Code correction in replacing dash sign while concatnet

    Try this...

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Row < 7 Then Exit Sub
    Dim rng As Range, r As Range, vTemp As Variant
    
    Application.EnableEvents = False
    
    If Target.Row > 1 And Not Range(Cells(Target.Row, "c"), Cells(Target.Row, "f")) Is Nothing Then
        Set rng = Range(Cells(Target.Row, "c"), Cells(Target.Row, "f"))
        For Each r In rng
            vTemp = vTemp & r.Value & " "
        Next r
        vTemp = Replace(Trim(vTemp), "-", " ")
        With rng
            Cells(Target.Row, 7).Value = vTemp
        End With
    End If
    
    Application.EnableEvents = True
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code correction in replacing dash sign while concatnet

    Hi, Mukesh,

    if you check the row to be greater or equal to 7 in order to proceed with the code there is no need to check again later if it will be greater 1 - this must be fulfilled. Maybe try

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Row < 7 Then Exit Sub
    Dim r As Range, vTemp As Variant
    
    Application.EnableEvents = False
    
    For Each r In Range(Cells(Target.Row, "c"), Cells(Target.Row, "f"))
        vTemp = vTemp & r.Value & " "
    Next r
    vTemp = Replace(Trim(vTemp), "-", " ")
    Cells(Target.Row, 7).Value = vTemp
    
    Application.EnableEvents = True
    
    End Sub
    This should work fine if you are only handling one row at a time.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Forum Contributor
    Join Date
    12-15-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    672

    Re: Code correction in replacing dash sign while concatnet

    Hi Sixthsense ,
    Excellent! It's working fine.

    Thank you for solving problem.

    Mukesh

  5. #5
    Forum Contributor
    Join Date
    12-15-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    672

    Re: Code correction in replacing dash sign while concatnet

    Hi Sixthsense ,
    Excellent! It's working fine.

    Thank you for solving problem.

    Mukesh

  6. #6
    Forum Contributor
    Join Date
    12-15-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    672

    Re: Code correction in replacing dash sign while concatnet

    Hello Holger,
    Excellent! It's working fine.

    Thank you for solving my problem.

    I want to use this code in my another file where the concatenate sequence is little different as follows:

    Title 'C First name 'E Surname 'D columns. Please tell me how to change it.

    Thank you once again for your guidance.

    Mukesh

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code correction in replacing dash sign while concatnet

    Hi, Mukesh,

    like this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Row < 7 Then Exit Sub
    Dim vTemp As Variant
    Dim var As Variant
    Dim lngArr As Long
    
    var = Array("C", "E", "D")
    
    Application.EnableEvents = False
    
    For lngArr = LBound(var) To UBound(var)
      vTemp = vTemp & Cells(Target.Row, var(lngArr)).Value & " "
    Next lngArr
    vTemp = Replace(Trim(vTemp), "-", " ")
    Cells(Target.Row, 7).Value = vTemp
    
    Application.EnableEvents = True
    
    End Sub
    Ciao,
    Holger

  8. #8
    Forum Contributor
    Join Date
    12-15-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    672

    Re: Code correction in replacing dash sign while concatnet

    Hi Holger,
    Excellent! Thank you.

    Mukesh

+ 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. [SOLVED] Ignore dash sign in formula subtotal
    By mukeshbaviskar in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-27-2014, 10:16 AM
  2. [SOLVED] Ignore dash sign while concatenate
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2013, 07:05 AM
  3. [SOLVED] code to insert dash sign
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-06-2013, 11:01 PM
  4. I see a dash, Excel sees a minus sign.
    By vishorizons in forum Excel General
    Replies: 3
    Last Post: 11-09-2009, 11:02 AM
  5. Replacing dash in item number
    By tmcleod in forum Excel General
    Replies: 3
    Last Post: 03-28-2006, 07:15 PM

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