+ Reply to Thread
Results 1 to 4 of 4

IF statement not working as expected?

Hybrid View

Aland2929 IF statement not working as... 07-30-2009, 06:31 AM
Phil_V Re: IF statement not working... 07-30-2009, 06:42 AM
romperstomper Re: IF statement not working... 07-30-2009, 06:49 AM
Aland2929 Re: IF statement not working... 07-30-2009, 07:03 AM
  1. #1
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    IF statement not working as expected?

    Hi All,

    I have two worksheets named "Data" and "Product". I want to use the following macro to sort the records depending which sheet is active. If I am in the "Data" sheet, it does the sort in the "Product" sheet? I was expecting it to ignore the second IF statement when I am in the "Data" sheet? Please help.

    Sub RegionSort()
    '   Sort by Region
     Application.ScreenUpdating = False
     If Worksheets("Data").Visible Then
        Application.Goto Reference:="Data"
     End If
     If Worksheets("Product").Visible Then
        Application.Goto Reference:="Data2"
     End If
        Selection.Sort Key1:=Range("B4"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        Range("A4").Select
     Application.ScreenUpdating = True
    End Sub
    Thank You
    Last edited by Aland2929; 07-30-2009 at 07:03 AM.

  2. #2
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: IF statement not working as expected?

    You are testing to see if the sheets are visible, which is not the same as being active.
    Both sheets ARE visible, (ie. not Hidden), so both 'IF' statements will run.
    Try testing to see which sheet the activesheet is instead.
    Change:
     If Worksheets("Data").Visible Then
    To be:
     If ActiveSheet.Name = "Data" Then
    Obviously you need to change the other IF statement as well
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: IF statement not working as expected?

    It would be even easier if you named the range on each sheet 'Data' - then you could just sort the Data range on the active sheet.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Smile Re: IF statement not working as expected?

    Thanks romperstomper. It now works perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1