Copy Paste Macro

chet645

Board Regular
Joined
Nov 10, 2010
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I created a macro that opens a workbook, copies information from that workbook, then pastes the information into a "destination" workbook. The problem is, conditional formatting exists in the workbook where the information is being copied, and when the information is pasted into the destination workbook, an external link is created.

Is there a way to paste values and formats without the conditional formatting also being pasted. I don't want to create an external link every time I run the macro?

VBA Copy / Paste Code:

ActiveWindow.ActivateNext
Application.DisplayAlerts = False
Selection.PasteSpecial Paste:=xlValues
Selection.PasteSpecial Paste:=xlFormats

Any help would be much appreciated.

Kindest Regards,

Chet
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I don't think you can pastespecial formats without the conditional formatting because as it says it is formatting but if you want to still keep the formatting without the rules you can try something like the below which removes the rules afterwards (but if that doesn't do what you want then we will probably have to resort to API calls).


Rich (BB code):
    Dim myCell As Range

    Application.DisplayAlerts = False
    Selection.PasteSpecial Paste:=xlValues
    Selection.PasteSpecial Paste:=xlFormats
    For Each myCell In Selection
        With myCell
            .Font.FontStyle = .DisplayFormat.Font.FontStyle
            .Interior.Color = .DisplayFormat.Interior.Color
            .Font.Strikethrough = .DisplayFormat.Font.Strikethrough
            .Interior.Pattern = .DisplayFormat.Interior.Pattern
            '.Borders.LineStyle = .DisplayFormat.Borders.LineStyle
        End With
    Next myCell
    Selection.FormatConditions.Delete
    Application.DisplayAlerts = True
 
Last edited:
Upvote 0
You can either delete this line of code
Code:
Selection.PasteSpecial Paste:=xlFormats

or delete your CFs from the source sheet and just copy the regular formatting, if desired.
 
Last edited:
Upvote 0
Hi Mark, great idea. I'm a novice VBA user, but when I scroll through macro step by step, it gets caught in a circle and can't get out. The circle happens here:

For Each myCell In Selection
With myCell
.Font.FontStyle = .DisplayFormat.Font.FontStyle
.Interior.Color = .DisplayFormat.Interior.Color
.Font.Strikethrough = .DisplayFormat.Font.Strikethrough
.Interior.Pattern = .DisplayFormat.Interior.Pattern
'.Borders.LineStyle = .DisplayFormat.Borders.LineStyle
End With
Next myCell
 
Upvote 0
The macro selects and entire worksheet in a target workbook, then pastes it into another workbook for consolidation purposes.
 
Upvote 0
would it be helpful if I posted the entire macro?
 
Upvote 0
If the entire sheet in the consolidation workbook is selected then it will loop through all 17,179,869,184 cells won't it?

Try the below which at least restricts it to only the cells with data (as long as your data starts in A1).


Code:
   [color=darkblue]Dim[/color] myCell [color=darkblue]As[/color] Range, lr [color=darkblue]As[/color] [color=darkblue]Long[/color], lc [color=darkblue]As[/color] [color=darkblue]Long[/color]

    Application.DisplayAlerts = [color=darkblue]False[/color]
    Selection.PasteSpecial Paste:=xlValues
    Selection.PasteSpecial Paste:=xlFormats

    lc = ActiveSheet.Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious, [color=darkblue]False[/color]).Column
    lr = ActiveSheet.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious, [color=darkblue]False[/color]).Row

    [color=darkblue]For[/color] [color=darkblue]Each[/color] myCell [color=darkblue]In[/color] Range(Cells(1, 1), Cells(lr, lc))
        [color=darkblue]With[/color] myCell
            .Font.FontStyle = .DisplayFormat.Font.FontStyle
            .Interior.Color = .DisplayFormat.Interior.Color
            .Font.Strikethrough = .DisplayFormat.Font.Strikethrough
            .Interior.Pattern = .DisplayFormat.Interior.Pattern
            [color=green]'.Borders.LineStyle = .DisplayFormat.Borders.LineStyle[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Next[/color] myCell

    Selection.FormatConditions.Delete
    Application.DisplayAlerts = [color=darkblue]True[/color]
[
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top