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

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
578
Office Version
2016
Platform
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
 

Jan Mach

New Member
Joined
Dec 8, 2017
Messages
20
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]
 

Forum statistics

Threads
1,081,545
Messages
5,359,444
Members
400,528
Latest member
Ratish52

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top