VBA Help - Select Case with Greater than or Less than Statement

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hi Guys,

Working on a piece of code and not getting the desired results. My code is looking in column C for a specific cell with text and giving me a row number, I am using the row number to dynamically set how many pages to make the worksheet print too (i.e small amount of data print to 1 sheet, large amount print to more than 1 sheet)

Currently my Foundcell.Row line returns: 205 and my case statement should have the variable: 3 appear in my messagebox but it keeps showing: 2.

I feel like there may be something wrong with my case greater than or less than statements, any help getting this sorted is appreciated!

Here is my code:
Code:
Sub PrintAreaByACT()


Dim lr As Long
Dim FoundCell As Range
Dim ws As Worksheet
Dim cL As Variant, PageCount As Variant 'Finds Column Letter
Dim PageV As Long


Application.ScreenUpdating = False
Application.DisplayAlerts = False


Set ws = ActiveSheet
   
 Const Searchtext As String = "Total Expense" 'Finds text "Total Expense" and updates Print Area below that row
     Set FoundCell = ws.Range("C:C").Find(What:=Searchtext)
 
 
 Select Case FoundCell.Row
 
 Case Is <= 60
    PageCount = 1
 
 Case Is >= 61 <= 130
    PageCount = 2
    
Case Is >= 131 <= 300
PageCount = 3
 
Case Is >= 301 <= 450
PageCount = 4
 
 End Select
 
 MsgBox PageCount
end sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
MUCH better is:

Code:
Dim PageCount& 'The Long type of course, NOT variant!
Select Case FoundCell.Row
Case Is <= 60: PageCount = 1
Case Is <= 130: PageCount = 2 'No need of [COLOR=#333333][FONT=monospace]Is >= 61, numbers less than 61 are used in the previous Case!
[/FONT][/COLOR]Case Is <= 300: PageCount = 3
Case Is <= 450: PageCount = 4
End Select

Your construction
Case Is >= 61 <= 130
means
<strike></strike>
Case Is >= (61 <= 130)
what means
Case Is >= -1
because the BOOLEAN statement 61<=130 gives the value True = -1.
If you need two conditions in one case statement, you must separate them with a comma:
Case Is >= 61, Is <= 130

But the BEST solution of your needlessly complicated select-case statement is pretty simple:

Code:
Dim [COLOR=#333333][FONT=monospace]PageCount[COLOR=#333333][FONT=monospace]&, [/FONT][/COLOR]i[COLOR=#333333][FONT=monospace]&
[/FONT][/COLOR][/FONT][/COLOR][LEFT][COLOR=#333333][FONT=monospace][LEFT][COLOR=#333333][FONT=monospace]i = [COLOR=#333333][FONT=monospace]FoundCell.Row
[/FONT][/COLOR]If [COLOR=#333333][FONT=monospace]i[/FONT][/COLOR][COLOR=#333333][FONT=monospace] <= 450[/FONT][/COLOR] Then PageCount = 4 [COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 300) [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 130) [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 60)
[/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR]

[/FONT][/COLOR][/LEFT]
If there are no numbers greater than 450 in your data, then of course

Code:
[COLOR=#333333][FONT=monospace]Dim [/FONT][/COLOR][COLOR=#333333][FONT=monospace]PageCount[COLOR=#333333][FONT=monospace]&, [/FONT][/COLOR]i[COLOR=#333333][FONT=monospace]&
[/FONT][/COLOR][LEFT][COLOR=#333333][FONT=monospace]i = [COLOR=#333333][FONT=monospace]FoundCell.Row
[/FONT][/COLOR]PageCount = 4 [COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 300) [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 130) [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]+ (i [/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace][COLOR=#333333][FONT=monospace]<= 60)
[/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][/COLOR]
[/FONT][/COLOR]​
[/FONT][/COLOR][/LEFT]
<strike></strike>

[/FONT][/COLOR][/LEFT]
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

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