Solved

Writing a If loop

Posted on 2014-10-30
6
101 Views
Last Modified: 2014-10-30
To show how bad I am at writing loops here is something I wrote.  I almost wish I was never shown the GoTo command.

I know that there is a different way to write this, like maybe a Do Unitl??
How would I rewrite this with less lines and not having to use the Goto commands?


    Range("BB2").Select
400

    If ActiveCell = "" Then
      ActiveCell = SalesOrderNum
      GoTo 500
    End If
    
    If Not ActiveCell = "" Then
        ActiveCell.Offset(1, 0).Select
        GoTo 400
     'MsgBox ("it is NOT blank ")
    End If
500

Open in new window

0
Comment
Question by:RWayneH
6 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40413499
Range("BB2").Select

Do until ActiveCell = ""
     ActiveCell.Offset(1, 0).Select
loop

ActiveCell = SalesOrderNum
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40413503
A better version would be:

introw = 2

Do until cells(introw,54)=""
    introw=introw+1
Loop

cells(introw,54)=SalesOrderNum
0
 
LVL 80

Expert Comment

by:byundt
ID: 40413520
An If block can have an Else clause. And a Do loop can have an Exit Do statement. The combination allows you to avoid needing line numbers.
Sub test3()
Dim SalesOrderNum As Variant
Range("BB2").Select
Do
    If ActiveCell.Value = "" Then
        ActiveCell.Value = SalesOrderNum
        Exit Do
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Loop
End Sub

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 80

Expert Comment

by:byundt
ID: 40413525
Alternatively, you can use the .End method to go straight to the bottom of your data:
Sub test2()
Dim SalesOrderNum As Variant
Range("BB3").Select
If Range("BB3").Value <> "" Then Range("BB2").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = SalesOrderNum
End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40413922
Can you explain what you are trying to actually do with your code?  It appears you're setting the SalesOrderNum variable to blank.  

If you really want to set the SalesOrderNum to the last value in column BB, then this code is significantly simpler:
Sub Set_SON()
    Dim SalesOrderNum as String
    SalesOrderNum = Range("BB" & Cells.SpecialCells(xlLastCell).Row).End(xlUp).Value
End Sub

Open in new window


This moves up "from the bottom" in case there are blank cells in column BB.

Regards,
-Glenn
0
 

Author Closing Comment

by:RWayneH
ID: 40414333
Thanks for the help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now