Solved

Simple Add to a Macro for Limiting List

Posted on 2012-12-29
8
151 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formula 6 47
Easy Excel formula needed 4 27
Problem with Excel and File Size 7 33
Problem counting secound table of filtered data. 9 26
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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.

895 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

11 Experts available now in Live!

Get 1:1 Help Now