+ Reply to Thread
Results 1 to 2 of 2

Weird... type mismatch only half of the time

  1. #1
    Registered User
    Join Date
    07-14-2005
    Posts
    15

    Question Weird... type mismatch only half of the time

    I have a GetValue function which returns a cell's value from a closed workbook using the ExecuteExcel4Macro command. I have no problem assigning the returned value to the corresponding cell of the active sheet. However, when I store the return value in a variant called "temp" to check for empty cells, half of the time it runs smoothly and the other half it returns the error "Type mismatch". I bolded the line that returns an error half of the time. Any suggestions?

    Private Function GetValue(path, file, sheet, ref)

    Dim arg As String

    arg = "'" & path & "[" & file & "]" & sheet & "'!" & Range(ref).Range("A1").address(, , xlR1C1)

    GetValue = ExecuteExcel4Macro(arg)

    End Function

    Private Sub FilterData(InputFile, InputSheet, InputRow, OutputRow, FileDone)

    Dim temp As Variant

    p = PathName(InputFile)
    f = FileName(InputFile)
    s = InputSheet

    Application.ScreenUpdating = False

    Do
    temp = GetValue(p, f, s, Cells(InputRow, 1).address)
    If temp = 0 Or temp = "" Then FileDone = True

    If Not FileDone Then
    For c = 1 To 20
    a = Cells(InputRow, c).address
    ActiveSheet.Cells(OutputRow, c) = GetValue(p, f, s, a)
    Next c
    InputRow = InputRow + 1
    OutputRow = OutputRow + 1
    End If

    Application.ScreenUpdating = True

    Exit Do
    FileDone = True
    Loop

    End Sub

  2. #2
    Dave Peterson
    Guest

    Re: Weird... type mismatch only half of the time

    If temp is returning an error (#n/a, #div/0, etc), then using that in the
    comparison will cause an error:

    * If temp = 0 Or temp = "" Then FileDone = True *

    I'd check first:

    if iserror(temp) then
    'do what you want
    else
    if temp= 0 _
    or temp = "" then
    Filedone = true
    end if
    end if

    (I like the multi-line if/end if better--personal choice!)

    paulharvey wrote:
    >
    > I have a GetValue function which returns a cell's value from a closed
    > workbook using the ExecuteExcel4Macro command. I have no problem
    > assigning the returned value to the corresponding cell of the active
    > sheet. However, when I store the return value in a variant called
    > "temp" to check for empty cells, half of the time it runs smoothly and
    > the other half it returns the error "Type mismatch". I bolded the line
    > that returns an error half of the time. Any suggestions?
    >
    > Private Function GetValue(path, file, sheet, ref)
    >
    > Dim arg As String
    >
    > arg = "'" & path & "[" & file & "]" & sheet & "'!" &
    > Range(ref).Range("A1").address(, , xlR1C1)
    >
    > GetValue = ExecuteExcel4Macro(arg)
    >
    > End Function
    >
    > Private Sub FilterData(InputFile, InputSheet, InputRow, OutputRow,
    > FileDone)
    >
    > Dim temp As Variant
    >
    > p = PathName(InputFile)
    > f = FileName(InputFile)
    > s = InputSheet
    >
    > Application.ScreenUpdating = False
    >
    > Do
    > temp = GetValue(p, f, s, Cells(InputRow, 1).address)
    > * If temp = 0 Or temp = "" Then FileDone = True *
    >
    > If Not FileDone Then
    > For c = 1 To 20
    > a = Cells(InputRow, c).address
    > ActiveSheet.Cells(OutputRow, c) = GetValue(p, f, s, a)
    > Next c
    > InputRow = InputRow + 1
    > OutputRow = OutputRow + 1
    > End If
    >
    > Application.ScreenUpdating = True
    >
    > Exit Do
    > FileDone = True
    > Loop
    >
    > End Sub
    >
    > --
    > paulharvey
    > ------------------------------------------------------------------------
    > paulharvey's Profile: http://www.excelforum.com/member.php...o&userid=25227
    > View this thread: http://www.excelforum.com/showthread...hreadid=387547


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1