Question: This question was asked earlier and it was wrong.... The file Cities.xlsx contains an AllCities sheet that list all cities where a company has offices.
This question was asked earlier and it was wrong.... The file Cities.xlsx contains an AllCities sheet that list all cities where a company has offices. Write a sub that does the following: (1) For each city in the list, it checks whether there is a worksheet with the name of that city in the workbook, and if there isnt one, it adds one; and (2) it deletes any city worksheet if the worksheets name is not the current AllCities list. The sub should be written so that it can be run at any time and will always respond with the current list of cities in AllCities sheet. (Note: Your sub should also work if the AllCities list contains exactly one city or no cities)
Hint you better consider the nested for loop. For example, for adding worksheets that exist in the list, your outer loop better uses 'for' loop to go through each city in the list and the inner loop uses 'for each' loop to visit each worksheet to compare the city name (from the list) and the worksheet name. If the city name doesn't exist in any worksheet, you add a new worksheet with the city name. For deleting worksheets that don't exist in the list, your outer loop better uses 'for each' loop to go through each worksheet name and the inner loop uses 'for' loop to go through the city names in the list. If the worksheet name doesn't exist in the list, the worksheet is deleted
| Cities where a company does business |
| Santa Fe |
| Phoenix |
| Kansas City |
| Denver |
| Sacramento |
| Seattle |
| San Diego |
| Salt Lake City |
| Las Vegas Sheets are: Allcities Dallas Denver Salt Lake City San Digeo This is what I have so far: Sub sbDeleteDallas() Sheets("Dallas").Delete End Sub Sub sbDeleteASheet() Sheet1.Delete End Sub Sub AddWorksheet() Worksheets.Add().Name = "Mysheet" End Sub Sub CreateSheet() With ThisWorkbook .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Dallas1" End With End Sub
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
