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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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