Solved

Writing a If loop

Posted on 2014-10-30
6
107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

726 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