I am trying to copy and paste the same worksheet (Overview) of a file (Connection Overview) to a new workbook and reiterate this action every time that a value in the worksheet "Overview" (value contained in the cell "Code") changes in accordance to a list of values (List) - changing therefore the output of the sheet "Overview".
In the end I would like to have the new workbook to be composed by sheets named by each code imputed in the cell "Code" and each sheet will therefore be a copy of the "Overview" worksheet with different numbers (depending on the code).
I was using this, but I clearly get stuck in the iteration:
Sub CopyItOver()
x = 1
For Each Lista In Range("List")
Worksheets("Overview").Range("Code") = Lista
Calculate
Set NewBook = Workbooks.Add
Workbooks("Connection Overview.xlsm").Worksheets("Overview").Copy
NewBook.Sheets(“Sheet(x)”).Paste
x=x+1
Next
End Sub
When you copy a worksheet with no destination, the copy is created in a new blank workbook. All that remains is to take control of the new ActiveWorkbook
and save it.
Right-click the worksheet's name tab and choose View Code . When the VBE opens, paste the following into the worksheet code pane titled something like Book1 - Sheet1 (Code) .
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Code")) Is Nothing Then
On Error GoTo bm_Safe_Exit
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim fn As String, rng As Range
fn = ThisWorkbook.Path & Chr(92) & "OVRVW_" & Format(Now, "yyyymmdd_hhmmss")
Set rng = ThisWorkbook.Names("List").RefersToRange
If Not IsError(Application.Match(Target.Value2, rng, 0)) Then
Target.Parent.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=fn, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close SaveChanges:=False
End If
End If
bm_Safe_Exit:
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Tap Alt + Q to return to the worksheet. Typing any of the values from the named List range should generate a new workbook.
The Workbook.SaveAs method uses the supplied filename and the original workbook's path. The new workbook is saved as n .XLSX type workbook.
Range("Code")
would be a single cell on the worksheet in question but there was no indication as to where Range("List")
was so I defined its range from workbook scope.
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.