#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default Stop Macro

I need to write a macro that will look in one column (unit price) and if the
price is less than $1,000 or blank then stop, otherwise continue.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Stop Macro

Using column A as example, here's one way

Sub test()
Dim c As Range

For Each c In ActiveSheet.Range("A:A")
If c.Value < 1000 Then
MsgBox "stopped at " & c.Address
Exit Sub
End If
Next c

End Sub


--
Hope that helps.

Vergel Adriano


"Beep Beep" wrote:

I need to write a macro that will look in one column (unit price) and if the
price is less than $1,000 or blank then stop, otherwise continue.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default Stop Macro

Public Sub CheckPrices()
Dim rngUsedRange As Range
Dim rngUnitPrices As Range
Dim rngCell As Range

Set rngUsedRange = ActiveSheet.UsedRange

With rngUsedRange
'Define rngUnitPrices to be column $A data (without header cell).
Set rngUnitPrices = .Resize(.Rows.Count - 1, 1).Offset(1)
End With

For Each rngCell In rngUnitPrices
If IsEmpty(rngCell) Then GoTo StopCheck
If rngCell.Value < 1000 Then GoTo StopCheck
Next rngCell

MsgBox "All Unit Prices are OK.", _
vbInformation + vbOKOnly, _
"Check Prices"
Exit Sub

StopCheck:
rngCell.Activate
MsgBox "This Unit Price is blank or less than $1,000.", _
vbCritical + vbOKOnly, _
"Check Prices"
End Sub

--
Regards,
Bill Renaud



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default Stop Macro

Vergel and Bill:

Thanks for replying, however I have tried both and although both work to
some degree they both seem to find less than 1,000 when indeed there isn't
anything less than 1,000 and also the message says it is blank when indeed
there are no blanks.

Also can we do away with the message box and just stop at the appropriate
cell???

Thanks


"Vergel Adriano" wrote:

Using column A as example, here's one way

Sub test()
Dim c As Range

For Each c In ActiveSheet.Range("A:A")
If c.Value < 1000 Then
MsgBox "stopped at " & c.Address
Exit Sub
End If
Next c

End Sub


--
Hope that helps.

Vergel Adriano


"Beep Beep" wrote:

I need to write a macro that will look in one column (unit price) and if the
price is less than $1,000 or blank then stop, otherwise continue.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default Stop Macro

I rechecked my macro, and it works fine, as far as I can tell. Is your
"Unit Price" column in column $A, or a different column. I rewrote my
routine slightly to allow for "Unit Price" to be in any column (on row
1). Change the constant blnShowMsgBox at the top of the routine back to
True, if you later decide that you want to show the message boxes again.
Without showing a message box, it is difficult for a user to know if
something happened or not.

Actually, you might be better off just using Data Filter. You will have
to use 2 cases, though, to show only rows where "Unit Price" is <
$1,000, or where "Unit Price" is blank. (Cells where "Unit Price" are
blank will not be displayed when a custom filter is applied to show rows
where "Unit Price" is < $1,000.)

Public Sub CheckPrices()
Const blnShowMsgBox As Boolean = False
Dim wsData As Worksheet
Dim rngUsedRange As Range
Dim rngColumnLabels As Range
Dim rngUnitPrice As Range
Dim rngUnitPriceData As Range
Dim rngCell As Range

Set wsData = ActiveSheet
Set rngUsedRange = wsData.UsedRange

With rngUsedRange
'Assume Column Labels are in row 1.
Set rngColumnLabels = .Resize(1)
Set rngUnitPrice = rngColumnLabels.Find("Unit Price")

'Define rngUnitPrices to be column $A data (without header cell).
Set rngUnitPriceData = rngUnitPrice.Offset(1) _
.Resize(.Rows.Count - 1, 1)
End With

For Each rngCell In rngUnitPriceData
If IsEmpty(rngCell) Then GoTo StopCheck
If rngCell.Value < 1000 Then GoTo StopCheck
Next rngCell

If blnShowMsgBox _
Then
MsgBox "All Unit Prices are OK.", _
vbInformation + vbOKOnly, _
"Check Prices"
End If
Exit Sub

StopCheck:
rngCell.Activate
If blnShowMsgBox _
Then
MsgBox "This Unit Price is blank or less than $1,000.", _
vbCritical + vbOKOnly, _
"Check Prices"
End If
End Sub

--
Regards,
Bill Renaud



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to stop at a certain tab and does not go to the next one. [email protected] Excel Worksheet Functions 3 March 16th 06 01:11 AM
MSG Box - Stop Macro Janet H[_2_] Excel Programming 0 September 7th 05 01:16 AM
Stop running a macro in the middle of a macro gmunro Excel Programming 3 June 9th 05 06:00 PM
Macro: With Stop it works. Without Stop it doesn't. Don Wiss Excel Programming 2 October 12th 04 10:49 AM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 07:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"