If Cell empty then condition - vba

I have a problem with my code. It does not recognize the Empty Cell.
The condition is "If the cell is empty then repeat, else change the font of the text in the cell above the current cell"
Sub lastprice()
Dim price As String
Do
Selection.Find.ClearFormatting
With Selection.Find.Font
.Size = 11
.Bold = True
End With
With Selection.Find
.Text = "last price ("
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Exit Do
Else
Selection.MoveRight Unit:=wdCell
price = Trim(Selection.Text)
If price <> "" Then ' I think there is some problem
Selection.MoveLeft Unit:=wdCell, Count:=2
Selection.Font.Name = "Cambria"
Selection.MoveDown Unit:=wdLine, Count:=1
Else
End If
End If
Loop
End Sub
For more details check
It should elaborate more by second image of table with conditions

I took the code in the debugger, which you should have done, and inspected what price was. It turns out it contains some kind of cell marker with character codes 13, 7. So an empty cell still in Word contains something. Change your if statement to the following:
If mid(price,1,1) <> Chr(13) Then

After Edited: #paul ogilvie
If mid(price,1,1) = Chr(13) Then
works for me, I change the logic <> to =
Here is working code
Sub lastprice()
Dim price As String
Do
Selection.Find.ClearFormatting
With Selection.Find.Font
.Size = 11
.Bold = True
End With
With Selection.Find
.Text = "last price ("
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Exit Do
Else
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
price = Trim(Selection.Text)
If Mid(price, 1, 1) = Chr(13) Then
Selection.MoveLeft Unit:=wdCell, Count:=2
Selection.Font.Name = "Cambria"
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveRight Unit:=wdCharacter, Count:=1
End If
End If
Loop
End Sub

Related

Looping through a Word Document and Replacing a String with PageBreak

I want to replace every occurrence of the string "#PAGEBREAK# with an actual pagebreak. This is what I came up with:
Sub InsertPageBreak()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#PAGEBREAK#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.InsertBreak Type:=wdPageBreak
.Execute
End With
End With
Application.ScreenUpdating = True
End Sub
What actually happens: The string "#PAGEBREAK#" is exchanged for an empty string. The .Find works as intended but I get the error message:
Method or Object not found
on the
.InsertBreak Type:= wdPageBreak
What methods could be used here in which way?
This will work For you:
Sub InsertPageBreak()
ActiveDocument.Range.Select
With Selection.Find
.Text = "#PAGEBREAK#"
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
End If
End Sub
If you want to Replace all of the "#PAGEBREAK#", use this below code:
Sub InsertPageBreak()
ActiveDocument.Range.Select
Do
With Selection.Find
.Text = "#PAGEBREAK#"
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
Else: Exit Sub
End If
Loop
End Sub

Select a particular sentence in range using find method and making it bold

