Solved

Simple Add to a Macro for Limiting List

Posted on 2012-12-29
8
154 Views
Last Modified: 2012-12-31
EE Pros,

I have a Macro that automatically replicates lines in a WS.  I need it to stop by either a counter or a specific cell reference.  So for example, I'd like to be able to specify that it can increment a total of 10 times or until it reaches say, Cell A17.  The choices should be able to operate independently and I'll comment out the choice.

Thank you in advance.

B.


'This code adds a new Case to the end of the last case.

Sub NewCase()
Dim rngNewCase As Range
Dim cht As ChartObject
Dim rngCurrent As Range
Dim rngDesired As Range

ActiveSheet.Unprotect Password:="pass"
Application.DisplayAlerts = False

    Set rngNewCase = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)

   Worksheets("Risk_Return").Range("Priority_Case").Copy rngNewCase
   
   ChartNo = rngNewCase.Row / 3
   
    Set rngCurrent = rngNewCase.Offset(, 2)
    Set rngDesired = rngNewCase.Offset(, 3)
    ActiveSheet.Protect Password:="pass"
    ActiveSheet.EnableSelection = xlUnlockedCells
     
End Sub
0
Comment
Question by:Bright01
  • 4
  • 4
8 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38729189
The macro you have shown simply copies the range called priority_case to the activesheet below the last row.

I am not clear what you want to do now. Can you give an example?
0
 

Author Comment

by:Bright01
ID: 38729545
SSaqubh,

Thanks for the comment.  It does exactly that.  It simply copies and pastes new rows.  What I want it to do is to STOP after a certain number of replications (say 10) or STOP when it reaches a certain Cell row like 17.  I only want it to respond to one of the ways to stop it; I'll comment out the one I don't use so I can use it later if necessary (or you can comment it out and I'll know what it is).  

I've attached the actual WS for you.

Thank you again,

B.
Risk-Graphic-v4.xlsm
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38729603
Try this

Sub Prioritise()
Dim xLast_Row As Long
Dim getp As Variant
Sheets("Risk_Return").Activate
ActiveSheet.Unprotect Password:="jam"
getp = InputBox("Enter a number or a cell reference", "Priority list")
If IsNumeric(getp) Then
xLast_Row = 6 + getp
ElseIf getp = "" Then
    MsgBox ("Please enter something.")
    Exit Sub
Else
xLast_Row = Range(getp).Row
End If

With Sheets("Risk_Return").Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("E7:E" & xLast_Row), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    .SetRange Range("A6:E" & xLast_Row)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Range("A7") = "1"
Range("A7").AutoFill Destination:=Range("A7:A" & xLast_Row), Type:=xlFillSeries

End Sub

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

Author Comment

by:Bright01
ID: 38729749
Ssaqibh,

Thanks for the code!   The Sub you have modified is not the right Sub.  The Sub I'm trying to restrict the replication count is "Sub NewCase()"......the Prioritize Sub prioritizes the range when numbers are entered into the Value and Risk Columns.  What I'm trying to do is to have a message such as "You have exceeded the number of entries" when you press the "New!" button and you have exceeded EITHER 10 entries OR you have reached a particular Row, say row 17.  I tried the code for kicks but quickly realized I had probably not been clear enough as to what I was looking for.  I hope this is a simple two or three line entry in the NewCase Sub to do a count or identify a row number and stop adding additional rows.

Make sense?

B.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38730403
Sub NewCase()
Dim rngNewCase As Range
Dim cht As ChartObject
Dim rngCurrent As Range
Dim rngDesired As Range
Dim getp As Variant

ActiveSheet.Unprotect Password:="jam"
Application.DisplayAlerts = False

Sheets("Risk_Return").Activate
getp = InputBox("Enter a number or a cell reference", "Priority list")
If IsNumeric(getp) Then
xLast_Row = 6 + getp
ElseIf getp = "" Then
    MsgBox ("Please enter something.")
    Exit Sub
Else
xLast_Row = Range(getp).Row
End If
    Set rngNewCase = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)

   Worksheets("Risk_Return").Range("Priority_Case").Copy Range(rngNewCase, Range("A" & xLast_Row))
   
   ChartNo = rngNewCase.Row / 3
    
    Set rngCurrent = rngNewCase.Offset(, 2)
    Set rngDesired = rngNewCase.Offset(, 3)
    ActiveSheet.Protect Password:="jam"
    ActiveSheet.EnableSelection = xlUnlockedCells
     
End Sub

Open in new window

0
 

Author Comment

by:Bright01
ID: 38730678
Ssaquibh,

Thanks for the code.  I tested it but am still perplexed by what it does. It asks for a reference number.......then adds another line as did the original code.

If you go back to my original code, the original code works perfectly; EXCEPT if you fire the "Newcase" macro via the button, it will make copies beyond 10 entries or go beyond row 17.  Try it.  Hit the New! button until it replicates beyond row 17.  This is what I'm trying to limit.  I'm looking for it to stop at row 17, either by identifying the row or by counting to 10; with the message, "you have exceeded the number of entries permitted".

Make sense?  Thanks for hanging in with me.

B.
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 38731036
Sorry I did not comprehend that you wanted to add one row at a time till you reach the limit. Try this...I have not tested it.
Sub NewCase()
Dim rngNewCase As Range
Dim cht As ChartObject
Dim rngCurrent As Range
Dim rngDesired As Range

ActiveSheet.Unprotect Password:="pass"
Application.DisplayAlerts = False

    Set rngNewCase = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
if rngnewcase.row > 17 then             'To limit to A17               'comment out one of the two
'if rngnewcase.row - 6 > 10 then        'To limit to 10 rows       'comment out one of the two
    msgbox "you have exceeded the number of entries permitted"
    exit sub
endif


   Worksheets("Risk_Return").Range("Priority_Case").Copy rngNewCase
   
   ChartNo = rngNewCase.Row / 3
   
    Set rngCurrent = rngNewCase.Offset(, 2)
    Set rngDesired = rngNewCase.Offset(, 3)
    ActiveSheet.Protect Password:="pass"
    ActiveSheet.EnableSelection = xlUnlockedCells
     
End Sub 

Open in new window

0
 

Author Closing Comment

by:Bright01
ID: 38732388
Ssaquibh,

THANK YOU!!!!  This works perfectly.  I hope you had a great holiday season and are preparing for a fantastic New Year's Eve.

If you get a chance, I have another question out that hasn't been addressed since the 21st of Dec.  It was close to being complete but has querks.  It's simply trying to auto save a copy of  a range to the desktop.  I think it may be about 80% done but is stalled.  If you get the chance......  

It's under:  "Autosave Macro"

Thanks,

B.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
copy same as above data 18 41
Turn date into age 17 33
VBA working with shapes 6 11
COPYING ROW W/ CHECKBOX TO SEPARATE SHEET 3 26
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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