Macro help - interesting problem

Dan Wilson

Well-known Member
Joined
Feb 5, 2006
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Good day. I am running Excel out of Office365 (updated) on Windows 10 Home version (updated). I have a worksheet that contains over 4000 rows of data. Column J will contain a two-digit number identifying a year. For example, 60 will idicate1960, 70 will indicate 1970. As the worksheet is used, Column J of each Row will be filled in according to the year of the data being used from that Row. The Top Row of the worksheet will be Frozen and I will place a Command Button in Column J. of Row A I would like to create a Macro to be assigned to the Command Button that will, on demand, examine the entire worksheet to Count the number of times that a particular two-digit year has been entered in Column J. The result of the search can be placed in Column K of Row A. I can create most of the Macro, but I have no idea how to make the Macro allow me to enter a specific two-digit number to search for in Column J while the Macro is running. Obviously, the two-digit entry will be different each time the Macro is run. I will appreciate any help with this.
Thank you,
Dan Wilson...
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
try this, it checks that a number is entered so if you enter characters is asks for a number, it also check that the number is between 0 a 99
VBA Code:
Sub input2nums()
Dim tt As Integer
On Error GoTo typeerror
 tt = InputBox(prompt:="Enter a two digit number", Title:="Year Selection")
If tt > 99 Or tt < 0 Then
 MsgBox ("Invalid number")
End If
MsgBox tt                 ' this is just to demonstrate the result. 
Exit Sub
typeerror:
 MsgBox ("Please enter a number!!")
End Sub
 
Upvote 0
I have no idea how to make the Macro allow me to enter a specific two-digit number to search for in Column J while the Macro is running.
Input box function? You could type it in or select a range.
 
Upvote 0
Good day offthelip. Thank you for responding to my request. What you gave me allows me to enter the two-digit year, but it does not include searching for the sum of the two-digit years found in in Column J. I have resolved the issue by entering a COUNTIF function in Cell J1 to do a sum using K1 as the value to search for. I will change K1 as necessary. It looks like I'm not as good with Macros as I thought I was. If you want to add the missing search element to the Macro that you sent, I would appreciate knowing how to do that.
Thank you,
Dan Wilson...
 
Upvote 0
Good day Micron. Thank you for responding to my request. I tried using the INPUTBOX function, but I'm not having any luck with it.
Dan Wilson...
 
Upvote 0
The way I would do it is like this:
VBA Code:
Sub input2nums()
Dim tt As Integer
On Error GoTo typeerror
 tt = InputBox(prompt:="Enter a two digit number", Title:="Year Selection")
If tt > 99 Or tt < 0 Then
 MsgBox ("Invalid number")
 Exit Sub
End If
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
cnt = Application.WorksheetFunction.CountIf(Range("J1:J" & LastRow), tt)
MsgBox cnt
Exit Sub
typeerror:
 MsgBox ("Please enter a number!!")
End Sub
 
Upvote 0
Good day again offthelip. Thank you for following up. It took me a while to understand what was going on, but I figured it out. The biggest thing I learned was how to include a function (like COUNTIF) into a Macro to make it work. I took out the elements for the LastRow, Dimensioned CNT as an Integer and all is well.
Thank you,
Dan Wilson...
 
Upvote 0
You commented that you dimensioned cnt as an integer. You might think that that I just forgot to dimension it, however I deliberately didn't declare it and I will explain why:
I never use option explicit and only add a dimension statement when it is really necessary. Leaving cnt undeclared means it defaults to being a variant which works perfectly well, and in this code the countif produces an integer while the msgbox I used to display it requires a string, so really it should be a variant. However Vba sorts it out if you declare it as a long or an integer
However this code is one example of where the dim statement I did put in makes a difference to the functionality.
I put the statement :
VBA Code:
Dim tt As Integer
I put this in deliberately because this means that if you type in a letter into the inputbox it will generate a type mismatch error and so the the line:
VBA Code:
 tt = InputBox(prompt:="Enter a two digit number", Title:="Year Selection")
goes straight to:
VBA Code:
typeerror:
 MsgBox ("Please enter a number!!")
Which gives the correct response for the input.
If I had left the dim statement out then tt would default to a variant and so the input box would not throw an error and the code go on to the next line and check whether tt was between 0 and 99 which would fail so to :
VBA Code:
If tt > 99 Or tt < 0 Then
 MsgBox ("Invalid number")
so you get " Invalid Number" message which isn't quite correct.
The reason I am writing all of this is make you aware of one of my complaints about using option explicit which forces you to declare every variable, is that most code ends up with lines and lines of declaration most of which tell the user nothing and can often hide the one declaration that really matters.
If I came back to look at this code where I have put one declaration in six months time, the one declaration I put in would stand out to me and immediately trigger a question in my mind: why did need to declare that variable there must someting special about it. ( and there is it changes the functionality)
So I favour only declaring variables when the excel default declaration doesn't work or would change the way it works. This means understanding the default declarations that excel makes.
I think it makes code much easier to read and it certainly saves on typing loads of rows of redundant repetitive code.
 
Upvote 0
Good day again offthelip. Thank you for the explanation. I appreciate the extra help. All I know is that without dimensioning the CNT, all I ended up with was a "0" in the msgbox instead of the count of the two-digit entry. I will keep rereading your entry and playing with it until I understand it better. It's good to have the explanation.
Thank you,
Dan Wilson...
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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