For each sheet in workbook not doing anything in the last sheet

Alroj

New Member
Joined
Jan 12, 2016
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hi Community,

I have this macro that creates sheets for the names included in a List located in sheet within the same workbook. Once the sheets are created the macro runs a procedure for each of them. For simplification purposes, I've changed the process and included the word "hello" to be written by the macro in each sheet created.
The macro does the job in each page BUT nothing is done in the last name included in the List. There is no error message that tells me what I should do.

I am hoping that you may be able to assist and check if I have missed something in the macro and improving it so it does the job across each of the names in the List, please

VBA Code:
Sub AAACreateSheetsAndIncludeTheWordHello()

  Dim c As Range
  Dim Ws As Worksheet
  
  With Sheets("FundList")
    For Each c In .Range("A2", .Range("A" & Rows.Count).End(3))
      If c.Value <> "" Then
        Sheets.Add After:=Sheets(.Name)
         ActiveSheet.Name = c.Value
      End If
    Next
  End With
  
  For Each Ws In ThisWorkbook.Worksheets
  If Ws.Name <> "ABC" And Ws.Name <> "BCD" And Ws.Name <> "FundList" Then
  Range("a1").Value = "Hello"
    Ws.Activate
    Debug.Print Ws.Name
    
  End If
  Next
  
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You need to specify the sheet to write to, otherwise it writes to the currently active sheet.

Change
VBA Code:
Range("a1").Value = "Hello"
to
VBA Code:
Ws.Range("a1").Value = "Hello"
 
Upvote 0
Solution
This works, brilliant! Thank you very much Myall_blues!
 
Upvote 0
You're problem has been solved but would it not be easier to do what needed to be done while your added sheet is active?
Code:
Sheets.Add After:=Sheets(.Name)
         With ActiveSheet
             .Name = c.Value
             .Range("A1").Value = "Hello"
         End With
      End If
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,686
Members
452,994
Latest member
Janick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top