Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Selecting Sheet1 by index rather than sheet name

I have a program to automatically transfer data from an older version to an
updated file The Macro runs from the new file. Right now I have the user
enter the old filename and first sheet name by input boxes. This works well
but I would like VBA to detect the old file that would be already open and
put the name into a variable. The transfer always starts from Sheet1 of the
old file no matter what the name of the sheet has been changed to.

This works but not what I want:
OLDFILENAME = Inputbox("ENTER OLD FILE NAME")
FIRSTSHEETNAME = Inputbox("ENTER NAME OF FIRST SHEET IN OLD FILE")
Windows(OLDFILENAME).Activate
Sheets("FIRSTSHEETNAME").Select

This is what I would like

<macro to find other open workbook
OLDFILENAME = <detected workbook
Windows(OLDFILENAME).Activate
Sheet1.Select <-- This gives an error msg

or

Windows(OLDFILENAME).Activate
Sheet1.Activate <-- This activates Sheet1
of the new workbook
that the macro runs in

Thank you in advance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Selecting Sheet1 by index rather than sheet name

Hi,

Assuming that you have two workbooks open.
You can refer to the FIRST workbook OPENED as WorkBooks(1) and the other as
WorkBooks(2)

The first sheet of a workbook can always be referenced as Sheets(1)

Hope this helps,
Regards,
Don

"Rich J" wrote in message
...
I have a program to automatically transfer data from an older version to an
updated file The Macro runs from the new file. Right now I have the user
enter the old filename and first sheet name by input boxes. This works
well
but I would like VBA to detect the old file that would be already open and
put the name into a variable. The transfer always starts from Sheet1 of
the
old file no matter what the name of the sheet has been changed to.

This works but not what I want:
OLDFILENAME = Inputbox("ENTER OLD FILE NAME")
FIRSTSHEETNAME = Inputbox("ENTER NAME OF FIRST SHEET IN OLD FILE")
Windows(OLDFILENAME).Activate
Sheets("FIRSTSHEETNAME").Select

This is what I would like

<macro to find other open workbook
OLDFILENAME = <detected workbook
Windows(OLDFILENAME).Activate
Sheet1.Select <-- This gives an error msg

or

Windows(OLDFILENAME).Activate
Sheet1.Activate <-- This activates Sheet1
of the new workbook
that the macro runs in

Thank you in advance



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Selecting Sheet1 by index rather than sheet name

Thanks Don, That worked great for selecting the workbook. Apparently as new
sheets are added to the workbook the index of the newest becomes (1). I need
to figure out a routine to scroll thru the sheets and detect the name of the
first sheet. They always begin with 1 - so I will test for the 1 and then
a space after it.
This is the code I wrote for picking the workbook and it was very simple
once you gave me the correct notation. Which most times is so simple but
just not aware of.

NEWFILE = ActiveWorkbook.Name <- where macro is run from
TEMP = Workbooks(1).Name
If TEMP = NEWFILE Then
OLDFILE = Workbooks(2).Name
Else
OLDFILE = Workbooks(1).Name
End If



"Don Lloyd" wrote:

Hi,

Assuming that you have two workbooks open.
You can refer to the FIRST workbook OPENED as WorkBooks(1) and the other as
WorkBooks(2)

The first sheet of a workbook can always be referenced as Sheets(1)

Hope this helps,
Regards,
Don

"Rich J" wrote in message
...
I have a program to automatically transfer data from an older version to an
updated file The Macro runs from the new file. Right now I have the user
enter the old filename and first sheet name by input boxes. This works
well
but I would like VBA to detect the old file that would be already open and
put the name into a variable. The transfer always starts from Sheet1 of
the
old file no matter what the name of the sheet has been changed to.

This works but not what I want:
OLDFILENAME = Inputbox("ENTER OLD FILE NAME")
FIRSTSHEETNAME = Inputbox("ENTER NAME OF FIRST SHEET IN OLD FILE")
Windows(OLDFILENAME).Activate
Sheets("FIRSTSHEETNAME").Select

This is what I would like

<macro to find other open workbook
OLDFILENAME = <detected workbook
Windows(OLDFILENAME).Activate
Sheet1.Select <-- This gives an error msg

or

Windows(OLDFILENAME).Activate
Sheet1.Activate <-- This activates Sheet1
of the new workbook
that the macro runs in

Thank you in advance




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying 'sheet1'!A1 to sheet 2, I want to get 'sheet2'A1, not 'she Dany Excel Discussion (Misc queries) 5 April 21st 07 11:14 PM
Selecting chartobject without index no. Jaylin Charts and Charting in Excel 4 August 24th 06 04:31 PM
transfer data from sheet1 to sheet 2 joe311 Excel Worksheet Functions 5 April 13th 06 01:49 AM
Selecting a cell from an index Canuck[_2_] Excel Programming 1 May 16th 04 09:07 PM
selecting a cell in sheet1 paul mueller Excel Programming 1 November 18th 03 09:15 PM


All times are GMT +1. The time now is 05:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"