Consulting

Results 1 to 5 of 5

Thread: Remove extra commas from end of content

  1. #1
    VBAX Regular
    Joined
    Feb 2016
    Posts
    17
    Location

    Remove extra commas from end of content

    Hello everyone,
    I hope this is something simple to fix. My VBA code inserts a formula into Column "D" to combine Columns "C & D" that is separated by a comma. The problem is when the content ends (number of rows varies) commas are inserted in the blank rows until the great beyond.

          Range("D4:D" & .Range("D" & .Rows.Count).End(xlDown).row).Formula = "=(C4&"", ""&B4)"
    Any suggestions on whether to delete the rows or what to insert to stop at end of content?
    Thanks for your help!

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Use xlUp rather than xlDown.

  3. #3
    VBAX Regular
    Joined
    Feb 2016
    Posts
    17
    Location
    My code designates what row to start at then work down. How do I designate an end row when I don't know what it is?

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    .End(xlDown) is down from the indicated cell.
    In your construction, the designated cell is Range("D" & .Rows.Count) i.e D1048576.

    You don't want to go down from that, you want to go up until the last filled cell.
    That would be Range("D" & .Rows.Count).End(xlUp)

    I would write your code

    With Range("D:D")
        Range(.Cells(4, 1), .Cells(.Rows.Count,1).End(xlUp)).Formula = "=SUBSTITUTE(TRIM(C4 & "" "" & B4), "" "" , "", "")"
    End With
    (If you expect no spaces in the cells being concatenated)

  5. #5
    VBAX Regular
    Joined
    Feb 2016
    Posts
    17
    Location
    I tried your code and it only changes rows 3 & 4 and overwrites my column header - using xlUp. I used your code but changed to xlDown it works perfectly, combines both columns and eradicates all the extra commas that was in the rows after the content ended.

    Thanks a lot for all your help!

Posting Permissions

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