?
Solved

Writing a If loop

Posted on 2014-10-30
6
Medium Priority
?
113 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 2000 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

765 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