Results 1 to 5 of 5

Thread: Error with VB code interacting with Excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    Error with VB code interacting with Excel

    I am using VB6 and Excel 2000

    I am getting the following error:

    'Object variable or With block variable not set' with the following code, BUT only when the cell value matches the RowName variable:

    Public Function doesRowExist(ExcelWorkSheet As Excel.Worksheet, Column As String, RowName As String) As Boolean
    Dim blnExists As Boolean
    Dim i As Integer

    blnExists = False
    'on error goto HandleError
    For i = 1 To 1000
    If ExcelWorkSheet.Range(Column & i).Value = RowName Then
    blnExists = True
    Exit For
    End If
    Next

    Set ExcelWorkSheet = Nothing
    doesRowExist = blnExists

    Exit Function
    HandleError:
    doesRowExist = False
    Set ExcelWorkSheet = Nothing
    End Function

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    sorry!

    VB Code:
    1. Public Function doesRowExist(WorkSheet As Excel.WorkSheet, Column As String, RowName As String) As Boolean
    2.     Dim blnExists As Boolean
    3.     Dim i As Integer
    4.    
    5.     blnExists = False
    6.     'on error goto HandleError
    7.     For i = 1 To 1000
    8.         If WorkSheet.Range(Column & i).Value = RowName Then
    9.             blnExists = True
    10.             Exit For
    11.         End If
    12.        
    13.     Next
    14.    
    15.     doesRowExist = blnExists
    16.    
    17.     Exit Function
    18. HandleError:
    19.     doesRowExist = False
    20. End Function

    It is occurring on this line:
    WorkSheet.Range(Column & i).Value = RowName
    but only when it should be true....

  3. #3
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    There is nothing wrong with the code you show. There might be something wrong with the code after it returns from the function. I am guessing you are trying to reference the worksheet still after you come back from the function if the function returns true. But in your function you set the worksheet to equal nothing since you are passing by reference. This is just my guess since there is nothing wrong with the code you show here. I would change your function declaration to pass byVal instead.

    VB Code:
    1. Public Function doesRowExist(ByVal ExcelWorkSheet As Excel.Worksheet, Column As String, RowName As String) As Boolean


    [Edit]
    I just saw what you posted. I tested your code and it worked fine for me and I used what you are using. I am guessing you are doing something wrong before you make your call.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    thanks everyone!

    I found out what was wrong...I was setting the wrksheet to nothing after the function....

    sorry about that!!!!

  5. #5
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Its fine, just glad you got it to work.

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