Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

Help with multiple conditions within an if clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MichaelB - 16 Mar 2006 18:05 GMT
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
rhamre@citation.com - 16 Mar 2006 18:59 GMT
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.

> 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
[quoted text clipped - 29 lines]
>      Next oTbl
>  End Sub
MichaelB - 16 Mar 2006 19:52 GMT
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

> you can put an if inside the if.
>
[quoted text clipped - 34 lines]
> >      Next oTbl
> >  End Sub
Dave Lett - 16 Mar 2006 19:29 GMT
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
>I need some help making the below script a little smarter. I wish to have
>the
[quoted text clipped - 32 lines]
>     Next oTbl
> End Sub
MichaelB - 16 Mar 2006 21:30 GMT
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

> Hi,
>
[quoted text clipped - 53 lines]
> >     Next oTbl
> > End Sub
Dave Lett - 16 Mar 2006 22:08 GMT
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

> Dave,
>
[quoted text clipped - 84 lines]
>> >     Next oTbl
>> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.