Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Add to a Macro for Limiting List

Posted on 2012-12-29
8
Medium Priority
?
157 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
Industry Leaders: 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!

 

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

661 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