98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » If cell contents equals...
If cell contents equals...
Resolved · Medium Priority · Version 2002/XP
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
Wed 20 Mar 2013: Automatically marked as resolved.
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
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Validating text entries1. Select the range of cells.
where A1 is the first cell in the range. 6. Click OK. |