Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Kay Kay is offline
external usenet poster
 
Posts: 129
Default IMPORTANT!Conditionally formatting just 2 of many characters in a

Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default IMPORTANT!Conditionally formatting just 2 of many characters in a

maybe something like will work. i used column A on sheet1 in this example

Option Explicit

Sub test()
Dim i As Long
Dim lastrow As Long
Dim startChar As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To lastrow
startChar = InStr(1, ws.Range("A" & i).Value, " eg", vbTextCompare)
If startChar 1 Then
With ws.Range("A" & i)
.Characters(startChar + 1, 2).Font.Bold = True
End With
End If
Next

End Sub


--


Gary


"Kay" wrote in message
...
Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default IMPORTANT!Conditionally formatting just 2 of many characters in a

Hi Kay,

The following macro searches the entire used area of the worksheet for the
character string and formats only the specific characters being searched.

After the formatting line:-
..Color = vbRed

you can add other formatting like:-
..Bold = True

Sub Format_Text()
Dim strToFind As String
Dim lngTofind As Long
Dim rngUsed As Range
Dim foundCell As Range
Dim startPos As Long
Dim firstAddress As String

strToFind = " eg" 'Set to required string

lngTofind = Len(strToFind)

With Sheets("Sheet1") 'Edit for your sheet name
Set rngUsed = .UsedRange
End With

With rngUsed
Set foundCell = .Find(What:=strToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundCell Is Nothing Then
firstAddress = foundCell.Address

Do
startPos = InStr(1, foundCell, strToFind)

With foundCell.Characters(Start:=startPos, _
Length:=lngTofind).Font
.Color = vbRed
End With
Set foundCell = .FindNext(foundCell)
Loop While Not foundCell Is Nothing And _
foundCell.Address < firstAddress
End If
End With
End Sub

--
Regards,

OssieMac


"Kay" wrote:

Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
Kay Kay is offline
external usenet poster
 
Posts: 129
Default IMPORTANT!Conditionally formatting just 2 of many characters in a

Thanks to both who responded. I was actually able to get the test macro to
work, but the second macro seemed to find only the first t wo characters of a
string. But, since I am very weak in code, I probably did somsething wrong

Is the coding the same for access?

"Kay" wrote:

Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default IMPORTANT!Conditionally formatting just 2 of many characters i

Hi Kay,

Do I interpret your comment to mean that " eg" can appear more than once in
the cells? If so, then the following will fix it although you probably do not
need it now if Gary's macro did the job.

In answer to your question "Is the coding the same for access?" I would say
that the answer is No. However, the principles of VBA remain the same and
there are a lot of similarities between them but for every similarity there
in another unique in each application.

Sub Format_Text()
Dim strToFind As String
Dim lngTofind As Long
Dim rngUsed As Range
Dim foundCell As Range
Dim startPos As Long
Dim firstAddress As String
Dim i As Long

strToFind = " eg" 'Set to required string

lngTofind = Len(strToFind)

With Sheets("Sheet1") 'Edit for your sheet name
Set rngUsed = .UsedRange
End With

With rngUsed
Set foundCell = .Find(What:=strToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundCell Is Nothing Then
firstAddress = foundCell.Address

Do
For i = 1 To Len(foundCell)
startPos = InStr(i, foundCell, strToFind)
If startPos 0 Then
With foundCell.Characters(Start:=startPos, _
Length:=lngTofind).Font
.Color = vbRed
'.Bold = True 'Other formatting if required
End With
End If
Next i
Set foundCell = .FindNext(foundCell)
Loop While Not foundCell Is Nothing And _
foundCell.Address < firstAddress
End If
End With
End Sub


--
Regards,

OssieMac


"Kay" wrote:

Thanks to both who responded. I was actually able to get the test macro to
work, but the second macro seemed to find only the first t wo characters of a
string. But, since I am very weak in code, I probably did somsething wrong

Is the coding the same for access?

"Kay" wrote:

Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
Kay Kay is offline
external usenet poster
 
Posts: 129
Default IMPORTANT!Conditionally formatting just 2 of many characters in a

Ossie Mac,

Thanks so much for you help. I like the loop because it is possible that
the eg will occur more than once in a cell.

You guys are the greatest!

Kay

"Kay" wrote:

Hello,

Of course, all are aware that conditional formats are specific to a cell
using the format option in Excel. Can code be written that will search for
the characters " eg" with a space before and format only those characters?
The client is adamant and it takes forever to search for the characters and
then format manually.

Hope someone will help!

Thanks

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally formatting Art Excel Worksheet Functions 3 February 28th 10 05:08 AM
conditionally formatting comments LindaJane Excel Discussion (Misc queries) 1 September 8th 08 06:51 PM
Finding Characters Conditionally - Easy Question Rothman Excel Worksheet Functions 4 August 30th 06 02:24 AM
Conditionally Formatting phmckeever Excel Worksheet Functions 1 August 25th 06 03:00 PM
Conditionally formatting Mark[_50_] Excel Programming 1 May 22nd 06 03:56 PM


All times are GMT +1. The time now is 12:53 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"