Solved

Writing a If loop

Posted on 2014-10-30
6
103 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

863 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

21 Experts available now in Live!

Get 1:1 Help Now