Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am converting some code from VB6 to .NET. I am having an issue evaluating an excel cell as empty.

    The goal is to see if an excel cell is empty and, if so, give it the contents of the cell in the column before it. Looping through rows, If the cell in column 8 is blank, replace it with the value in column 7 and format it as a date.

    The pre-conversion code is:
    If Not .Cells(LoopIndex, 8) = "" Then .Cells(LoopIndex, 8) = Format(.Cells(LoopIndex, 7), "mm/dd/yyyy")

    The .Net conversion offers:
    If Not (.Cells._Default(LoopIndex, 8) = "") Then .Cells._Default(LoopIndex, 8) = VB6.Format(.Cells._Default(LoopIndex, 7), "mm/dd/yyyy")

    Using the conversion as-is I get
    *** ERROR *** Overload resolution failed because no Public '=' can be called with these arguments: 'Public Shared Operator =(a As String, b As String) As Boolean': Argument matching parameter 'a' cannot convert from '__ComObject' to 'String'.

    So, trying to evaluate the comObject I use .ToString to evaluate the content of the cell:
    If Not (.Cells._Default(LoopIndex, 8).ToString = "") Then .Cells._Default(LoopIndex, 8) = VB6.Format(.Cells._Default(LoopIndex, 7), "mm/dd/yyyy")

    Now it runs without errors, but one problem. The condition is never satisfied. The "cell/string" appears to never be blank, null, nothing, isnullorempty, or an empty string even if I Trim it, even though I cannot see anything in it.

    Is my problem in how I am using the comObject or the fact that the cell isnt truly empty? How can I find out what is in that cell and how can I evaluate it for this condition? I appreciate any help in understanding how this cell range is being used further.

  2. #2
    New Lounger
    Join Date
    Dec 2009
    Location
    Quebec city
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have to use "==" to evaluate a value, "=" is to assign a value.

    You can also use .Equals() method which is the better way. Or better yet, if your using a sting and want to check if it's null or empty use String.IsNullOrEmpty(valueToCheck).

    Hope this helps

    Ben

  3. #3
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! I did use String.IsNullOrEmpty(valueToCheck) as one of my attempts and that does not work. As far as I can tell I think the cell has "spaces" of some kind. I did a Len(), if that is appropriate, as it says 18. I dont know what is in there since I tried to trim to nothing....

Posting Permissions

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