Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2005, 10:24 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to compare cell contents with a character?

Hello, I have a following problem. I am writing a code in VBA as Excel Macro where one of the tasks is to compare a string which is in a cell (string is unknown!) with a character-letter 'D'. I tried to use InStr function as follows :

Dim Val As Integer
Set c = Application.Worksheets(1).Cells(1,1).Value
Val = InStr(c, "D", 1)
If Val = 0 Then bla bla bla 'which means that if letter D is in a
                             'string in cell a1 then...

Though I get a [run-time error '13': types mismatch] in concern to InStr function. Maybe I should declare c differently? I know that c in InStr Function must be a 'string expression' but I don't know how I should write a string to c if not by Value Property. Any suggestions? Please help!
Thanx, Daniel.


 
Old June 8th, 2005, 10:38 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

You have used the syntax Set C, remove the Set and it works super dooper.

Cheers

Matt

 
Old June 8th, 2005, 10:47 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

might I also recommend using this:-

Dim Val As Integer
If IsEmpty(Application.Worksheets(1).Cells(1, 1).Value) Then
    c = 0
    Else
    c = Application.Worksheets(1).Cells(1, 1).Value
End If
Val = InStr(1, c, "D")

your could even slim it down a bit with ...
Dim Val As Integer
If IsEmpty(Application.Worksheets(1).Cells(1, 1).Value) Then
  Val = 0
Else
  Val = InStr(1, Application.Worksheets(1).Cells(1, 1).Value, "D")
End If
...
also are you sure that your worksheet is always going to be index item 1, could you call it by name rather than by index number?
 
Old June 8th, 2005, 10:55 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Matt, I removed Set syntax and wrote that line as:
c = Application.Worksheets(1).Cells(1, 1).Value; though the compiler shows me the very same error.
Daniel.


 
Old June 8th, 2005, 11:02 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not sure if you mean to only require an upper case D but if you wanted any D i.e. d or D then use this code...

Dim Val As Integer
If IsEmpty(Application.Worksheets(1).Cells(1, 1).Value) Then
  Val = 0
Else
  Val = InStr(1, UCase(Application.Worksheets(1).Cells(1, 1).Value), "D")
End If

are you still getting the error?

 
Old June 8th, 2005, 11:36 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Matt, thx much for your help. It finally works! I didn't even have to write .Value at the end. And with Set syntax it works as well. I have to write Set 'cause I need to work on c later and if I don't write Set c than an arror occurs that object is required. Anyways, thx a lot again! Daniel.






Similar Threads
Thread Thread Starter Forum Replies Last Post
compare a cell with nothing popaerou Excel VBA 2 October 26th, 2008 12:35 PM
How to create a Hyperlink based on Cell Contents chatguy Excel VBA 4 October 15th, 2007 09:43 PM
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 03:30 PM
Pointer to contents of another cell swood829 Excel VBA 2 June 14th, 2005 03:51 PM
Delet contents of a single cell marmer Classic ASP Basics 1 August 27th, 2003 08:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.