ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Using "contains" or "part" in VBA for Wksheets (https://www.excelbanter.com/excel-discussion-misc-queries/55004-using-contains-part-vba-wksheets.html)

newtovba

Using "contains" or "part" in VBA for Wksheets
 
Hi. I am new to VBA and trying to write a code for an excel document. In
it, i have a functioning code however ti is tied to a worksheet name with a
with and find procedure. I'd like to adjust the code so it would have a
contains (if that exists) rather than a constant worksheet name. ie. today
it is "Sheet1" but I would also like it to work for "Sheet1 (2)" without
readjusting.

Any thoughts!!??
Thanks.

Dave Peterson

Using "contains" or "part" in VBA for Wksheets
 
for each wks in activeworkbook.worksheets
if lcase(wks.name) like lcase("sheet1*") then
'do your stuff
else
'don't do it
end if
next wks

You may want to look at InStr in VBA's help, too.

newtovba wrote:

Hi. I am new to VBA and trying to write a code for an excel document. In
it, i have a functioning code however ti is tied to a worksheet name with a
with and find procedure. I'd like to adjust the code so it would have a
contains (if that exists) rather than a constant worksheet name. ie. today
it is "Sheet1" but I would also like it to work for "Sheet1 (2)" without
readjusting.

Any thoughts!!??
Thanks.


--

Dave Peterson

newtovba

Using "contains" or "part" in VBA for Wksheets
 
Hi Dave.
Thank you. Quick question. What is a "wks?". I'm trying the rest out right
now.

"Dave Peterson" wrote:

for each wks in activeworkbook.worksheets
if lcase(wks.name) like lcase("sheet1*") then
'do your stuff
else
'don't do it
end if
next wks

You may want to look at InStr in VBA's help, too.

newtovba wrote:

Hi. I am new to VBA and trying to write a code for an excel document. In
it, i have a functioning code however ti is tied to a worksheet name with a
with and find procedure. I'd like to adjust the code so it would have a
contains (if that exists) rather than a constant worksheet name. ie. today
it is "Sheet1" but I would also like it to work for "Sheet1 (2)" without
readjusting.

Any thoughts!!??
Thanks.


--

Dave Peterson


Dave Peterson

Using "contains" or "part" in VBA for Wksheets
 
I should have included:

dim wks as worksheet

at the top of the code.

I wanted to use a variable that could represent a worksheet so that I could loop
through all the worksheets.

Sorry,

newtovba wrote:

Hi Dave.
Thank you. Quick question. What is a "wks?". I'm trying the rest out right
now.

"Dave Peterson" wrote:

for each wks in activeworkbook.worksheets
if lcase(wks.name) like lcase("sheet1*") then
'do your stuff
else
'don't do it
end if
next wks

You may want to look at InStr in VBA's help, too.

newtovba wrote:

Hi. I am new to VBA and trying to write a code for an excel document. In
it, i have a functioning code however ti is tied to a worksheet name with a
with and find procedure. I'd like to adjust the code so it would have a
contains (if that exists) rather than a constant worksheet name. ie. today
it is "Sheet1" but I would also like it to work for "Sheet1 (2)" without
readjusting.

Any thoughts!!??
Thanks.


--

Dave Peterson


--

Dave Peterson

newtovba

Using "contains" or "part" in VBA for Wksheets
 
Dave,
I ended up using "activesheet" rather than specify a worksheet. Working like
a charm.
Thanks.

"Dave Peterson" wrote:

for each wks in activeworkbook.worksheets
if lcase(wks.name) like lcase("sheet1*") then
'do your stuff
else
'don't do it
end if
next wks

You may want to look at InStr in VBA's help, too.

newtovba wrote:

Hi. I am new to VBA and trying to write a code for an excel document. In
it, i have a functioning code however ti is tied to a worksheet name with a
with and find procedure. I'd like to adjust the code so it would have a
contains (if that exists) rather than a constant worksheet name. ie. today
it is "Sheet1" but I would also like it to work for "Sheet1 (2)" without
readjusting.

Any thoughts!!??
Thanks.


--

Dave Peterson



All times are GMT +1. The time now is 01:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com