Results 1 to 8 of 8

Thread: [RESOLVED] Sheet name does not exist late bound excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Resolved [RESOLVED] Sheet name does not exist late bound excel

    Hi

    I need to check if a excel sheet name exists in late bound VB6.0. But I am having difficulty setting each variable and generally making the sheet code to to work. This is the code I have so far. Whats wrong with the code?

    VB Code:
    1. ' Grabs filename and allocates it to mywbook
    2.  
    3. frmeverythingLMP.txtFileName.Text = FileName4
    4. pos = InStrRev(FileName4, "\")
    5. mywbook = Mid(FileName4, pos + 1, Len(FileName4) - pos + 1)
    6.  
    7.  
    8. ' Need to appropriately set xlsheet
    9.  
    10. Set xlapp = GetObject(, "Excel.Application")
    11. Set wkbobj = xlapp.Workbooks(mywbook)
    12. Set xlbegin = wkbobj.Sheets(1)  ' this variable is used further down but not shown
    13.  
    14.  
    15. 'Check if result sheet exists - is this right
    16.  
    17. For Each xlsheet In wkbobj.Worksheets                       'Check if a sheet named result exists.
    18.         If xlsheet.Name <> Result Then
    19.            MsgBox ("The Result sheet does not exist")
    20.     Exit Sub
    21.         End If
    22. Next xlsheet

    thanks
    Last edited by gphillips; Jan 19th, 2007 at 05:50 AM.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Sheet name does not exist late bound excel

    Not enough code shown for me to figure out what is wrong. What exactly are your error indications? Show the "Dim" statement for var Result and show where you set it. That might help me a little?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Sheet name does not exist late bound excel

    Hia,


    It just goes staright to the msgbox regardless whether result sheet exists or not.

    I haven't set xlsheet becauase I don't know how - xlsheet should be any worksheet in the selected workbook
    I think this is where the problem are and also withhin the worksheet check code.

    I have posted all the code but not the module for the filename - because this is not where the problems are. This element works fine.


    VB Code:
    1. Private Sub cmdCopy_Click()
    2.  
    3. 'This code first checks if the result sheet exists - if not produces an error. It then creates the starting point sheet name if result sheet exist.
    4.  
    5. Dim xlsheet as object
    6. Dim xlapp as object
    7. Dim wkbobj as object
    8. Dim xlbegin as object
    9.  
    10.  
    11.  
    12. ' strips network location path to identify only the workbook name - there is nothing wrong with element of code
    13. ' its links to a module not shown - but
    14.  
    15.  
    16.  
    17. frmeverythingLMP.txtFileName.Text = FileName4
    18. pos = InStrRev(FileName4, "\")
    19. mywbook = Mid(FileName4, pos + 1, Len(FileName4) - pos + 1)
    20.  
    21. Set xlapp = GetObject(, "Excel.Application")
    22. Set wkbobj = xlapp.Workbooks(mywbook)
    23. Set xlbegin = wkbobj.Sheets(1) ' xlbegin is starting sheet
    24.  
    25. For Each xlsheet In wkbobj.Worksheets 'Check to see if the result sheet exists
    26.         If xlsheet.Name <> Result Then
    27.            MsgBox ("You have chosen to create a full structure LMP - but you have not added the Result worksheet. The Result worksheet should contain the IK designs extracted from the baseline metrics report")
    28.     Exit Sub
    29.         End If
    30. Next xlsheet
    31.  
    32. ' Create a starting point sheet
    33. xlbegin.Copy After:=wkbobj.Sheets(wkbobj.Sheets.Count)
    34. wkbobj.Sheets(wkbobj.Sheets.Count).Name = "Starting point"
    35.  
    36.  
    37. Set xlapp = Nothing
    38. Set wkbobj = Nothing
    39. Set xlbegin = Nothing
    40.  
    41. cmdCopy.Enabled = False
    42. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Sheet name does not exist late bound excel

    Missed one point.

    Result is not a variable it is a worksheet. Can you help with the code ammendments

    If xlsheet.Name <> Result Then

    Thnks

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Sheet name does not exist late bound excel

    But like this it is expecting Result to be a variable containing some sort of string. If this is not the case, i.e. you are actually looking for a sheet called "Result", then you will need to denote it as such using quotes as I have just done.


    Turn Option Explicit on by declaring it at the top of your module - it will avoid errors such as these by not allowing you to have variables which are not declared.


    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Sheet name does not exist late bound excel

    The problem is deeper than this. It still keeps on showing the msgbox whether it exists or not.

    How do I set xlsheet appropriately late bound. I havn't set it. XLSHEET is not one particular sheet as below:

    Set xlsheet = wkbobj.Sheets(1)


    Perhaps the problem is even more deeper than this.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Sheet name does not exist late bound excel

    You have a problem in the structure of your for loop which is checking to see
    if the result sheet actually exists.

    N.B. I have put quote marks around Result as it seems that is the name of
    your actual sheet, and not a variable in which you would hold the sheet name.

    VB Code:
    1. For Each xlsheet In wkbobj.Worksheets 'Check to see if the result sheet exists
    2. [B]    If xlsheet.Name <> "Result" Then
    3.        MsgBox ("You have chosen to create a full structure LMP - but you" & _
    4.                    " have not added the Result worksheet. The Result worksheet " & _
    5.                    "should contain the IK designs extracted from the baseline " & _
    6.                    "metrics report")
    7.        Exit Sub
    8.     End If[/B]
    9. Next xlsheet

    What the code is doing is checking each sheet in turn, If the name of the
    sheet that it is checking is not "Result" then it is displaying the MsgBox and
    exiting the sub.

    Consequently if you have two or more sheets, then it will always exit the sub
    when it encounters the first one that is not named Result

    Try the following code.

    VB Code:
    1. ' Include this among your declarations
    2. Dim WkShtExists As Boolean

    VB Code:
    1. WkShtExists = False
    2. For Each xlsheet In wkbobj.Worksheets 'Check to see if the result sheet exists
    3.     If xlsheet.Name = "Result" Then
    4.          WkShtExists = True
    5.          Exit For
    6.     End If
    7. Next xlsheet
    8.  
    9. If Not WkShtExists Then
    10.        MsgBox ("You have chosen to create a full structure LMP - but you" & _
    11.                    " have not added the Result worksheet. The Result worksheet " & _
    12.                    "should contain the IK designs extracted from the baseline " & _
    13.                    "metrics report")
    14. End If

    Hope this helps.
    Signature Under Construction

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Sheet name does not exist late bound excel

    Thanks very much appreciated, this works just add exits sub after the msg box.

Posting Permissions

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



Click Here to Expand Forum to Full Width