Solved

Copy range into clipboard and paste it above rows with bold cell entries

Posted on 2012-03-28
2
305 Views
Last Modified: 2012-03-29
Dear Experts:

I would like to copy and paste a selected range as follows:

This range (B2:G2) on the active worksheet is selected manually.

The macro is to copy the selection into the clipboard and paste it
above each row that has bold cell entries.

I have attached a sample file with detailed explanations for your convenience.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Inserting-Headers-Before-Rows-Co.xlsm
0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37779647
Here's your code which starts at row 4, then looks for Bold cells in column B, pasting the header on row 2 one row above the found bold row.

Option Explicit

Sub pasteHeaders()
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim r As Range
Dim rCopyRange As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    Set rCopyRange = wks.Range("B2:G2")
    
    Set rng = wks.Range("B4", wks.Range("B" & wks.Rows.Count).End(xlUp))
    
    For Each r In rng
        If r.Value <> vbNullString Then
            If r.Font.Bold = True Then
                rCopyRange.Copy r.Offset(-1, 0)
            End If
        End If
    Next r
End Sub

Open in new window


PS - if you anticipate a wider header range and want to use SELECTION to dictate the header, just change line 13 to:

Set rCopyRange = Selection 'as opposed to wks.Range("B2:G2")

Cheers,

Dave

See attached.

Enjoy!

Dave
Inserting-Headers-Before-Rows-Co.xlsm
0
 

Author Closing Comment

by:AndreasHermle
ID: 37782819
Hi Dave,

great! Works like a charm.

Thank you very much for your swift and professional help.

Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now