PDA

View Full Version : [SOLVED:] If Condition Help!!!!



Rakesh
09-10-2014, 04:33 PM
Hi Guys,

I have created the below macros. By using split function I split the paragraph from the hyphen and get the numeric value as string one by one.
The 1st macro get the numeric value as string from the 1st para to the end of the document one by one.
The 2nd macro get the numeric value as string from the 2nd para to the end of the document one by one.



Sub test1()
Dim strtxt As String
Dim strtmp As String
Dim md, b As String
Dim x As Integer

With ActiveDocument.range
' Loop through all paragraphs
For i = 1 To .Paragraphs.count
'Get the paragraph text, minus the paragraph marker
strtmp = .Paragraphs(i).range.Text
strtmp = Left(strtmp, Len(strtmp) - 1)
For j = 1 To UBound(Split(strtmp, Chr(45)))
strtxt = Split(strtmp, Chr(45))(j)
strtxt = Left(strtxt, 3)
md = strtxt
For x = 1 To Len(md)
L = Mid(md, x, 1)
If IsNumeric(L) Then newmd = newmd & L
Next x
MsgBox newmd
Application.ScreenUpdating = True
newmd = ""
Next
Next
End With
End Sub




Sub test2()
Dim strtxt1 As String
Dim strtmp1 As String
Dim xd, c As String
Dim Y As Integer

With ActiveDocument.range
' Loop through all paragraphs
For m = 2 To .Paragraphs.count
'Get the paragraph text, minus the paragraph marker
strtmp1 = .Paragraphs(m).range.Text
strtmp1 = Left(strtmp1, Len(strtmp1) - 1)
For n = 1 To UBound(Split(strtmp1, Chr(45)))
strtxt1 = Split(strtmp1, Chr(45))(n)
strtxt1 = Left(strtxt1, 3)
xd = strtxt1
For k = 1 To Len(xd)
P = Mid(xd, k, 1)
If IsNumeric(P) Then newxd = newxd & P
Next k
MsgBox newxd
Application.ScreenUpdating = True
newxd = ""
Next
Next
End With
End Sub

I have to compare both the strings, If the string doesn’t matches, then the whole para should be highlighted.
But there are some paragraph which returns Null “” as string. In this case should skip that para and to compare with the next para.

For example.
To compare the 1st and 2nd para string. If the 2nd para string doesn’t matches, 2nd para should be highlighted.
If the 2nd para returns Null “” as string, should compare 1st and 3rd para. Likewise Loop throughout the Document.
I tried and failed.

Can anybody help in this case Or any other way to simplify this macro.

Thanks,
Rakesh

macropod
09-11-2014, 12:00 AM
Perhaps:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, RngFnd As Range
Dim StrFnd As String, StrRep As String
StrRep = "-"
Set RngFnd = ActiveDocument.Range(0, 0)
With ActiveDocument.Range
.HighlightColorIndex = wdYellow
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "-[0-9]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = 0
Set RngFnd = .Duplicate
StrFnd = RngFnd.Text
If InStr(StrRep, StrFnd & "-") = 0 Then
StrRep = StrFnd & StrRep
StrFnd = Replace(StrFnd, "-", "")
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = StrFnd
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
i = i + 1
If i > 1 Then
.Duplicate.Paragraphs(1).Range.HighlightColorIndex = wdNoHighlight
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End If
If i > 1 Then RngFnd.Paragraphs(1).Range.HighlightColorIndex = wdNoHighlight
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub

Rakesh
09-11-2014, 01:13 PM
Hi macropod,

Thanks for your Valuable Time, spent for me. I have checked the code. First it's highlighting the whole document and then removing the highlight for the para which is mismatch. Whereas I need to highlight only the mismatch para. I have fixed it by changing the lines as follows.

I have set comment Block on this Line


.HighlightColorIndex = wdYellow

and changed the below lines


.Duplicate.Paragraphs(1).range.HighlightColorIndex = wdYellow


If i > 1 Then RngFnd.Paragraphs(1).range.HighlightColorIndex = wdYellow


Second it's unhighlighting the para for only the 1st mismatch, others mismatch para remains the same.

But as per my first post, it should be as below


Compare the 1st and 2nd para string. If the 2nd para string doesn’t matches, 2nd para should be highlighted.
If the 2nd para returns Null “” as string, should compare 1st and 3rd para. Likewise Loop throughout the Document.

Apologize for not attached the sample output file on the 1st post.

Herewith I have attached the files for your reference

Thanks,
Rakesh

macropod
09-11-2014, 04:01 PM
You have not understood the code I posted. Sure, it starts off by highlighting the whole document, but it then removes the highlights for the paragraphs with matches, leaving only the unmatched paragraphs highlighted. This is the opposite of what you concluded.

snb
09-13-2014, 03:55 AM
Sub M_snb()
With ActiveDocument
sn = Filter(Split(.Content, vbCr), " ")
.Content = Join(sn, vbCr)

For j = 1 To UBound(sn) - 1
If Val(Split(sn(j - 1), "-")(1)) <> Val(Split(sn(j), "-")(1)) And Val(Split(sn(j - 1), "-")(1)) <> 0 And Val(Split(sn(j), "-")(1)) <> 0 Then .Paragraphs(j + 1).Range.HighlightColorIndex = wdTurquoise
Next
End With
End Sub

Rakesh
09-15-2014, 08:45 AM
Hi snb,

Thank you very much.

It's works fine. But the code missing one condition as per my previous post, which is shown below.


