简体   繁体   中英

Copy and paste to each new worksheet with formatting and formulas

I need your assistance on VBA code to Copy Paste to each new worksheet with formatting and formulas using the range method. Here are 2 variations of what I have. One copies and pastes with format but pastes as values and the other retained the formula but not the format. Your assistance is greatly appreciated.

  1. This keeps the format but paste as value

     rngHeader.Copy Destination:=.Range("A4") res.Copy Destination:=.Range("A5") 
  2. This keeps formula but loses the formatting

     .Range("A4").Resize(, rngHeader.Columns.Count).Formula = rngHeader.Formula .Range("A5").Resize(res.Rows.Count, res.Columns.Count).Formula = res.Formula 

If you want to paste it all, try:

res.Copy
.Range("A4").PasteSpecial xlPasteAll '~~> paste to destination

Edit:1 Based on comments, if it is a non-contiguous cell then you'll have to loop or use brute force.

  1. Brute Force

     With Sheet1 '~~> change to your actual sheet res.Copy .Range("A4").Select '~~> you have to select the destination .Paste '~~> use Sheet.Paste method End With 
  2. Looping

     Dim c As Range, i As Long: i = 0 With Sheet1 For Each c In res c.Copy .Range("A4").Offset(i).PasteSpecial xlPasteAll i = i + 1 Next End With 

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