Hi,
In our VBA code we used Sheets("xxx") to refer to the data in the sheets in
the workbook which has the code. This worked well even if we open several
other workbooks thru that code. But suddenly, when we upgraded to Excel 2007,
this code no longer worked. Then we refered using ThisWorkbook.sheets("xxxx").
Can anybody know why "ThisWorkbook" keyword is not required in Excel 2003
VBA to refer to the sheets in the same workbook, whereas it is required in
Excel 2007 to refer to the sheets ?
Thanks,
Krishna
|
|
0
|
|
|
|
Reply
|
Utf
|
2/26/2010 11:53:01 AM |
|
If you use sheets("xxx") in a routine in a general module, then this sheet will
refer to the activeworkbook. It doesn't always refer to the workbook that owns
the code.
If you use sheets("xxx") in the ThisWorkbook module, then it will refer to the
object owning the code--ThisWorkbook.
I've never seen a difference in the way xl2003 and xl2007 works. I'd bet you're
mistaken. But since you didn't actually share a small snippet of code (and
where it was located), it's difficult to test.
Personally, I think you should qualify your objects--no matter where the code is
located or what version of excel you're using.
Jayashree Krishna wrote:
>
> Hi,
>
> In our VBA code we used Sheets("xxx") to refer to the data in the sheets in
> the workbook which has the code. This worked well even if we open several
> other workbooks thru that code. But suddenly, when we upgraded to Excel 2007,
> this code no longer worked. Then we refered using ThisWorkbook.sheets("xxxx").
>
> Can anybody know why "ThisWorkbook" keyword is not required in Excel 2003
> VBA to refer to the sheets in the same workbook, whereas it is required in
> Excel 2007 to refer to the sheets ?
> Thanks,
> Krishna
--
Dave Peterson
|
|
0
|
|
|
|
Reply
|
Dave
|
2/26/2010 1:17:30 PM
|
|
It's not required in Office 2003 either, but I'd have to say that it's highly
recommended. I suspect what's happening is that while the code is running,
"ThisWorkbook" is no longer the active workbook and it's trying to find the
sheets you reference in the Active Workbook. Let me give an example.
If your active workbook name is "WB1.name" then this line
Sheets("xxx")
References the sheet named "xxx" in WB1.
If your activeworkbook name is "WB2.name", this line references sheet "xxx"
in WB2.
If your code reads
ThisWorkbook.Sheets("xxx"), there is no doubt which workbook is being
referenced.
--
HTH,
Barb Reinhardt
"Jayashree Krishna" wrote:
> Hi,
>
> In our VBA code we used Sheets("xxx") to refer to the data in the sheets in
> the workbook which has the code. This worked well even if we open several
> other workbooks thru that code. But suddenly, when we upgraded to Excel 2007,
> this code no longer worked. Then we refered using ThisWorkbook.sheets("xxxx").
>
> Can anybody know why "ThisWorkbook" keyword is not required in Excel 2003
> VBA to refer to the sheets in the same workbook, whereas it is required in
> Excel 2007 to refer to the sheets ?
> Thanks,
> Krishna
|
|
0
|
|
|
|
Reply
|
Utf
|
2/26/2010 1:19:02 PM
|
|
"ThisWorkbook" refers to the Workbook containing the code. Excel VBA
supports implicit qualification to the ActiveWorkbook and ActiveSheet etc if
not otherwise qualified. If your code workbook is not active you must
qualify with a reference to the workbook you are working on, in this case
the code workbook.
Your code workbook might be referred to in any of these ways.
ThisWorkbook
Application.Workbooks("myBook.xls")
and if it is active -
ActiveWorkbook
Nothing has changed in this respect from Excel 2003 to 2007
(It is possible to rename "ThisWorkbook")
Regards,
Peter T
"Jayashree Krishna" <JayashreeKrishna@discussions.microsoft.com> wrote in
message news:82B7C937-056E-4C18-B0FC-E3E7AE27D6D4@microsoft.com...
> Hi,
>
> In our VBA code we used Sheets("xxx") to refer to the data in the sheets
> in
> the workbook which has the code. This worked well even if we open several
> other workbooks thru that code. But suddenly, when we upgraded to Excel
> 2007,
> this code no longer worked. Then we refered using
> ThisWorkbook.sheets("xxxx").
>
> Can anybody know why "ThisWorkbook" keyword is not required in Excel 2003
> VBA to refer to the sheets in the same workbook, whereas it is required in
> Excel 2007 to refer to the sheets ?
> Thanks,
> Krishna
|
|
0
|
|
|
|
Reply
|
Peter
|
2/26/2010 1:25:48 PM
|
|
Thanks to everyone for your insight.
Regards,
Krishna
"Peter T" wrote:
> "ThisWorkbook" refers to the Workbook containing the code. Excel VBA
> supports implicit qualification to the ActiveWorkbook and ActiveSheet etc if
> not otherwise qualified. If your code workbook is not active you must
> qualify with a reference to the workbook you are working on, in this case
> the code workbook.
>
> Your code workbook might be referred to in any of these ways.
>
> ThisWorkbook
> Application.Workbooks("myBook.xls")
> and if it is active -
> ActiveWorkbook
>
> Nothing has changed in this respect from Excel 2003 to 2007
>
> (It is possible to rename "ThisWorkbook")
>
> Regards,
> Peter T
>
>
> "Jayashree Krishna" <JayashreeKrishna@discussions.microsoft.com> wrote in
> message news:82B7C937-056E-4C18-B0FC-E3E7AE27D6D4@microsoft.com...
> > Hi,
> >
> > In our VBA code we used Sheets("xxx") to refer to the data in the sheets
> > in
> > the workbook which has the code. This worked well even if we open several
> > other workbooks thru that code. But suddenly, when we upgraded to Excel
> > 2007,
> > this code no longer worked. Then we refered using
> > ThisWorkbook.sheets("xxxx").
> >
> > Can anybody know why "ThisWorkbook" keyword is not required in Excel 2003
> > VBA to refer to the sheets in the same workbook, whereas it is required in
> > Excel 2007 to refer to the sheets ?
> > Thanks,
> > Krishna
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
3/4/2010 7:09:01 AM
|
|
|
4 Replies
277 Views
(page loaded in 0.15 seconds)
Similiar Articles: VBA: Paste in a range - microsoft.public.excel.programming ...>> > >> > with thisworkbook >> > .worksheets("sheet1").cells(2+i,11).copy ... How to Copy & Paste Using VBA | eHow.com VBA (Visual Basic for Applications) is the language ... how to move sheets from one workbook to another - microsoft.public ...I need a vba code to MOVE the COPY of sheets from input.xls To thisworkbook . "VBA code For Moving The Copy oF sheeets" ... ThisWorkbook.RefreshAll Issue/Question - microsoft.public.excel ...ThisWorkbook.RefreshAll Issue/Question - Excel VBA Help ... Microsoft Excel Forum Members Post questions in this forum if they are related to using Microsoft Excel, this ... VBA to open a PDF, print, then close - Stumped - microsoft.public ...... Dim FName As String Dim bk As Workbook Dim strPath As String strPath = ThisWorkbook ... VBA to open a PDF, print, then close - Stumped - microsoft.public ... Hi all, I ... Open VBA Editor? - microsoft.public.excel.programmingVisual Basic for Applications (VBA) is the programming ... the button is ... ... to do this but to install the code open the VBA editor (Alt/F11) and double click ThisWorkbook ... SumProduct in VBA - Allow the user to select the file - microsoft ...... user to select the source file, and probably open the file (sumproduct in VBA ... SUMPRODUCT(--(" & engid & "=" _ & engid2 & "),--(" & rev & "))") ThisWorkbook ... Create Hyperlink in VBA - microsoft.public.excel.programming ...... ActiveCell If .Hyperlinks.Count > 0 Then ThisWorkbook ... creating hyperlink through vba Visual Basic for Applications (VBA) > ... in the user friendly ... Determine who last modified an Excel file???? - microsoft.public ...My question is, is there any VBA code to determine who was the last user to ... i usually just create a hidden sheet. then in the thisworkbook before close module, add ... Return All Sheets to Cell A1 before save - microsoft.public.excel ...Darryl Place this macro in the ThisWorkbook module. Note that you have to have 2 ... > > How can I do this with vba code. > > Thanks > > Darryl > > VBA to find filename and replace - microsoft.public.excel ...Hi I just found the answer to "Find and Replace in VBA" and tried to adapt it ... TargetFile As String > Dim Link As Hyperlink > > Set aWorkbook = ThisWorkbook ... ThisWorkbook Property [Excel 2003 VBA Language Reference]Returns a Workbook object that represents the workbook where the current macro code is running. Read-only. Remarks. Use this property to refer to the workbook that ... Code Modules And Code Nameswill continue to work, because VBA still knows that worksheet by its code name of Sheet1. You can change the code name of either the ThisWorkbook or a Sheet object. VBA for Excel workbooks adn WindowsThisWorkbook. ThisWorkbook is the workbook within which your VBA procedure runs. So if you write: ThisWorkbook.Save The workbook within which your VBA procedure (macro) runs ... VBA - ThisWorkbook Excel - Excel Discussion List Tuesday, May 29 ...Hi, In our VBA code we used Sheets(xxx) to refer to the data in the sheets in the workbook which has the code. This worked well even if we open several o Answer : VBA - ThisWorkbook - GNT : your source for software ...VBA - ThisWorkbook - answer - Hi, In our VBA code we used Sheets("xxx") to refer to the data in the sheets in the workbook which has the code. This worked well even ... 7/23/2012 10:37:54 PM
|