简体   繁体   中英

Macro to copy formatted cells and paste into new sheets

I am using a macro to generate new sheets based on a summary list. In addition to the summary list sheet I have another sheet with formatted cells that I want copied into the new sheets that are automatically being created from the summary list. I tried using the following code but it doesn't seem to work.

Sub createsheets()

Dim data_export As Worksheet
Dim newSheet As Worksheet
Dim MasterForm As Worksheet


Dim r As Integer

r = 2

Do While Sheets("data_export").Cells(r, 1).Value <> ""

Set newSheet = Sheets.Add

newSheet.Name = Sheets("data_export").Cells(r, 1).Value

Sheets("MasterForm").Range("A1:E13").Copy

Sheets("newSheet.Name").Range("A1:E13").Paste

r = r + 1

Loop

End Sub

You are close. Sheets("newSheet.Name").Range("A1:E13").Paste is wrong though. When you put quotes around newSheet.Name VBA only sees the string "newSheet.Name" and you probably don't have a tab with the name "newSheet.Name" because that would be wierd.

You already have a sheet object stored in variable newSheet no need to use the Sheets object again. Just do:

newSheet.Range("A1:E13").Paste

Furthermore, you can copy/paste without having to involve the systems clipboard. Just do:

newSheet.Range("A1:E13").value = Sheets("MasterForm").Range("A1:E13").value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM