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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EXCEL VBA: Look for Dynamic Hyperlink

Status
Not open for further replies.

JAMES717

Technical User
Oct 13, 2009
15
US
I have a static list of numbers (Vendor ID) and the job cycles through each number and opens a file (if present).

The problem is that the On Error only works the first time through the job. I can not use On Error Resume Next, because I have lines of code that if the file opens that need to be completed.

I can not use “if DIR(myfile) = “” then” because I am trying to lookup a file on the internet and DIR(“ & ActiveCell & “.xls”) is not working.

Sample CODE (as is):

Do Until ActiveCell = ""

On Error GoTo errhndler:

Workbooks.Open FileName:=" & ActiveCell.Offset(0, 1) & " " & ActiveCell & ".xls
 
With some minor tweaking, I got the first part to work. I can surely tell the difference in my guerilla coding in your professional coding.

Next error:

on Set ws = Workbooks.Open( _
FileName:=sPath & "On-time%20Shipment/" & r.Offset(0, 1) & " " & r.Value & ".xls")

It File Open, but I am getting a Type mismatch error on that line
 

Code:
Set ws = Workbooks.Open( _
        FileName:=sPath & "On-time%20Shipment/" & r.Offset(0, 1) & " " & r.Value & ".xls")[b][red].Sheets(1)[/red][/b]
or whatever sheet you want.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I will finish the coding out and let you know how it looks. Thanks so much for your help.
 
Skip,

Looks like it will work, but I can not get the job to go to the next ID. I do not use for each next loops, so I think I may have the next posted inaccuratly.

I do not think I need to use the activeCell.Offset(1,0).Select anymore becaue the for each next loop takes the place of all that, but where do I put the next?
 


activeCell.Offset(1,0).Select is an amateur method of looping. It is slow and cumbersome. I rarely use acive anything. Rather I reference object explicitly, using object variables.

Here is the basic control structure...
Code:
Sub test()
    Dim r As Range

    On Error Resume Next
    
    For Each r In Range([A2], [A2].End(xlDown))
    
        If Err.Number = 0 Then
        
        Else
          
          Err.Clear
        End If
    
    Next
End Sub
I would put the three folders in a list on a sheet and loop thru each of them...
Code:
Sub test()
    Dim r1 As Range, r2 as range

    On Error Resume Next
    
    For Each r1 In Range([A2], [A2].End(xlDown))
      for each r2 in range([i]YourFolderListrange[/i])
        'open the workbook for the r2 folder
        If Err.Number = 0 Then
           'save the workbook
           'copy the sheet
           'close the workbook
        Else
          
          Err.Clear
        End If
      next
    Next

   'whatever you do after all the workbooks have been opened, saved & copied

End Sub


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top