+ Reply to Thread
Results 1 to 7 of 7

Worksheet consolidation, VBA. Stop copying column headers

Hybrid View

Lifeseeker Worksheet consolidation, VBA.... 02-20-2012, 05:18 PM
watersev Re: Worksheet consolidation,... 02-20-2012, 05:46 PM
Lifeseeker Re: Worksheet consolidation,... 02-20-2012, 05:55 PM
Lifeseeker Re: Worksheet consolidation,... 02-21-2012, 09:39 AM
Lifeseeker Re: Worksheet consolidation,... 02-21-2012, 10:01 AM
watersev Re: Worksheet consolidation,... 02-21-2012, 10:51 AM
Lifeseeker Re: Worksheet consolidation,... 02-21-2012, 10:56 AM
  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Worksheet consolidation, VBA. Stop copying column headers

    Hi there,

    I took another mate's sheet and applied it to my workbook to play around but ran into one problem.

    The copied the range displayed is the header row of the sub worksheet. I double checked the code, but cannot seem to find where the problem is.

    So if you activate the "master" worksheet, all that gets copied over is the column header.

    Anyone able to assist? I must be missing something.

    Option Explicit
    Private Sub Worksheet_Activate()
    Dim ws As Worksheet, LR As Long
    
    Me.UsedRange.Offset(1).Clear
    
    For Each ws In Worksheets
            LR = ws.Range("A" & Rows.Count).End(xlUp).Row
                ws.Range("A2:F" & LR).Copy
                Me.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
    Next ws
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A2:F" & LR)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
        .FormatConditions(1).Borders(xlTop).Weight = xlThin
        .FormatConditions(1).Borders(xlBottom).Weight = xlThin
        .FormatConditions(1).Interior.ColorIndex = 34
    End With
    
    Columns.AutoFit
    Range("A2").Select
    
    End Sub
    Thanks
    Attached Files Attached Files
    Last edited by Lifeseeker; 02-21-2012 at 10:57 AM. Reason: adding the code in there

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Worksheet consolidation, VBA. Stop copying column headers

    hi there, the problem hides here:

    LR = ws.Range("A" & Rows.Count).End(xlUp).Row
    If the header in A1 is the only value in the column A, LR will be equal to 1 and further down you will end up with copying

    ws.Range("A2:F" & LR).Copy 'the code copies range("A2:F1")

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Worksheet consolidation, VBA. Stop copying column headers

    oh right right....

    great thank you!

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Worksheet consolidation, VBA. Stop copying column headers

    In the code below:
    For Each ws In Worksheets
            LR = ws.Range("A" & Rows.Count).End(xlUp).Row
                ws.Range("A2:F" & LR).Copy
                Me.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
    Next ws
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A2:F" & LR)
    What does the "&" mean in ws.range("A2:F" & LR)? Is it like the word AND?

    Same thing with [code] With Range("A2:F" & LR)?

    Also what does the following code do?
    .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
    thanks
    Last edited by Lifeseeker; 02-21-2012 at 09:48 AM. Reason: few more clarifications needed

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Worksheet consolidation, VBA. Stop copying column headers

    Figured out what the last line do. It is for alternating row colors.

    Still need an answer to my first question though.

    Thanks

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Worksheet consolidation, VBA. Stop copying column headers

    if LR=1

    Range("A2:F" & LR)
    gives Range("A2:F1")

    x = "What" & "is it?"
    gives x = "What is it?"

    & - concatenation operator

    VB help reference extract:

    & Operator

    Used to force string concatenation of two expressions.

    Syntax


    result = expression1 & expression2

    The & operator syntax has these parts:

    Part Description
    result Required; any String or Variant variable.
    expression1 Required; any expression.
    expression2 Required; any expression.

    Remarks

    If an expression is not a string, it is converted to a String variant. The data type of result is String if both expressions are string expressions; otherwise, result is a String variant. If both expressions are Null, result is Null. However, if only one expression is Null, that expression is treated as a zero-length string ("") when concatenated with the other expression. Any expression that is Empty is also treated as a zero-length string.
    Last edited by watersev; 02-21-2012 at 10:55 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Worksheet consolidation, VBA. Stop copying column headers

    Ah I see I see.

    Quite an important code element! I had never understood that until now.

    Thank you

+ 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