Solved

Writing a If loop

Posted on 2014-10-30
6
106 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 81

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 81

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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