View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GB GB is offline
external usenet poster
 
Posts: 230
Default IF statements in Macros

At this moment, I can only answer the first question, the second would
require me to spend some time that I unfortunately can not offer at this
moment:

If statements are a part of a subroutine or a function
so need to have something like:
public sub XYZ()
or
public function XYZ()
or substitute private for public as desired

then after that statement and before a corresponding end (I.e., end sub or
end function)

you can use if statements like this:

if X = Y then

end if

The above is a single if statement that has a clear start and clear end.

In the situation you are discussing where you are looking at the first few
characters of a string for your replacement then you could do something like
this:

if X = Y then
'Do the comparisons/replacements desired
elseif X = Z then
'Do the comparisons/replacements desired
elseif X = AA then
'Do the comparisons/replacements desired
end if

As for the identification that a string begins with desired text then you
could do something like:

if left(LongString,len(ShortString)) = ShortString then

end if

or you may have to use a strcomp function such as:

if strcomp(left(LongString, len(ShortString)), ShortString) = 0 then

end if

the = 0 means that both are equal to each other. I don't recall which is
the case but a positive number means either that the first is greater than
the second or it may mean less than, and a negative number means the reverse.
Text comparisons include case, so if you the replacement of abc with xyz is
acceptable if it starts with ABC and would be replaced by xyz then you can
use the UCASE() or LCASE() function as desired for comparison:

if strcomp(UCASE(left(LongString, len(ShortString))), UCASE(ShortString)) =
0 then

end if


"Zak" wrote:

How do i put in IF statements in macros? i know what i want the macro to do
but cant seem to get the terminology correct, also can i put in numerous IF
statements within the same macro or do i need to create seperate modules?

an example: i want the macro to recognise that IF a cell begins with the
word GTS then it should replace it so it is only GTS. i also want to have IF
statments that say IF something = this then it should change to this. or IF
something = this then it should copy this or paste that.

i know what i want but dont know how to get it.

also, on a different note i am trying to create a macro that will build a
report based on a big spreadsheet. what is the best way to get around this as
at the moment i am just using the record macro option to record while i copy
and paste, hide etc cells to view just what is required for the report! then
format it to look nice, is there a better way to do this?

thanks in advance.