Using following code firstly I make the selected paragraph as range and then I want to bold text from start of the paragraph till first sentence end which in my case it is ". "
How its is possible to select the first sentence in given range?
Dim CON As Range
selection.MoveDown Unit:=wdParagraph, COUNT:=1, Extend:=wdExtend
If selection.Range.ComputeStatistics(wdStatisticLines) < 3 Then
selection.Font.Bold = True
selection.MoveRight Unit:=wdCharacter, COUNT:=1
Else
selection.MoveLeft Unit:=wdCharacter, COUNT:=1
selection.ExtendMode = True
selection.EndKey Unit:=wdLine
selection.MoveDown Unit:=wdLine, COUNT:=2
Set CON = selection.Range
selection.ExtendMode = False
With CON.Find
.Text = ". "
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If CON.Find.Found Then
'' Now here I want to bold the sentence
else
end if
UPDATE
I set the code and now it can evaluation what it is found.
Set CON = selection.Range
selection.ExtendMode = False
Set conFind = CON.Duplicate
'''''''''''''>>''''''''''''
With conFind.Find
.Text = ">>"
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If conFind.Find.Found Then
CON.End = conFind.End
CON.Font.Bold = True
Else
'''''''''''''. ''''''''''''
With conFind.Find
.Text = "^?. "
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If conFind.Find.Found Then
If confind = "S. " Then
conFind.Find.Execute
If conFind.Find.found Then
CON.End = conFind.End
CON.Font.Bold = True
Else
End If
Else
CON.End = conFind.End
CON.Font.Bold = True
End If
Else
''''''''''''', ''''''''''''
With conFind.Find
.Text = ", "
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If conFind.Find.Found Then
CON.End = conFind.End
CON.Font.Bold = True
Else
End If
End If
End If
Declare an additional Range variable, for example
Dim conFind as Word.Range
then set it to be a duplicate of the original Range. Use this for the Find - if Find is successful conFind will be the found Range. Then set the End point of the original Range to the end point of the found Range and apply the bold formatting.
Note: I prefer to also create a boolean variable to hold the success of Find.Found, rather than testing Range.Find.Found as in my experience it's more reliable. I've left the code as you have it, however...
Set CON = selection.Range
selection.ExtendMode = False
Set conFIND = CON.Duplicate
With conFind.Find
.Text = ". "
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If conFind.Find.Found Then
CON.End = conFind.End
CON.Font.Bold = True
else
end if

Find text, Insert page break and repeat

How do I find a phrase, go to the beginning of the line the phrase is in, insert a page break, then execute the macro again.
I have attempted the following but it will not go to the following value of "Agent Name" upon subsequent execution.
Sub mFI()
' ' mFI Macro ' '
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdPageBreak, Count:=1
End Sub
assuming code you provided is correct, This will help you.
Sub mFI()
' ' mFI Macro ' '
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdPageBreak, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
IF you want to insert page break to all the occurrences of agent name you could try.
Sub Demo()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Agent Name"
.Replacement.Text = "^m^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Find text and format

I have recorded a macro in Word 2007 that finds a word, moves the cursor two lines up, inserts three '***', then highlights the line. It works on the first instance of the found word. I am struggling to get it to repeat throughout the document with all instances of the word I want it to find.
This is the output from my recorded macro. I need the actions to be repeated for each instance of "B,".
Sub HighlightNewItems()
'
' HighlightNewItems Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "B,"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="***"
Selection.TypeParagraph
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
Try putting the following construct within your With.Selection.Find
Do While .Execute
'(logic that you want to apply after finding string)
Loop
In your case, your code would look like
Sub HighlightNewItems()
'
' HighlightNewItems Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "B,"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="***"
Selection.TypeParagraph
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop
End With
End Sub

User Input Should be Replacement Text

I am working on a macro, it has 3 problems with it. Probably more depending on how experience a programmer you are. I am trying to get my code to do this 1 thing right now.
At the bottom of the code, I am trying to make the input box work so that what the user's input will be the replacement text for 'Assigned BK Specialist:'
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
End With
Sub Combined_Code()
'Condense Version
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String
Dim BkSpecName As String
For i = 1 To 2
With Selection.Find
.Text = "date received"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
End With
Selection.Find.Execute
Selection.Paste
Selection.MoveDown Unit:=wdLine, Count:=10
Next i
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
Selection.Find.Replacement.ClearFormatting
' Insert your name for the file.
With Selection.Find
.Text = "By:"
End With
Selection.Find.Execute
Selection.TypeText Text:="By: Robert Birch"
With Selection.Find
.Text = "Date Assigned:"
End With
' Insert current date on 3rd page.
Selection.Find.Execute
Selection.TypeText Text:="Date Assigned: "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
'Prompt
strPrompt = "What chapter is the MFR for?, NO = 13, Yes = 7"
'Dialog's Title
strTitle = "What Chapter Are You Working On?"
'Display MessageBox
iRet = MsgBox(strPrompt, vbYesNoCancel, strTitle, Yes = "Option1", No = "Option2")
'Check pressed button
If iRet = vbNo Then
MsgBox "Running Ch.13!"
' Added code for ch 13
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
For q = 1 To 7
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next q
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
Selection.MoveDown Unit:=wdLine, Count:=10
Selection.MoveRight Unit:=wdCell, Count:=2
For w = 1 To 9
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next w
Else
MsgBox "Running Ch.7!"
' Added coded for Ch.7
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
For b = 1 To 7
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
'modded
Next b
Selection.MoveDown Unit:=wdLine, Count:=10
For j = 1 To 3
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next j
Selection.TypeText Text:="n/a"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="n/a"
For o = 1 To 4
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="x"
Next o
End If
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.TypeText Text:="x"
'End part of program requires user input for BK spec's name
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
End With
Try it like this:
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue '<----- add that
End With