• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Macro to copy table only values and formatting, not formulas|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Macro to copy table only values and formatting, not formulas|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosMacro to copy table only values and…
sp_PrintTopic sp_TopicIcon
Macro to copy table only values and formatting, not formulas
Avatar
Mark Milan
Member
Members
Level 0
Forum Posts: 24
Member Since:
March 17, 2020
sp_UserOfflineSmall Offline
1
June 2, 2022 - 3:25 am
sp_Permalink sp_Print sp_EditHistory

Hello everybody,

 

I hope you can help me.

I need to create a macro to copy a table from one sheet and paste it to another sheet, including the values and the format, the width of columns (but not the formulas).

The sheet is protected and the table has dynamic rows. That's why I've preferred not to define the range as number of rows.

The macro listed here below works but it copies and pastes the table, also included the formulas...

 

thnks for your support

Mark

 

*************************************************************

'Name macro
Sub CopyFilteredTable()

'Dimension variables and declare datatypes
Dim rng As Range
Dim WS As Worksheet

Set WS = ThisWorkbook.Sheets("Table1")
ThisWorkbook.Sheets("Table1").Unprotect Password:="XXXXXX"

'Go through rows in Table1
For Each Row In Range("Table1").Rows

'Check if row is visible
If Row.EntireRow.Hidden = False Then

'The SET statement allows you to save an object reference to a variable, the image above demonstrates a macro that assigns a range reference to a range object.
If rng Is Nothing Then Set rng = Row

'Returns the union of two or more ranges.
Set rng = Union(Row, rng)
End If

'Continue with next row
Next Row

'Create a new worksheet
Set WS = Sheets.Add

'Copy rng to cell A1 in worksheet WS
rng.Copy Destination:=WS.Range("A1")
ThisWorkbook.Sheets("Table1").Protect Password:="XXXXXXXX"
End Sub

 

**************************************************************************************

Avatar
Jessica Stewart
Northern USA
Member
Members


Trusted Members
Level 0
Forum Posts: 202
Member Since:
February 13, 2021
sp_UserOfflineSmall Offline
2
June 2, 2022 - 7:36 am
sp_Permalink sp_Print

Here (rng.Copy Destination:=WS.Range("A1")), instead of copying directly to the range do a paste special. Unfortunately, I don't know how to paste it all in one go, that would take research. But with PasteSpecial you can paste both column widths and just the value copied; though, I'm not sure how to do it in one line of code except a workaround of:

rng.Copy
WS.Range("A1").PasteSpecial xlPasteColumnWidths
rng.Copy
WS.Range("A1").PasteSpecial xlPasteValues

Hope that gets some ideas flowing for you. 🙂

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Jeff Krueger, Shanna Henseler
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27237

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.