Run One Macro or Another From Worksheet Button

SSP

New Member
Joined
Mar 5, 2009
Messages
2
I hope someone can help.

I have a spreadsheet (don't we all) that contains X number of rows, on the sheet I want to have a single button to extract the rows as .csv.

In essence what I want to happen when the button is clicked is for Macro A to run if there are less than X rows and Macro B to run if there are X rows or more. In my instance X will be 2.

Obvoiously this is easy of one uses 2 buttons but that isn't neat enough for my solution.

Cheers,

Matt
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
Matt

Why do you need 2 macros?

Why not just check for the number of rows in the command button click event and then act accordingly?
 
Upvote 0

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Hi Matt,

Welcome to the board.

Here is a suggestion to use your two macros:
Code:
Sub RowCheck()
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    If LastRow < 2 Then
        Call MacroA
    Else
        Call MacroB
    End If
End Sub
You can assign this code to your existing button, or, as Norie suggests, incorporate checking the row count and your macros "A" and "B", into the button's event code.
 
Upvote 0

SSP

New Member
Joined
Mar 5, 2009
Messages
2
Many thanks for the suggestions I'l give both a try to increase my knowledge.

Cheers,

Matt
 
Upvote 0

Forum statistics

Threads
1,195,596
Messages
6,010,639
Members
441,558
Latest member
lambierules

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
Top