Range to be automatic as opposed to fixed

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,738
Office Version
  1. 2007
Platform
  1. Windows
Hi,

How can i change this existing working code so it isnt a fixed range.
I get easily confused each time with the xlup code for which it needs,sorry



Rich (BB code):
Private Sub UserForm_Initialize()
Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "HONDA MC KEY #1")
Me.TextBox2 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "HONDA MC KEY #2")
Me.TextBox3 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "HONDA MC KEY #3")
Me.TextBox4 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "HONDA MC KEY #4")
Me.TextBox5 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "REMOTE FOB 3B UK")
Me.TextBox6 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "YAMAHA YH35")
Me.TextBox7 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "BUNDLE")
Me.TextBox8 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "BLACK")
Me.TextBox9 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "GREY")
Me.TextBox10 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "RED")
Me.TextBox11 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "CLEAR")
Me.TextBox13 = Application.WorksheetFunction.CountIf(ws.Range("$A$8:$K$200"), "REMOTE FOB 3B USA")

Me.CommandButton1.SetFocus

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Here is one way:
VBA Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Dim lr As Long
Dim rng As Range

Set ws = ActiveSheet

'Find last row with data on column K of sheet
lr = ws.Range("K" & Rows.Count).End(xlUp).Row

'Set range variable
Set rng = ws.Range("A8:K" & lr)

Me.TextBox1 = Application.WorksheetFunction.CountIf(rng, "HONDA MC KEY #1")
Me.TextBox2 = Application.WorksheetFunction.CountIf(rng, "HONDA MC KEY #2")
Me.TextBox3 = Application.WorksheetFunction.CountIf(rng, "HONDA MC KEY #3")
Me.TextBox4 = Application.WorksheetFunction.CountIf(rng, "HONDA MC KEY #4")
Me.TextBox5 = Application.WorksheetFunction.CountIf(rng, "REMOTE FOB 3B UK")
Me.TextBox6 = Application.WorksheetFunction.CountIf(rng, "YAMAHA YH35")
Me.TextBox7 = Application.WorksheetFunction.CountIf(rng, "BUNDLE")
Me.TextBox8 = Application.WorksheetFunction.CountIf(rng, "BLACK")
Me.TextBox9 = Application.WorksheetFunction.CountIf(rng, "GREY")
Me.TextBox10 = Application.WorksheetFunction.CountIf(rng, "RED")
Me.TextBox11 = Application.WorksheetFunction.CountIf(rng, "CLEAR")
Me.TextBox13 = Application.WorksheetFunction.CountIf(rng, "REMOTE FOB 3B USA")

Me.CommandButton1.SetFocus

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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