0

I have a code in a macro in excel. in the Sheet4

and in a part of the code i have:

Call functionA("file.csv", "A3", "Sheet2")

and i need that if the macro can not do the function Hidden a column

i intent with the following code, but not works

On Error Resume Next
    all functionA("file.csv", "A3", "Sheet2")
Columns("Sheet3!V:V").EntireColumn.Hidden = True

first never hide the column of a specific sheet. And if there is a error continue, and it works fine. the problem is with i intent hide the column. I need hide the column only if a error appear

The problem is i read some csv files. the functionA load the file to excel. and if the file not exist. the excel must to continue and hide a column V of Sheet 3 because in V of Sheet 3 i have some dates that i calculate of the file that do not exist

Community
  • 1
  • 1
camilo soto
  • 1,211
  • 2
  • 9
  • 10
  • Just so I'm clear...if there is an error when executing Call functionA("file.csv", "A3", "Sheet2") hide column V, otherwise don't hide it. Is that correct? – sous2817 Jun 07 '13 at 16:34
  • remove the "On Error Resume Next" line and re-run.. It's a bad idea to hide errors like that. What is the error? – Sam Jun 07 '13 at 16:34
  • The problem is i read some csv files. the functionA load the file to excel. and if the file not exist. the excel must to continue and hide a column V of Sheet 3 because in V of Sheet 3 i have some dates that i calculate of the file that do not exist – camilo soto Jun 07 '13 at 16:39
  • Can you post your functionA sub? What you should do is check if the file exists BEFORE calling the sub. If file exists, run the functionA sub, otherwise hide the column. – sous2817 Jun 07 '13 at 16:42
  • you need to do a check for if the file exists then... sous2817's answer looks like a good starting point – Sam Jun 07 '13 at 16:54

3 Answers3

1

What about How to Use "On Error" to Handle Errors in a Macro. You may take a look at good-patterns-for-vba-error-handling I often use the macro recorder to do the stuff i do not know while recording it and then take a look at the generated macro code.

Sub MyMacro()

    On Error GoTo ErrorHandler
    . . .
    Exit Sub
ErrorHandler:
    . . .
    Resume <or Exit Sub>
    . . .
End Sub

Hide a column

Sub HideColumn()
    Dim colIndex As Integer
    colIndex = CInt(InputBox("Index of column to hide"))
    Cells(1, colIndex).EntireColumn.Hidden = True
End Sub
Community
  • 1
  • 1
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
1

OK, I've made a few assumptions here, but this should get you on the right path:

Option Explicit

Sub a()

    If FileThere("c:\test\file.csv") Then
        Call functionA("file.csv", "A3", "Sheet2")
        {WHATEVER ELSE NEEDS TO HAPPEN AFTER THAT functionA CALL GOES HERE}
    Else
        Sheet3.Range("V:V").EntireColumn.Hidden = True
        Exit Sub
    End If

End Sub


Function FileThere(FileName As String) As Boolean
     If (Dir(FileName) = "") Then
        FileThere = False
     Else:
        FileThere = True
     End If
End Function

Sub functionA(f1 As String, f2 As String, f3 As String)
    {WHATEVER YOU NEED TO DO GOES HERE}


End Sub
sous2817
  • 3,915
  • 2
  • 33
  • 34
0

Make your function return a success value.

So : here is your function

Function functionA (Byval Cvsfiel As string, byVal SheetName as string) as integer

    On error go to Err
    ' insert your code here
    ....
    ...
    ....

    return 0 '0 means function was successfull

    Exit function

    Err: 

    Return -1 ' -1 means function did not run successfullt

End Function 

Now your code will be this:

 Dim result as integer

    result=functionA("file.csv", "A3", "Sheet2")

    If result=-1 then  'means if error
    Columns("Sheet3!V:V").EntireColumn.Hidden = True

    End If
S Nash
  • 2,363
  • 3
  • 34
  • 64