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!

Opening and editing question?

Status
Not open for further replies.

ribhead

Technical User
Jun 2, 2003
384
US
I was able to open a workbook and put text in to the newly opened workbook but I'm not sure why I can't put a formula in to it. It puts the formula in to the workbook with the macro. It was working but I must have something missing because it always comes up file does not exist. I'm sure it's easy to somebody.

Sub gogetit()
Dim sfile As String
sfile = InputBox("File Name", "File Name Entry")
On Error GoTo errexit
Workbooks.Open Filename:="H:\sfile.xls"
Worksheets("Sheet1").Range("A1").Formula = "=$A$4+$A$10"
GoTo line10
errexit:
Err.Clear
MsgBox "File does not exist", , "File Not Found"
line10:
End Sub

I may not be very smart but I'm sure wirey!!!!
 
Change the line to look like this:
[blue]
Code:
  Workbooks.Open Filename:="H:\" & sfile & ".xls"
[/color]

And you should add code to test for the possibility that the user may include the ".xls" when entering the file name and not put another ".xls" in that case.

But why not just use the File Open dialog?

 
Thanks I don't know what the File Open dialog is. I am very new to this code and have never received any training other than what I've read on this forum. By the way is the code correct for inputting a new formula in to the newly opened workbook. I'll check in to the File Open dialog. The problem is I don't have a Help File.

I may not be very smart but I'm sure wirey!!!!
 
ribhead:
The Common Open Dialog provides a user interface for opening files that is similar to the normal Windows environment. May I suggest...

Sub gogetit()
Dim sfile As String
sfile = Application.GetOpenFilename(fileFilter:=" Excel Worksheets (*.xls), *.xls")
If sfile = False Then
'User clicked the 'Cancel' button
MsgBox "A File was not chosen."
Exit Sub
End If
Workbooks.Open Filename:="H:\" & sfile & ".xls"
Worksheets("Sheet1").Range("A1").Formula = "=$A$4+$A$10"
End Sub

********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top