Reference a column with variable rows with vba

Rdhostetler

New Member
Joined
Feb 2, 2015
Messages
34
hello
i have some code that is supposed to look at a specified sheet and figure out how many active rows it has and then look at column D and check each cell in that column to see if any are over 55. if any cell is over 55 it will give a message box as you can see... with this code it is giving me the message box even though i dont have any over 55. any help would be appreciated.

thanks

Code:
Private Sub check_screen_width()


'check screen widths on order
Dim width As Long


numrows = Sheet17.Range("d1", Sheet17.Range("d1").End(xlDown)).Rows.Count


For width = 1 To numrows
Application.ScreenUpdating = True


If Sheet17.Cells(width, 4).Value >= 55 Then MsgBox "You have screens on the order above the 55 inch maximum width! Please manually open the screen order and change these screens to mulled screens with adapters", vbOKOnly, "Over Width Screens on this order!"
Application.ScreenUpdating = False




Next width






End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I only had errors with your code. (line 4, so I approachedit differently)

Try this ...
Code:
Private Sub check_screen_width()

'check screen widths on order
    Dim width As Long
    Dim numrows as integer

   numrows = ThisWorkbook.Worksheets("Sheet17").Cells(ThisWorkbook.Worksheets("Sheet17").Rows.Count, "D").End(xlUp).Row
   Application.ScreenUpdating = True
   For width = 1 To numrows
       If ThisWorkbook.Worksheets("Sheet17").Cells(width, 4).Value >= 55 Then 
             MsgBox "You have screens on the order above the 55 inch maximum width! Please manually open the screen order and change these screens to mulled screens with adapters", vbOKOnly, "Over Width Screens on this order!"
       End if
   Next Width
   Application.ScreenUpdating = False
End Sub.
 
Last edited:
Upvote 0
hello
i tried the code and i get a error saying it is out of range on this line
numrows = ThisWorkbook.Worksheets("Sheet17").Cells(ThisWorkbook.Worksheets("Sheet17").Rows.Count, "D").End(xlUp).Row
any idea why this would be?

thanks
 
Upvote 0
Are you sure that the values in column D are numbers rather than text?
 
Upvote 0
Ok, how about
Code:
Private Sub check_screen_width()
   Dim Wdth As Long
   
   Application.ScreenUpdating = True
   For Wdth = 2 To Sheet17.Range("D" & Rows.Count).End(xlUp).Row
      If Sheet17.Cells(Wdth, 4).Value >= 55 Then MsgBox "You have screens on the order above the 55 inch maximum width! Please manually open the screen order and change these screens to mulled screens with adapters", vbOKOnly, "Over Width Screens on this order!"
   Next Wdth
End Sub
You should avoid using VBA keywords (such as Width) as names of variables.
 
Upvote 0
Ok, how about
Code:
Private Sub check_screen_width()
   Dim Wdth As Long
   
   Application.ScreenUpdating = True
   For Wdth = 2 To Sheet17.Range("D" & Rows.Count).End(xlUp).Row
      If Sheet17.Cells(Wdth, 4).Value >= 55 Then MsgBox "You have screens on the order above the 55 inch maximum width! Please manually open the screen order and change these screens to mulled screens with adapters", vbOKOnly, "Over Width Screens on this order!"
   Next Wdth
End Sub
You should avoid using VBA keywords (such as Width) as names of variables.

Kudos to you! that works! thank you very much!!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,164
Members
448,870
Latest member
max_pedreira

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