LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 183
Default Declaring Objects

I hope this post makes sense. I am trying to learn VB on my own and I know
that I need to declare my objects and know that I would prefer to declare
them early in the code.

I want to write a macro that does a find and replace for "Green", "Yellow",
"Red", and "No" on Sheet1 and replaces the words with the values 1, 2, 3, 4,
respectively. The range of my data is from B1:Q400 and S1:AI400. I named
the non-contiguous range "Colors". I would like to use the name of my range
in my code.

Below is my first attempt to writing a macro, partially using the macro
recorder. The code the recorder gave me was:

Sub FindAndReplace()

Selection.Replace What:="green", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Selection.Replace What:="yellow", Replacement:="2", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Selection.Replace What:="red", Replacement:="3", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Selection.Replace What:="no", Replacement:="4", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False

End Sub

Since I don't want the user to have to select the range each time to run the
macro, I want to change the code to apply to the range I named "Colors". So
here's my first attempt at declaring my "Colors" range as my object. Except,
since I don't really know what I'm doing, it doesn't work.

Can anyone help me declare my range "Colors"?

Sub FindAndReplace()

Dim Colors As Range
Set Colors = Colors.Range("B1:Q400", "S1:AI400")

Colors.Replace What:="green", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Colors.Replace What:="yellow", Replacement:="2", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Colors.Replace What:="red", Replacement:="3", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Colors.Replace What:="no", Replacement:="4", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End Sub

Thanks so much,

Sharon

 
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
Dynamically Assign Objects to Form Objects. The Vision Thing Excel Programming 2 December 11th 04 04:02 PM
Passing, Declaring, and Assigning Objects tfrank Excel Programming 0 November 1st 04 06:45 PM
Unable to remove Sheet objects in the Microsoft Excel Objects Adrian[_7_] Excel Programming 1 August 26th 04 10:49 PM
declaring a msgbox Tracey[_6_] Excel Programming 2 May 12th 04 04:43 PM
Declaring a DLL Erich Neuwirth Excel Programming 1 May 10th 04 06:27 PM


All times are GMT +1. The time now is 09:45 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"