Solved

Simple Add to a Macro for Limiting List

Posted on 2012-12-29
8
150 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
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 Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now