1

I want to reference to multiple sheets in a workbook and then copy those to another workbook with vba. This is all progress of a bigger Macro I build. The problem I have is, that I cannot reference the sheets of the for loop with their name. May be you guys can help. This is an extract of my code, all names are referenced at the top of my file (like sh As worksheet, and wb as workbook) What my macro should do, is check sheets if they should be send to a recipient, and some of those sheets (like the code below) merged into one workbook and then send this workbook. But the problem I have is, how to delete/overwrite a specific sheet in the destination workbook. (Line 5)

For Each sh In ThisWorkbook.Worksheets
    If sh.Range("A1").Value Like "?*@?*.?*" And sh.Name <> "A" Or sh.Name <> "B" Or sh.Name <> "C" Then
         Workbooks.Open Filename:= _
         "Path" & "Name" & ".xlsx", UpdateLinks:=0
        Sheets(chr(34)&sh.name&chr(34)).Delete
        Set wb = ActiveWorkbook
        sh.Copy Before:=wb.Worksheets(1)
        Set wb = ActiveWorkbook
        sh.Copy Before:=wb.Worksheets(1)
        Set sc = wb.Worksheets(chr(34)&sh.name&chr(34))
        With sc.UsedRange
        .Value = .Value
        Rows("244:310").Select
        Selection.EntireRow.Hidden = True
        ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
        Cells("B1").Select
        ActiveWindow.ScrollRow = 1
        End With

        Filename = "Name"

        Set wb = ActiveWorkbook
        Set OutMail = OutApp.CreateItem(0)
        Set OMail = OutApp.CreateItem(0)

        With wb
            .SaveAs FilePath & Filename & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OMail
            .Display
            End With
                signature = OMail.HTMLbody
            With OMail
                .to = sh.Range("A1").Value
                .CC = sh.Range("A3").Value
                .BCC = ""
                .Subject = Text             
                .Attachments.Add.wb.FullName                                       

            End With

            On Error GoTo 0

1 Answer 1

2

Since sheets inherently have a numeric representation, you can go between the sheets of a single workbook with a loop, such as:

Dim i as long
For i = 1 to sheets.count
    'do something using Sheets(i)
Next i

Regarding deletion of a sheets, you can use the above inclusive of the .delete:

Dim i as long
For i = 1 to sheets.count
    If Sheets(i).Range("A1").Value Like "?*@?*.?*" And Sheets(i).Name <> "A" Or Sheets(i).Name <> "B" Or Sheets(i).Name <> "C" Then  Sheets(i).Delete  'note that this is in-line
Next i

Additionally, if you don't want to use a loop, or simply want to look at the current sheet, you could use the below for the deletion:

ActiveSheet.Delete

I would recommend recording the sheet name as a string, before using in your if statement, purely for speed/efficiency (probably not noticeable for small bits of code, but decent practice), similar to:

Dim shName as String
shName = ActiveSheet.Name
If Sheets(shName).Range("A1").Value Like "?*@?*.?*" And shName <> "A" Or shName <> "B" Or shName <> "C" Then

This should allow VBA to utilize the already stored string (allocated memory) rather than going back to the name every time the call is made for that .name to be found.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Cyril for the detailled.answer. The problem is, that I do not want to delete the sheet in the source workbook, but in the destination workbook where a last month version of the sheet is stored. So I want to transfer multiple sheets from WB A to WB B. WB B already has the sheets from last month and some other important sheets that can not be deletet. What I want to do is to overwrite Sheets A B and C in WB B with the new Sheets A B and C from WB A. Since there is no overwrite possibility (as far as I know), I have to delete Sheets A B and C in WB B first, then copy from WB A.
My plan was to reference the sheet names by their name to delete the correct sheets in WB B and not by their sheet number. Is there any way to make this work by referencing their real worksheet name?
Okay I got it. Your answer helped me! Thanks a lot @Cyril. Didn't really read your first sentence. Have a nice christmas!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.