简体   繁体   中英

Copy a range of cells and not the whole sheet

How can I copy only the range of cells from A1 to J47 to a new worksheet, and not the whole sheet? Thank you

Sub Salva()
Dim X As String 
X = "Foglio salvato n° " & Range("G3").Value
Sheets(1).Copy 
ActiveWorkbook.SaveAs Filename:="C:\Users\wc074\Documents\archivi\" & X & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWindow.Close
End Sub

Please, try the next code:

Sub Salva()
  Dim X As String, sh As Worksheet, newWb As Workbook, rng As Range
  
   Set sh = ActiveSheet
   X = "Foglio salvato n° " & sh.Range("G3").value
   
   Set rng = sh.Range("A1:J47")
   Set newWb = Workbooks.Add
   rng.Copy newWb.Sheets(1).Range("A1")
   newWb.Sheets(1).UsedRange.EntireColumn.AutoFit
   newWb.saveas FileName:="C:\Users\wc074\Documents\archivi\" & X & ".xls", FileFormat:=xlExcel8 'Excel 97-2003 Workbook
   newWb.Close False
End Sub

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