Help solving Run Time error 9

aa2000

Board Regular
Joined
Aug 3, 2011
Messages
87
Hi folks

I have a macro, that when called will look through column 1 in sheet 2 and compare the text to column 1 sheet 1. If they match it will select the cell 2 to the left in sheet 1 and try and follow the filepath of the hyperlink in that cell.

My code is:
Code:
Sub Importselect()
Dim a As Integer

For i = 3 To 15000
For a = 3 To 15000
name = Tabelle1.Cells(a, 1).Offset(0, 2).Hyperlinks.Item(1).Address
If Tabelle2.Cells(i, 1) = Tabelle1.Cells(a, 1) Then ImportTextFile FName:="name", Sep:="   "
Next a
Next i

End Sub

However when I run this macro I receive runtime error 9: Subscript out of range, and pressing debug highlights the following line:

Code:
If Tabelle2.Cells(i, 1) = Tabelle1.Cells(a, 1) Then ImportTextFile FName:="name", Sep:="   "

I believe the problem lies in the fact that it is not actually picking up the filepath, but I'm not sure and it could be elsewhere.

Any ideas on how to solve this? I'd really appreciate any sort of help!

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I thought that since when I manually add a filepath, it is in quotes, that name would also need to be.

Without putting it in quotes I get the Compile Error 'ByRef Argument type mismatch'
Not sure what that means?
 
Upvote 0
Try this:

Code:
Sub Importselect()
    Dim i As Integer, a As Integer, FName As String
    For i = 3 To 15000
        For a = 3 To 15000
            FName = Tabelle1.Cells(a, 1).Offset(0, 2).Hyperlinks.Item(1).Address
                If Tabelle2.Cells(i, 1) = Tabelle1.Cells(a, 1) Then ImportTextFile FName:=FName, Sep:="   "
        Next a
    Next i
End Sub

I changed the name of your variable because it's unwise to use an existing VBA property.
 
Upvote 0
Unfortunately that too returns the same Runt time error no. 9. I am beginning to think that the problem maybe due to the hyperlink itself rather than with the code. What do you think?

I will try it on another hyperlink that I manually added and see fi the problem remains and fet back to you.
 
Upvote 0
Fname is the correct file path that I am trying to import, so that at least works correctly. Thank you for that.
When I tried it with a manually inserted hyperlink that dispalyed the full file path in the cell rather than just the file name it imported it fine then displayed the same runtime error. So now I have no idea what that runtime error is connected to.
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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