Count up and down from active cell

KX13ZN

New Member
Joined
Sep 12, 2019
Messages
34
Office Version
  1. 365
  2. 2019
  3. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
I have macro that runs from an 'activecell' that copies the active row, macro included into the row right below it. Its a simple insert new row macro. Now my problem is i only need 16 continuous rows. After 16 rows i need a different event to trigger. But i can't figure out how to count 16 continuous rows when you can insert rows from any point in the 16 row continuum.
There are blank cells above and below the first instance of the active cell before the macro is run.

eg:

blank
macro <- insert macro can be run from this cell
blank

running the macro would make it:

blank
macro <- insert macro can be run from this cell
macro <- insert macro can be run from this cell
blank

So i need a row count to check above and below the active cell(macro) and if the total rows counted is higher than 16 then the macro doesn't run.

I've looked at count rows until blank but i can't figure out how the code works or how to adapt it to my needs

Please help, i would really appreciate some advice.

While it may not be needed the insert macros code is:

Code:
ActiveSheet.Unprotect
ActiveCell.EntireRow.Select
Selection.Copy
Selection.Insert Shift:=xlDown
ActiveSheet.Select
ActiveSheet.protect
ActiveSheet.EnableSelection = xlUnlockedCells
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I Solved it myself.... oh well if anyone finds this and has a similar issue this is what i did to get my answer, figured it was simple.

Code:
Sub Insert()


' Insert Macro
Dim u As Integer
Dim d As Integer
Dim up As Integer
Dim down As Integer
Dim limit As Integer
 
u = -1
d = 0
up = 0
down = 0


        Do While ActiveCell.Offset(u, 0).Value > 1
            u = u - 1
            up = up + 1
        Loop
        Do While ActiveCell.Offset(d, 0).Value > 1
            d = d + 1
            down = down + 1
        Loop
        
        limit = up + down


If limit < 16 Then
    ActiveSheet.Unprotect
    ActiveCell.Select
    ActiveCell.EntireRow.Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    ActiveSheet.Select
    ActiveSheet.protect
    ActiveSheet.EnableSelection = xlUnlockedCells
        Else
        MsgBox ("nope")
        On Error GoTo 0
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top