If the 2nd para returns Null “” as string, should compare 1st and 3rd para. Likewise Loop throughout the Document.

Thanks,
Rakesh

snb
09-15-2014, 11:37 AM
You are mistaken. The code provides for that situation.

PS. Can you please remove the exclamation marks from the title of this thread ?

Rakesh
09-15-2014, 12:14 PM
Hi snb,

I tried to remove the exclamation mark from the title. But I can't, it is disabled.

I have tested the code with sample file which I have attached in the post 3. It's missing one condition as I have mentioned in post 6. Below lines for your reference.

@CA-9c entry ind p9:Net increase (decrease) in net assets 454,367 57,197 199,768 (724,503) 12,062,873 369,019 (506,659) 75,633 (2,405,490)
@CA-tbl subhd p5^:Net assets:
@CA-10c entry ind p9 rule_\=thn:Beginning of year 384,725 3,773,246 2,739,545 <\!_> 1,682,399 2,727,040 249,610 2,405,490
@CA-9c entry$ ind p9 rule_\=dbl:End of year $4,375,776 $441,922 $3,973,014 $2,015,042 $12,062,873 $2,051,418 $2,220,381 $325,243 $<\!_>


Thanks,
Rakesh

snb
09-15-2014, 02:40 PM
Please check what nullstring means in VBA

macropod
09-15-2014, 03:11 PM
Personally, I'm having trouble following the logic behind the shading you have in your 'Sample Output'. It's not at all apparent why some paragraphs are shaded and others aren't. For example, why are the 1st and 4th paragraphs (a -9c paragraph not followed by another with -9c) not highlighted, but the 2nd one (a -10c paragraph followed by another with -10c) is?

A revised version of my original macro follows. I should like to point out that your original post mentioned nothing of these strings you're interested in being alpha-numeric. You only mentioned the hyphens and numbers. You cannot expect to get the correct results when you omit crucial details.

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, RngFnd As Range, RngTmp As Range
Dim StrFnd As String
With ActiveDocument.Range
.HighlightColorIndex = wdYellow
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "-[0-9]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = 0
StrFnd = .Duplicate.Words.First.Text & .Duplicate.Words.Last.Text
If .Duplicate.Paragraphs.Last.Range.End = ActiveDocument.Range.End Then Exit Do
Set RngFnd = .Duplicate: Set RngTmp = .Duplicate
RngFnd.End = RngFnd.Paragraphs.Last.Next.Range.End
RngTmp.End = RngTmp.Paragraphs.Last.Next.Range.End
If RngFnd.End <> ActiveDocument.Range.End Then
RngFnd.End = RngFnd.Paragraphs.Last.Next.Range.End
RngTmp.End = RngTmp.Paragraphs.Last.Next.Range.End
End If
With RngFnd
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = StrFnd
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
If .InRange(RngTmp) Then
i = i + 1
If i > 1 Then
.Duplicate.Paragraphs(1).Range.HighlightColorIndex = wdNoHighlight
End If
.Collapse wdCollapseEnd
.Find.Execute
Else
Exit Do
End If
Loop
End With
'If i > 1 Then .Duplicate.Paragraphs.First.Range.HighlightColorIndex = wdNoHighlight
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
You'll see there's a commented-out line in the code. That determines whether the first paragraph in a matched range gets highlighted. You may want to uncomment that line. other than that, I suggest you explain more clearly what your highlighting criteria are.

Rakesh
09-16-2014, 10:02 AM
I doesn’t know where I was lack in my explanation. To my view I’m clear in my explanation. I’m extremely sorry if I was wrong anywhere.

Please give this as a last chance for me. If I failed. Please don’t waste your valuable time for me.

Below Colored Text are the para to be highlighted. My criteria is (as I mentioned only Numeric after hyphen was to compare)

Compare 1st and 2nd para. (9 and 10 is mismatch), So the 2nd para should be Highlighted) next
2nd and 3rd para. (10 and 10 is matching), So should search for the next.
3rd and 4th para. (10 and 9 is mismatching), So the 4th para should be Highlighted) next
4th and 5th para. (9 and 9 is matching), So should search for the next.
5th and 6th para. (9 and “No Numeric i.e., tbl”) So should search for next para
5th and 7th para (9 and 10 is mismatching), So the 7thth para should be Highlighted) next
7th and 8th para (10 and 9 is mismatching), So the 8thth para should be Highlighted)


@CA-9c
@CA-10c
@CA-10c
@CA-9c
@CA-9c
@CA-tbl
@CA-10c
@CA-9c

SNB macro fulfilled all the criteria except the 5th, 6th and 7th para concept.

Thanks,
Rakesh

snb
09-16-2014, 11:52 AM
Sub M_snb()
With ActiveDocument
sn = Filter(Split(.Content, vbCr), " ")
.Content = Join(sn, vbCr)

c00 = Val(Split(sn(0), "-")(1))
For j = 1 To UBound(sn) - 1
If c00 <> Val(Split(sn(j), "-")(1)) And c00 <> 0 And Val(Split(sn(j), "-")(1)) <> 0 Then
.Paragraphs(j + 1).Range.HighlightColorIndex = 3
c00 = Val(Split(sn(j), "-")(1))
End If
Next
End With
End Sub

Rakesh
09-16-2014, 12:53 PM
Hi snb,

Marvelous Its working Fine

I get, what I need?. You save me a week.

Once again thanks a lot for your valuable time spent for me