473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Select Case worksheet

142 100+
I want to use the Select Case statement in excel VBA to check each worksheet. My code is something like this:
Expand|Select|Wrap|Line Numbers
  1. Sub Template()
  2. n=1
  3. Do While n>0
  4. Select Case Worksheets("Template(n)")
  5. Case "Template(n)"
  6. Worksheets("xxx")=Worksheets("Template(n)")
  7. Msgbox("set to same")
  8. End select
  9. n=n+1
  10. Loop
  11. End Sub
  12.  
Anybody know what is wrong with my code?

thanks
Apr 26 '07 #1
5 1910
SammyB
807 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. Sub Template()
  2.     n = 1
  3.     Do While n > 0
  4.         Select Case Worksheets("Template(n)")
  5.         Case "Template(n)"
  6.             Worksheets("xxx") = Worksheets("Template(n)")
  7.             MsgBox ("set to same")
  8.         End Select
  9.         n = n + 1
  10.     Loop
  11. End Sub
Do While n > 0
This will loop from 1 to infinity: it will take a long time.

Select Case Worksheets("Template(n)")
Case "Template(n)"
This is equivalent to an If clause:
If the worksheet whose name is Template(n) is equal to the string Template(n)
Note the n is just the letter n, it has nothing to do with the loop variable; ie, it will be the letter n for each iteration of the loop.
However, a worksheet is never going to equal a string, so the Select Case will always fail. Fortunately, it will fail with "Object doesn't support this property or method."

Worksheets("xxx") = Worksheets("Template(n)")
Sounds like a nice way to copy a worksheet, but it won't work, you will get "Object doesn't support this property or method."

So, your code does nothing forever. :D What do you want it to do?
Apr 26 '07 #2
Killer42
8,435 Expert 8TB
If you just want to loop through the worksheets, your best bet is probably a For Each loop. You can still throw a Select Case in there if you insist. For example, here's a little macro I just tested (it worked)...
Expand|Select|Wrap|Line Numbers
  1. Sub aaa()
  2. Dim sht As Worksheet
  3. For Each sht In Application.Worksheets
  4.   Select Case sht.Name
  5.     Case "Sheet1"
  6.       sht.Cells(1, 1) = "This is the first sheet"
  7.     Case "Sheet2"
  8.       sht.Cells(1, 1) = "This is the second sheet"
  9.     Case "Sheet3"
  10.       sht.Cells(1, 1) = "This is the third sheet"
  11.   End Select
  12. Next
  13. End Sub
Apr 26 '07 #3
SammyB
807 Expert 512MB
Also, to copy a worksheet, you need to
Expand|Select|Wrap|Line Numbers
  1.  Worksheets("Sheet1").Cells.Copy Sheets("Sheet2").Cells
But, that does not copy any embedded objects like drawings, charts, or controls. To copy them, you must create a new worksheet:
Expand|Select|Wrap|Line Numbers
  1. Worksheets("Sheet1").Copy After:=Worksheets(3)
HTH --Sam
Apr 27 '07 #4
joemo2003
142 100+
Also, to copy a worksheet, you need to
Expand|Select|Wrap|Line Numbers
  1.  Worksheets("Sheet1").Cells.Copy Sheets("Sheet2").Cells
But, that does not copy any embedded objects like drawings, charts, or controls. To copy them, you must create a new worksheet:
Expand|Select|Wrap|Line Numbers
  1. Worksheets("Sheet1").Copy After:=Worksheets(3)
HTH --Sam
i was thinking too complicated at begining, what i want just to see if that sheet exist, so I just use
For each sh in thisworkbook.sheets
if sh.name="xxx" then

thanks anyway.
Apr 30 '07 #5
Killer42
8,435 Expert 8TB
Excellent! :)

Glad to see you've got the result you wanted.
Apr 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dayron | last post by:
Hi, I would like export the records retrieved from database to excel worksheets. unfortunately, each worksheet will support until 65535 records and I have about 1 million of records to write. So I...
3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
2
by: James | last post by:
Hi, I am using the following code to protect the active worksheet: Excel.Worksheet activeWorkSheet = (Excel.Worksheet) excelApp.ActiveWorkbook.ActiveSheet; ...
1
by: madeleine.macphail | last post by:
All I'm currently attempting to move us from a spreadsheet based system to a database system. The first phase is to import the data on a regular basis from the spreadsheets to get the database...
1
by: WWUser | last post by:
I have an Excel workbook where data of a particular type is separated by "header" rows using colons to specify the data type: :DataType1 FirstDataPoint SecondDataPoint ThirdDataPoint...
2
by: Steve Kershaw | last post by:
Hello, I've been trying to spin off an Excel worksheet on the client from an ASP.NET website. I was successfull in displaying an Excel worksheet on the client in a test website (not using IIS,...
0
by: hne | last post by:
Hi friends, I have an application, written in C#, in which I am updating an Excel spreadsheet with data I pull from a sql database. When I try to select a cell in where I want to begin inserting my...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.