Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open File Using Variable Name

Status
Not open for further replies.

dar149

MIS
Mar 5, 2002
117
US
I am opening a file using a variable name. That part works, but once it's open the subprocedure is exited without running the rest of the code. Following is what I have so far..

Sub Compare()
Dim Comp As String
Dim compval As String

Comp = InputBox("Name of Comp File ", "Comp")
'TEST is the result of the input box.

Workbooks.Open Filename:="M:\Excel Issues\" & Comp
Workbooks("TEST").Activate

Range("A1").Select
compval = ActiveCell.Value

Windows("TEST2").Activate

Range("A1").Select

Do Until ActiveCell.Value = compval
If ActiveCell.Value = compval Then
'do something
Else
ActiveCell.Offset(0, 1).Range("A1").Select
End If
Loop

End Sub

Any suggestions???
Thanks...
 
well do you have the workbook "Test 2" open, maybe its not open so your getting an error?
 
I'm not sure which file is the master and which is the comp file. You are activating Test2.

Also, your loop is not working as intended I'm sure.

Range("A1").Select

Do Until ActiveCell.Value = compval
If ActiveCell.Value = compval Then
'do something
Else
ActiveCell.Offset(0, 1).Range("A1").Select
End If
Loop

I don't think this is the code you are intending.
The "Do Until" line and the following "If" statement don't make sense. You've told the loop to exit the loop when they match, therefore your "If" statement will never fire.

My guess is your code is firing fine. What happened when I tested it is my ActiveCell.Value and Compval where the same in A1, so the loop exited and the sub ended.

Try stepping (F8) through the code to see what's happening. Also, you can hover over code to see what value it's holding or you can right click the code and "Add Watch".

Hope this helps,
calculus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top