problem with a macro

argatica

Board Regular
Joined
Feb 3, 2011
Messages
79
hi everyone, im not an ace of macros with error handlers, and i have this one that open files and copies the info on a book.

the problem is that sometimes as i don't have some files, an error dialog appears and it turns very annoying.

every i represents a file, so i'm trying to make the errorhandler to jump to the next i everytime it doesn't find the file.

HTML:
Public Sub VTA()
'extrae VTA
    
Dim file            As String, _
    i               As Long, _
    j               As Long, _
    parentwb        As Workbook, _
    parentdirec     As String, _
    filews          As Worksheet, _
    destrow         As Long, _
    lastrow         As Long, _
    operador        As Variant
    
    
parentdirec = ActiveWorkbook.Path
operador = Array("ENTEL", "MOVISTAR", "CLARO")

Application.ScreenUpdating = False

Set parentwb = ActiveWorkbook
For i = 1 To 90
Set semana = Sheets(1).Range("D1")
    file = parentwb.Sheets("info").Range("A" & i).Value
On Error GoTo errhandler

    Workbooks.Open parentdirec & "\" & semana & "\" & file
    Set filews = Workbooks(file).Sheets(1)
    For j = 24 To 29 Step 2
        filews.Range(Cells(5, j), Cells(48, j + 1)).Copy
        With Workbooks("venta.xlsm").Sheets("tempvta")
            destrow = .Range("d" & .Rows.Count).End(xlUp).Row + 1
            lastrow = Application.Max(.Range("A" & .Rows.Count).End(xlUp).Row, destrow)
            .Range("B" & destrow).PasteSpecial xlPasteValues
            .Range("D" & destrow & ":D" & lastrow).Value = file
            .Range("E" & destrow & ":E" & lastrow).Value = operador(j / 2 - 12)
            .Range("A" & destrow & ":A" & lastrow).Copy Destination:=.Range("A" & lastrow).Offset(1, 0)
        End With
    Next j
    Application.DisplayAlerts = False
    Workbooks(file).Close
Next i
Range("F2:F" & lastrow).Value = semana
Application.ScreenUpdating = True

Exit Sub
errhandler:

    Next i

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
If Len(Dir(parentdirec & "\" & semana & "\" & file)) > 0 Then

This is a test to see if the file exists. If it exists, Dir() gives you the filename, else it will be an empty string "". Hence, testing if the length is bigger than 0 or not will give you what you want.
 
Upvote 0
thanks for the answer! sadly it won't work cause it checks whether the file is on the list, not if it really exists, but maybe you know a way to correct that. hope you can shed some more light on this.
 
Upvote 0
The test is for the file you are trying to open with the Workbooks.Open statement. So the test is correct, no?
 
Upvote 0
not really, cause that line of code checks if the cell has a value on it, not if the file exists.
 
Upvote 0
My test:

Code:
If Len(Dir(parentdirec & "\" & semana & "\" & file)) > 0 Then

will determine if the file a concatenation of 3 things) exists on your hard drive.

It does not have to do anything with a cell being empty or not.

Please see whether the concatenated text:

parentdirec & "\" & semana & "\" & file

is EXACTLY the file you want and need to open using Workbooks.Open(). Use MsgBox or Debug.Print.
 
Upvote 0
oh i thought that checked if the concatenation of of the 3 vars equaled empty or not. uhm well anyway, it's not working, i still got error message 1004: can't find the file.
 
Upvote 0
Again, as said above, check the contents of these variables using MsgBox or Debug.Print.
 
Upvote 0
i know what the values of the variable are, i can see them on the error msg cause they are the file that doesn't exists. what i want to is just skip those files.
 
Upvote 0
Insert the IF statement... !

Please post your full code that you are now using.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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