To be honest, there are a couple of issues with this code that makes
it look kinda rough - first, the other folks are right, the error is
that you are ending your If statement prematurely.  When you build an
If statement in VB, you can create it as a single-line, or multi-line
if statement.

Single Line Syntax:
If someValue > someOtherValue then DoSomething()

Multi-line Syntax:
If someValue > someOtherValue then
     DoSomething()
End If

Only multi-line If statements support Else.

But even beyond that, your block of code uses what a lot of
programmers refer to as undesirable goto's.  This is where you avoid a
block of code by using a goto statement.  While it works, it's
generally considered an ugly code practice.  Your original block of
code might at least look a little better by doing this:

If Not(Range("D33").Value > Range("d42").Value) Then
    Range("D33").Select
    Selection.Copy
    Range("D42").Select
    ActiveSheet.Paste
    Range("C33").Select
    Selection.Copy
    Range("E42").Select
    ActiveSheet.Paste
End If

Or,

If Range("D33").Value >= Range("d42").Value) Then
    Range("D33").Select
    Selection.Copy
    Range("D42").Select
    ActiveSheet.Paste
    Range("C33").Select
    Selection.Copy
    Range("E42").Select
    ActiveSheet.Paste
End If

This way, you don't need the Goto, or even the Else at all - and get
the same desired result.

Good luck, hope this helps.

Pops


On Oct 3, 3:41 pm, JHB <[EMAIL PROTECTED]> wrote:
> I am totally new to Visual Basic, and am working on an Excel
> spreadsheet. I have a number of macros, and one that I cannot make
> work. It is as follows:
>
> Sub setlowest()
> '
> ' setlowest Macro
> ' Macro recorded 10/3/2008 by John
> '
> Sheets("Menu").Select
>
> If Range("D33").Value > Range("d42").ValueThen Then GoTo getout
> Else
>     Range("D33").Select
>     Selection.Copy
>     Range("D42").Select
>     ActiveSheet.Paste
>     Range("C33").Select
>     Selection.Copy
>     Range("E42").Select
>     ActiveSheet.Paste
> End If
> getout:
> End Sub
>
> I get an error that I have an "else" statement  without an "if". I can
> see the "if" there but its clear that there is something I have not
> done that I should have done. Can someone tell me what I need to do to
> make this macro work, please.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web 
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/DotNetDevelopment

You may subscribe to group Feeds using a RSS Feed Reader to stay upto date 
using following url  

<a href="http://feeds.feedburner.com/DotNetDevelopment";> 
http://feeds.feedburner.com/DotNetDevelopment</a>
-~----------~----~----~----~------~----~------~--~---

Reply via email to