Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

if cell contents equals

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » If cell contents equals...

If cell contents equals...

ResolvedVersion 2002/XP
Edited on Wed 13 Mar 2013, 16:24

Caspar has attended:
Excel VBA Intro Intermediate course

If cell contents equals...

Hello again, greetings.


I'm trying to use an 'If' statement with cell contents. It's really simple but for some reason actually referring to what is in the cell (rather than the cell shading and other attributes and so on) is strangely elusive...


So far I have


Sub deleteifahyphen()

Set myTable = ActiveDocument.Tables(18).Tables(8)
Set myRange = ActiveDocument.Range(myTable.Cell(2, 2) _
.Range.Start, myTable.Cell(2, 2).Range.End)


If myRange = " - " Then _

ActiveDocument.Tables(18).Tables(8).Columns(2).Delete

End If

End Sub




Now all this is trying to say is that if there's a hyphen in cell (2,2) within a certain table, delete the whole table column.

It runs but does not have any effect. Should saying 'If cell x has y in it, do z' be so hard?

RE: If cell contents equals...

Hi Caspar

Thanks for getting in touch. Does the code work if you substitute all the mentions of "Cell" to "Cells" ?

By the way, that will search for a hyphen surrounded by spaces, so just remove them if you want only a hyphen.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: If cell contents equals...

[I've been away/had other duties for a while]


I'm afraid replacing "Cell" with "Cells" doesn't work, in fact it springs an error.

Yes, the hyphen is surrounded by spaces. Although it may be useful to say 'contains a hyphen' instead.


I'm not sure if there is an 'In (x,y,z,)' command for 'contains'. Any thoughts?

There must be an easier way to do this - am I approaching things in the wrong way?

RE: If cell contents equals...

Hi Caspar

Thanks for your reply. There is a function called InStr which will see if a cell "contains" something.

So it could be something like

If InStr(Tables(18).Tables(8).Cell(2,2), "-") Then

ActiveDocument.Tables(18).Tables(8).Columns(2).Delete

End If

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: If cell contents equals...

InStr works! Many thanks!

Wed 20 Mar 2013: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

DATEDIF function

The DATEDIF function is a worksheet function that computes the difference between two dates. This function is documented only in the help files for Excel 2000. It isn't documented in Excel 2002.
the formula function is:
=DATEDIF(Your age,Today(),"Y")
Y stands for year

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.13 secs.