Macro Runs Even When Criteria not met! Excel Vba

Status
Not open for further replies.

theteerex

Board Regular
Joined
Mar 2, 2018
Messages
102
My code is supposed to run only if n > NumRows.
n is the number of rows in my worksheet. I calculate the number using a formula in another spreadsheet at location A1.
For the purposes of this post, that number is 30.

My code:
Code:
'Sheet where row number is calculated, 30 for this example
Private Sub Worksheet_Calculate()

    Dim n As Long
    n = Worksheets("TableSize").Range("A1").Value
    If n > NumRows Then Call MacroY 'MacroY is a sub that is called if there is an increase in rows, leading to a difference between n and NumRows

    NumRows = n

End Sub

Code:
Public NumRows As Long

Code:
'Located in the worksheet where rows are added. Currently at 30 rows as well
Private Sub Workbook_Open()

  NumRows = Worksheets("TableSize").Range("A1").Value

End Sub

The problem is, whenever I run ANY macro on the worksheet that can have rows added, my MacroY runs, adding more rows to a 3rd worksheet.
Am I missing something? To me, the code looks like it is telling MacroY to run only if n > NumRows. Am I wrong?
Is it the _Calculate?

Any help would be awesome!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Status
Not open for further replies.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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