+ Reply to Thread
Results 1 to 11 of 11

Concatenate only where there is data

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Smile Concatenate only where there is data

    Hi All,
    I created a macro (mostly by copying/pasting macros from other posts and tweeking) that:
    1. Inserts a column C
    2. Concatenates A+B into the new column C,
    3. Pastes the values
    4. Shades every other block of new data within column C, and
    5. Deletes the column C that was created.

    The issue that I am having is the fact I don't know how to limit all of the above to only rows that have data in column B. In other words, the macro is doing this for 65,000 lines! How could I limit the above to only rows with data in column B?

    Sub group_and_shade()
    '
    ' group_and_shade Macro
    '
    
    '
    Dim Cll As Range
        Dim Group1Color As Long
        Dim Group2Color As Long
        Dim Group As Integer
        
        Group1Color = -4142 'No Fill
        Group2Color = 15    'Gray
        Group = 1
    
        Range("C1:C65000").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],RC[-1])"
        Selection.Copy
        Range("C1:C65000").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        For Each Cll In Range("C2:C" & Range("C:C").SpecialCells(xlCellTypeLastCell).Row)
            If Cll.Offset(-1, 0).Value <> Cll.Value Then
                Group = Group * -1
            End If
            If Group > 0 Then
                Cll.EntireRow.Interior.ColorIndex = Group1Color
            Else
                Cll.EntireRow.Interior.ColorIndex = Group2Color
            End If
        Next Cll
        Selection.Delete Shift:=xlToLeft
    End Sub
    As always, thank you kindly for any assistance you are able to offer.
    Bryan
    Last edited by tiger10012; 10-05-2011 at 03:57 PM.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Concatenate only where there is data

      Dim Cll As Range
      Dim Group1Color As Long
      Dim Group2Color As Long
      Dim Group As Integer
      Dim lrColB As Long
      
      lrColB = Range("B" & Cells.Count).End(xlUp).Row
      
      Group1Color = -4142 'No Fill
      Group2Color = 15    'Gray
      Group = 1
      
      Range("C1:C" & lrColB).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
      'snip...

  3. #3
    Registered User
    Join Date
    06-17-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Concatenate only where there is data

    Thank you very much for the information Kenneth. When I ran the updated macro, I received an "Overflow" message. There were 2 places where I incorporated the "& lrColB". Did I mess up my updates? Here is the updated code:
     Sub group_and_shade()
    '
    ' group_and_shade Macro
    '
    
    '
    Dim Cll As Range
        Dim Group1Color As Long
        Dim Group2Color As Long
        Dim Group As Integer
        Dim lrColB As Long
        
        lrColB = Range("B" & Cells.Count).End(x1Up).Row
        
        Group1Color = -4142 'No Fill
        Group2Color = 15    'Gray
        Group = 1
    
        Range("C1:C" & lrColB).Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],RC[-1])"
        Selection.Copy
        Range("C1:C" & lrColB).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        For Each Cll In Range("C2:C" & Range("C:C").SpecialCells(xlCellTypeLastCell).Row)
            If Cll.Offset(-1, 0).Value <> Cll.Value Then
                Group = Group * -1
            End If
            If Group > 0 Then
                Cll.EntireRow.Interior.ColorIndex = Group1Color
            Else
                Cll.EntireRow.Interior.ColorIndex = Group2Color
            End If
        Next Cll
        Selection.Delete Shift:=xlToLeft
    End Sub

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Concatenate only where there is data

    Sorry, forgot the rows part.

        lrColB = Range("B" & Cells.Rows.Count).End(xlUp).Row
    I don't use the SpecialCells method because it only updates after a save. Use a method like mine.

  5. #5
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Concatenate only where there is data

    Quote Originally Posted by Kenneth Hobson View Post
    I don't use the SpecialCells method because it only updates after a save. Use a method like mine.
    Oops. Didn't read fully before posting. I didn't realize that specialcells use the last saved values. Thanks.

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Concatenate only where there is data

    Use xLup rather than xOneUp.
    Last edited by Kenneth Hobson; 10-05-2011 at 03:40 PM.

  7. #7
    Registered User
    Join Date
    06-17-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Concatenate only where there is data

    I get a "400" error code when I run the macro now. I have trimmed the code to this, and still get the "400" error code:
     Sub group_and_shade()
    '
    ' group_and_shade Macro
    '
    
    '
    Dim lrColB As Long
        
        lrColB = Range("B" & Cells.Rows.Count).End(x1Up).Row
        
        Range("C1:C" & lrColB).Select
        Selection.Copy
        
    End Sub

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Concatenate only where there is data

    Diet coke withdrawal I guess:
        lrColB = Range("B" & Rows.Count).End(xlUp).Row
    Last edited by Kenneth Hobson; 10-05-2011 at 03:39 PM.

  9. #9
    Registered User
    Join Date
    06-17-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Concatenate only where there is data

    Still getting the "400" error message

  10. #10
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Concatenate only where there is data

    I like to use this method:
    Activesheet.UsedRange.SpecialCells(xlLastCell).Row
    Or
    Range("B:B").SpecialCells(xlLastCell).Row

  11. #11
    Registered User
    Join Date
    06-17-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: Concatenate only where there is data

    There were 2 things going on:
    1. My blindness, and
    2. The error code 400 will appear if there is no data on the spreadsheet. When I was troubleshooting, I kept moving components of code to new spreadsheets. Didn't think to take the data as well.

    Thank you very much everyone!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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