Consulting

Results 1 to 16 of 16

Thread: Solved: type mismatch on open

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location

    Solved: type mismatch on open

    I am opening a workbook from another and get an error of type mismatch after it has run the code in the second workbook on opening. The code work great if opened manually. The error occurs at end sub.

    Here is the code:

    [VBA]
    Private Sub Workbook_Open()
    If Evaluate("=isblank(name)") = False Then GoTo Non

    nam = InputBox(Prompt:="Please enter your name", Title:="NEW WEEK")
    If nam = "" Then Exit Sub
    Range("totals!name").Value = nam
    hos = InputBox(Prompt:="Please enter your number", Title:="number")
    If hos = "" Then Exit Sub
    Range("totals!hosnum").Value = "NUMBER"
    Range("totals!hosnum1").Value = hos

    ' retreive the file name from diet
    workfil = Workbooks("diet.xlsm").Worksheets("Sheet1").Range("shfname")

    ' save the file
    Application.DisplayAlerts = False
    SaveAs Filename:=workfil
    Application.DisplayAlerts = True

    Non:


    End Sub
    [/VBA]

  2. #2
    whats this?

    [VBA]If Evaluate("=isblank(name)") = False Then GoTo Non
    [/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is it not the SaveAs statement, you have to say what you are saving such as Activeworkbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    is this a named range?
    are you trying to put a range of values into a variable?

    [VBA]Range("totals!name").Value = nam
    [/VBA]

  5. #5
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location

    Whats this

    Quote Originally Posted by daniel_d_n_r
    whats this?

    [vba]If Evaluate("=isblank(name)") = False Then GoTo Non
    [/vba]
    Evaluate is just that. Evaluates the contents of a cell. isblank is evaluating if the contents is blank in cell. (name) is the name of the cell. i.e. changed from c15 to name.
    NON is a label that it jumps to if it is found to be false.

  6. #6
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location
    Not trying to save anything.

  7. #7
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location
    The code works well if workbook is opened manually. No errors occur. It is only when the workbook is opened from another workbook that the error occurs.

  8. #8
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by barebum
    The code works well if workbook is opened manually. No errors occur. It is only when the workbook is opened from another workbook that the error occurs.
    by default,
    [VBA]
    Range("totals!hosnum").Value = "NUMBER"
    [/VBA]refers to
    [VBA]
    ActiveWorkbook.ActiveSheet.Range("totals!hosnum").Value = "NUMBER"
    [/VBA]when the workbook is opened from another workbook you need to use explicit references to the workbook and worksheet to which the code is to apply...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by barebum
    Not trying to save anything.
    Then what is

    [vba]

    SaveAs Filename:=workfil
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location

    Type mismatch

    Quote Originally Posted by xld
    Then what is

    [vba]

    SaveAs Filename:=workfil
    [/vba]
    This is not where the error is occuring. The code works provided the workbook is not opened from another workbook. If it is the error occurs at "End sub". The rest of the code completed successfully. Including the saveas.

  11. #11
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    post the workbook.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by barebum
    This is not where the error is occuring. The code works provided the workbook is not opened from another workbook. If it is the error occurs at "End sub". The rest of the code completed successfully. Including the saveas.
    Maybe that is so, but you said you wern't doing a save. That line suggests that you are at least trying to.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location

    type mismatch

    Quote Originally Posted by xld
    Maybe that is so, but you said you wern't doing a save. That line suggests that you are at least trying to.
    Based on this then it we could assume that the save is causing the problem. I tryed error trapping after the save and it still gave the same error at the End Sub. This code also runs perfectly as long as it is not trying to run after the workbook is open from another workbook.

  14. #14
    VBAX Regular
    Joined
    Sep 2007
    Posts
    7
    Location
    The problem was that it was trying to run the open workbook line in the previous workbook. The solution was to put a on error resume next command before and after the open workbook line. I tried before only and after only and that didn't work. It only worked when it was before and after.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I'm just a Wordie passing through so feel free to ignore me

    I am no wiser about anything from having read this whole thread - I don't know what the problem is or was - and the 'solution', it seems, is to ignore errors.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You are not alone TJ. The 'problem' has never been properly articulated, requests for a workbook, or information have been ignored. Pointing out non-working code has been dismissed by the OP as not relevant without any explanation as to why.

    I for one will reflect on any further posts by this OP.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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