Excel macro/VBA question

MassSpecGuru

Board Regular
Joined
Feb 23, 2010
Messages
55
I am designing a workbook that takes laboratory instrument data and after some minor manipulations, summarizes the results on a specific worksheet within the workbook. The overall process is fairly simple/straightforward (parsing, simple calculations, etc.) and I think I have what I need for the most part; however, I have a small problem I was wondering if somebody could help me with.

Since this is such a repetitive task, I thought I would try my hand at my first macro and placing some buttons in my worksheets. I have no problems following the code generated using Excel’s Macro Recorder, however there is a problem with the fact that the instrument data file name is not the same each time I want to process some data. Thus, I thought I’d also try my hand at adding a little bit of VBA code to the original macro code to make things work better.

The code (see below) all works fine except that now I cannot close the file the instrument data was copied from. I have tried two techniques to solve this issue, but have been unsuccessful so far and would like some input.

My first attempt (see Plan A below the “End Sub” code), was to call the data file explicitly and close it, however, when I substitute those two lines of code for the code between the ' ---------- remarks, I get a:

Run-time error ‘9’:
Subscript out of range

message and Excel kicks me into the Debugger, where apparently there is some problem with the

Windows(fn).Activate

command even though the Immediate window shows the correct file was chosen:

Selected file: C:\Validation Data\Instrument A\Results_122209A.rdb.txt

So, I tried resorting to the “tried-and-true” method of switching between open windows using the Alt-Tab keyboard trick. This is outlined in Plan B below.

This doesn’t work either. Instead, it closes the summary workbook -- but it does it without saving the changes, so that’s progress, right?

Can anybody suggest a way around this simple problem?

(Sorry for the brevity of this post, however, after typing my verbose message for almost an hour online, the original post disappeared and I am very frustrated to say the least!)

Thanks in advance,
MSG



Sub OpenOneFile01()
Dim fn As Variant
fn = Application.GetOpenFilename("Analyst data,*.txt", _
1, "Select A File To Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
' the user didn't select a file
Debug.Print "Selected file: " & fn
Workbooks.Open fn
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Range("A1:Z400").Select
Selection.Copy
Windows("Method Validation Template.xls").Activate
Sheets("Analyst data 01").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
' ----------
Windows("Results_122209A.rdb.xls").Activate
ActiveWindow.Close
' ----------
Sheets("Data summary").Select
Range("A1").Select
End Sub


Plan A:
Windows(fn).Activate
ActiveWindow.Close SaveChanges:=False

Plan B:
SendKeys "%{TAB}", True
ActiveWindow.Close SaveChanges:=False
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Thanks for the quick response, Lynn, but it didn't work. The macro still stops at:

Windows(fn).Activate

and kicks me into debugger.

Besides, I don't want to close the entire workbook (I could already do that); however, I only want to close the "input" file, e.g. Results_122209A.rdb.txt.

Thanks.
 
Upvote 0
fn = Application.GetOpenFilename("Analyst data,*.txt", _
1, "Select A File To Open", , False)

GetOpenFilename returns the full path and filename c:\directory\filename
so fn =path\filename

Windows(path\filename) is not valid, that only wants the filename..

Once the book is opened, you don't refer to the path anymore..because it's already open and loaded into memory.

Try this

Set MyBook = Workbooks.Open fn
...rest of your code...
MyBook.Close False
 
Upvote 0
No problems, Lynn. I appreciate any and all responses. Someday I hope to be able to return the favor.

I do have another question, though, and I hope it's not too dumb:

Is there a difference between buttons created with View -> Toolbars -> Forms and View -> Toolbars -> Control Toolbox?

I created the buttons I am trying to run my code from via the Forms menu, not Control Toolbox.

BTW, I'm using Excel 2003. Sorry. I probably should have mentioned that in my original post.

MSG
 
Upvote 0
I'm not sure if it's something I did incorrectly, however, when I placed the "Set MyBook.Open fn line right after the "Debug.Print ..." line, I got a "Compile error: Expected: end of statement" message.

Thanks



GetOpenFilename returns the full path and filename c:\directory\filename
so fn =path\filename

Windows(path\filename) is not valid, that only wants the filename..

Once the book is opened, you don't refer to the path anymore..because it's already open and loaded into memory.

Try this

Set MyBook = Workbooks.Open fn
...rest of your code...
MyBook.Close False
 
Upvote 0
fn = Application.GetOpenFilename("Analyst data,*.txt", _
1, "Select A File To Open", , False)
Set MyBook = Workbooks.Open fn
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,706
Members
449,118
Latest member
MichealRed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top