Range to be automatic as opposed to fixed

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,232
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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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
Thanks very much.
Now posted another question.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,125,002
Members
449,202
Latest member
Pertotal

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