VBA - How to run code if only a certain number of rows are present

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I feel like this should be simple, but I don't know the language needed. In Excel 2010, I need VBA code to run according to the number of rows. Rows will always be grouped together at the top of the sheet, so there will be no blank rows in the middle of the data. Something like this:

create R
R = # of rows with with any populated fields

If R = 1, then run code1
If R = 2, then run code2
If R > 3, then run code3

(R will never = 0)

This problem arose from an AutoFill command running into trouble when there are not enough rows to satisfy a
Code:
[INDENT]Selection.AutoFill Destination:=Range("J3:L" & Range("A3").End(xlDown).Row)
[/INDENT]
type instruction.


Thanks!
PS. I see my user signature is out of date mentioning 2007 on XP. I'm running 2010 on Win 7.
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try like this

Code:
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Select Case LR
    Case 1: Call code1
    Case 2: Call code2
    Case Else: Call code3
End Select

You can edit your signature ;)
 
Upvote 0
Would this work?
Code:
Dim Row_Count as Long

Row_Count = Range("A" & Rows.Count).End(xlUp).Row

Select Case Row_Count
  Case Is = 1
     ' Your code for code1
  Case Is 2
     ' Your code for code2
  Case Is >= 3
     ' Your code for code3
  Case Else
     ' Your code for anything else
End Select
 
Upvote 0
Thanks for the quick reply.

Do I need to set up code1, code2, etc. as separate subs or something to call them? Or can I just insert the code for "code1", etc. in your example? Here's an example of one of the codes:
Code:
Range("J3").Select
        ActiveCell.FormulaR1C1 = "=IF(RC[-9]=R[-1]C[-9],RC[-7]+R[-1]C,RC[-7])"
Range("K3").Select
        ActiveCell.FormulaR1C1 = "=IF(RC[-10]=R[-1]C[-10],RC[-7]+R[-1]C,RC[-7])"
If I can keep it all in the same Sub, I'd prefer that.

Signature edited. Check.
 
Upvote 0
Thanks, JackDanIce. I think that's what I'm looking for. I'll test and get back.

Tanqueray and Jack Daniels - two of my favorites. Thanks guys.
 
Upvote 0
That would be something like

Code:
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Select Case LR
    Case 1: Range("J3").FormulaR1C1 = "=IF(RC[-9]=R[-1]C[-9],RC[-7]+R[-1]C,RC[-7])"
    Case 2: Range("K3").Formula = "=IF(RC[-10]=R[-1]C[-10],RC[-7]+R[-1]C,RC[-7])"
    Case Else: 'something else
End Select
 
Upvote 0
Thank you both very much. I'm off and running!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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