refer to active window when more than one window open for a file

M

mcambrose

How do I refer to a specific window when I have a file with multiple windows
open. I originally wrote a macro to operate on a workbook with only one
window open. I used the workbooks("mybook").activate, but now that I have
more than one window open, it always selects the first window. If i am in the
second window when I run the macro, it puts the information in the active
cell in the first window of the workbook. How to I specifically refer to
window where the cursor is. Do I use some type of activewindow command? do i
create an object and set it equal to the current window? Thanks.
 
A

Alan Moseley

Depending upon what you are doing, you probably need to use the
ActiveWorkbook object , the ActiveSheet object, maybe even ActiveCell.
 
J

JLGWhiz

If you know the order in which you opened the windows, you can use the index
number, Windows(3).Activate, or you can use the window name, which would be
the workbook (or file) name, Windows("filename").Activate,
 
M

mcambrose

I found that if I select a cell in the window that I want to operate on, I
can use the command workbooks("mybook"). windows(1).activate (assuming
"mybook" is the name for my workbook") to come back to that same window if I
don't change the selected window in my code. I thought that the index value
would match the order in which the windows were created, but it seems that
the index changes each time a window is selected. For example, if I issue the
command workbooks("mybook"). windows(2).activate, it will toggle to the other
window. If I run the command again it will toggle back to the previous
window. Thus if two windows are open for a workbook, the active window is
window 1 and the other window is window 2. I don't know or care what happens
if more than two windows are open. I just wanted to follow up in case somone
else ran into this question/issue.
 
J

JLGWhiz

Sorry about that, I had workbooks on my mind when I mentioned the opening
order. Windows follow some kind of z-order with the active window always
being windows(1). I believe the z-order runs left to right beginning at the
active window and will wrap if required. For instance if you have WindowB,
WindowA, WindowE and WindowC in that order showing on your status bar and
WindowE is the active window, if you use Windows(4).Activate then WindowA
would become the active window. I think it also only recognizes windows
within the application, i.e. if your active window is in Excel, it would not
activate a window in Word by using the index number. My personal preference
is to use the file name to navigate between workbooks or documents. That is
a lot less complicated. You can always set them to variables if the name
length is a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top