Solved

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

Posted on 2012-03-28
2
301 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Conditional format Excel Cell 3 17
Excel 2010 - Text not aligning at bottom of cell! 5 16
Merging multiple rows to one 22 33
Excel 3 22
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

708 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

19 Experts available now in Live!

Get 1:1 Help Now