简体   繁体   中英

Use Excel VBA to copy a worksheet into a new workbook and make changes to the new workbook

Apologies in advance if this is a duplicated post - I searched for quite a while and couldn't find anything to solve my issue. I have an excel spreadsheet that I would like to duplicate into a new workbook, and then perform some functionality on the new workbook to remove various portions of the copied content while leaving the original unaltered.

I have the code working that will copy the sheet into a new workbook and save it, which is shown below:

Dim strFileName As String

'Copy sheet as a new workbook
Sheets("Sheet1").Copy

'SaveAs
strFileName = Application.GetSaveAsFilename(ActiveWorkbook.Name) & "xlsx"
If strFileName = "False" Then Exit Sub 'User Canceled
ActiveWorkbook.SaveAs Filename:=strFileName

My issue is in where to put the code that I'd like to use after the new workbook is created, and whether I need to put more code specifying which workbook or worksheet I'd like to perform the functionality in (maybe a .Activate or a .open?). Below is what code I've written to remove what data I don't want, which works fine if I run it inside the original worksheet rather than the duplicated copy:

'removes rows when criteria is met
x = 4
Do While Sheet1.Cells(x, 1).Value <> ""
    If (Sheet1.Cells(x, 1) = "Prospect") Or (Sheet1.Cells(x, 1) = "Lead") Then
    Sheet1.Rows(x).Delete
    Else
    x = x + 1
    End If
Loop

'removes rows when criteria is met
y = 4
Do While Sheet1.Cells(y, 5).Value <> ""
    If (Sheet1.Cells(y, 5) = "Non-exclusive") Then
    Sheet1.Rows(y).Delete
    Else
    y = y + 1
    End If
Loop

'removes columns
Sheet1.Columns("P:S").Delete
Sheet1.Columns("F").Delete

So in short how do I get this code working on the sheet I just created? Any help would be very much appreciated, and if I did not clarify something please feel free to ask!

Set a reference to the sheet once you've copied it, and use that reference in your code:

Dim wb as Workbook, sht as WorkSheet
Dim strFileName As String

'Copy sheet as a new workbook
ActiveWorkbook.Sheets("Sheet1").Copy

Set wb = ActiveWorkbook
Set sht = wb.Sheets(1)

'SaveAs
strFileName = Application.GetSaveAsFilename(wb.Name) & ".xlsx"
If strFileName = "False" Then Exit Sub 'User Canceled
wb.SaveAs Filename:=strFileName

'removes rows when criteria is met
x = 4
Do While sht.Cells(x, 1).Value <> ""
    If sht.Cells(x, 1) = "Prospect" Or sht.Cells(x, 1) = "Lead" Then
    sht.Rows(x).Delete
    Else
    x = x + 1
    End If
Loop

'removes rows when criteria is met
y = 4
Do While sht.Cells(y, 5).Value <> ""
    If sht.Cells(y, 5) = "Non-exclusive" Then
    sht.Rows(y).Delete
    Else
    y = y + 1
    End If
Loop

'removes columns
sht.Columns("P:S").Delete
sht.Columns("F").Delete

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