Faulty Code

jabawalkie5000

New Member
Joined
Jun 13, 2011
Messages
41
Hi All

I am trying to get this code to open up several documents stored in a sheet called "tab"

It doesnt work, can you spot the problem?

Thanks

Sub openFiles2()
Dim strpath As String
Dim rngFileNames As Range: Set rngFileNames = ActiveSheet.Range("A2", Cells(Rows.Count, "a").End(xlUp))
Dim FileNameCell As Range
Application.ScreenUpdating = False
strpath = "F:\J\Procurement\Change Management\Del\"
For Each FileNameCell In rngFileNames
If Not IsEmpty(FileNameCell) Then Workbooks.Open strpath & FileNameCell.Value
Next FileNameCell
Application.ScreenUpdating = False
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How does its failing manifest itself?

Just had a thought - is the sheet with the filenames active when the code is run?
 
Upvote 0
It doesn't work - can you be more helpful?
What happend when you run the code?
Is the code running without error (bombing)?
Is your code in a Standard module?
 
Upvote 0
The fact that nothing is happening suggests it's going through the range and finding nothing but empty cells.

try

Set rngFileNames = sheets("tab").Range("A2", sheets("tab").Cells(Rows.Count, "a").End(xlUp))
 
Upvote 0
Hi thanks it works, only problem is it wont let me open a word document or a powerpoint document in the code. I have added the word and powerpoint reference libraries, is there a code to invoke the opening of both applications and getting the files to be opened?

Thanks

The fact that nothing is happening suggests it's going through the range and finding nothing but empty cells.

try

Set rngFileNames = sheets("tab").Range("A2", sheets("tab").Cells(Rows.Count, "a").End(xlUp))
 
Upvote 0
You could use the shell command

Code:
shell "winword.exe ""c:\myfiles\this is a test.doc""

Note you need the two sets of double quotes if the file name has a space.

Put it together like:

Code:
Sub test()
    strpath = "F:\J\Procurement\Change Management\Del\"
    fname = "mydocument.doc"
    strshell = "winword.exe """ & strpath & fname & """"
    Shell strshell
End Sub
You could use Select Case or some if... then to test the last 3 or 4 chars of the filename if there are several apps you might need to use.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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