Help with multiple conditions within an if clause

M

MichaelB

I need some help making the below script a little smarter. I wish to have the
macro key off of two differ criteria, one being the already present
..Columns.Count = 4 but I also need to include if the text within cell(1,1) =
“Mikeâ€. I have tried to make the if clause read two conditions but it appears
you can't do that (or should I say I can not do it). I have also tried
putting in a nested if but to no avail. Can someone point me in the right
direction?

Need the if clause to meet both conditions:
Columns.Count = 4 AND
Text within the cell(1,1) = “textâ€


Any help would be greatly appreciated..
Mike

********************************************


Sub ResizeTables()
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
With oTbl
If .Columns.Count = 4 Then
.AutoFitBehavior Behavior:=wdAutoFitFixed
.Columns.PreferredWidthType = wdPreferredWidthPoints
.Columns(1).PreferredWidth = InchesToPoints(1#)
.Columns(2).PreferredWidth = InchesToPoints(0.69)
.Columns(3).PreferredWidth = InchesToPoints(0.63)
.Columns(4).PreferredWidth = InchesToPoints(3#)
End If
End With
Next oTbl
End Sub
 
R

rhamre

you can put an if inside the if.

also, i think you can use "&&" and then put your second perameter, but i do
know the double if will work.
 
D

Dave Lett

Hi,

Every cell in every table has an end of cell mark included, which is what I
suspect is causing your problem. Try the following code on your document (be
sure to identify the correct table) and see if it works.

Dim oTbl As Table
Dim oCl As Cell
Dim oRng As Range

Set oTbl = ActiveDocument.Tables(1)
Set oCl = oTbl.Cell(1, 1)
Set oRng = oCl.Range
oRng.MoveEnd Unit:=wdCharacter, Count:=-1

If oTbl.Columns.Count = 4 And oRng.Text = "text" Then
MsgBox "True"
End If

HTH,
Dave Lett
 
M

MichaelB

I have tried the nested IF but was unable to get it to work. I know I’m doing
something wrong but I can’t put a finger on it. I believe there is a way to
include multiple conditions within an IF clause without having to use nested
IF statements (?). (I know you can due this within VB by just using the AND
operator but within VBA there must other rules.) I was hoping someone could
show me the exact format so I can then see the errors of ways.

Mike
 
M

MichaelB

Dave,

It appears that this does work but only for the first table. I need to
implement this within the loop so that all the tables throughout the document
get validated. I have tried the below which I believe is close but the
Range.Text variable is being populated with the entire contents of the table
and not just the first cell. If I can figure how to get the first cell to be
read in (only), I should be good to go.

(Shouldn’t it be something like .Cells(1,1).Range.Text = “Mike†?)


Sub ResizeTablesWith4columns()
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
With oTbl
If .Range.Text = "Mike" And oTbl.Columns.Count = 4 Then
MsgBox "True" .
End If
End With
Next oTbl
End Sub
 
D

Dave Lett

Hi,

No, it should ABSOLUTELY not be .Cells(1,1).Range.Text = "Mike".
Remember, the cell(1,1) and every cell in every table has an "end of cell"
marker. So, while you see "Mike" the .Range.Text of the cell is "Mike" &
EndOfCellCharacter (actually it's two characters). Try this instead:

Dim oTbl As Table
Dim iTbl As Integer
Dim oCl As Cell
Dim oRng As Range
For iTbl = ActiveDocument.Tables.Count To 1 Step -1
Set oTbl = ActiveDocument.Tables(iTbl)
Set oCl = oTbl.Cell(1, 1)
Set oRng = oCl.Range
oRng.MoveEnd Unit:=wdCharacter, Count:=-1

If oTbl.Columns.Count = 4 And oRng.Text = "Mike" Then
MsgBox "True"
End If
Next iTbl

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top