Solved

Simple Add to a Macro for Limiting List

Posted on 2012-12-29
8
155 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vlookup Help 3 29
Embed XL WS and Objects into a PPTX 8 34
need count any combinaton of 4 numbers 7 25
Problem to macro 5 21
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

734 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