Making a command button dynamic

WcCannons

New Member
Joined
Dec 20, 2018
Messages
8
Hi everyone,

I'm fairly good with google and fairly beginner with VBA. I've found a VBA command to return a "no data" message in a row I've assigned to allow customers I am sending this file a quick check on the validity of the data. I basically want "no blanks" back from the customer in predefined columns.

However I do not know enough to make my search range dynamic, it's currently set to a dumb "4999" value because 4500 rows is the largest data set I've seen so far. However we have some smaller customers that may only give us 900 rows of data. I want the tool to be smart enough to find the last row of data and set that as the limit. Is there a way to either automatically make this happen, or add one more button to "find" the last row and insert that as the upper limit to the range in the macro?

For reference, this is what I currently have:

Private Sub CommandButton1_Click()


With Application.WorksheetFunction
If .CountA(Range("A14:A4999")) <= 0 Then [A13] = "No Data"
If .CountA(Range("B14:B4999")) <= 0 Then [B13] = "No Data"
If .CountA(Range("C14:C4999")) <= 0 Then [C13] = "No Data"
If .CountA(Range("D14:D4999")) <= 0 Then [D13] = "No Data"
If .CountA(Range("E14:E4999")) <= 0 Then [E13] = "No Data"
If .CountA(Range("F15:F4999")) <= 0 Then [F13] = "No Data"
If .CountA(Range("G15:G4999")) <= 0 Then [G13] = "No Data"
If .CountA(Range("H15:H4999")) <= 0 Then [H13] = "No Data"
If .CountA(Range("I15:I4999")) <= 0 Then [I13] = "No Data"
If .CountA(Range("J15:J4999")) <= 0 Then [J13] = "No Data"
If .CountA(Range("O15:O4999")) <= 0 Then [O13] = "No Data"
If .CountA(Range("P15:P4999")) <= 0 Then [P13] = "No Data"
If .CountA(Range("Q15:Q4999")) <= 0 Then [Q13] = "No Data"
If .CountA(Range("R15:R4999")) <= 0 Then [R13] = "No Data"
If .CountA(Range("S15:S4999")) <= 0 Then [S13] = "No Data"
If .CountA(Range("T15:T4999")) <= 0 Then [T13] = "No Data"
If .CountA(Range("U15:U4999")) <= 0 Then [U13] = "No Data"
If .CountA(Range("V15:V4999")) <= 0 Then [V13] = "No Data"
If .CountA(Range("W15:W4999")) <= 0 Then [W13] = "No Data"
If .CountA(Range("X15:X4999")) <= 0 Then [X13] = "No Data"
If .CountA(Range("Y15:Y4999")) <= 0 Then [Y13] = "No Data"
If .CountA(Range("Z15:Z4999")) <= 0 Then [Z13] = "No Data"
End With


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe this....BUT it does also apply the code to columns K,L,M as well
Also assumes a last row in Col "A"
If this is a problem, we can break it up to miss those columns

Code:
Private Sub CommandButton1_Click()
Dim lr As Long, c As Integer
lr = Cells(Rows.Count, "A").End(xlUp).Row
For c = 1 To 22
With Application.WorksheetFunction
    If .CountA(Range(Cells(14, c), Cells(lr, c))) = 0 Then Cells(13, c) = "No Data"
End With
Next c
End Sub
 
Last edited:
Upvote 0
This might be better, assumes a max of 5000 rows in each column
Code:
Private Sub CommandButton1_Click()
Dim lr As Integer, c As Integer
lr = 5000
For c = 1 To 22
With Application.WorksheetFunction
    If .CountA(Range(Cells(14, c), Cells(lr, c))) = 0 Then Cells(13, c) = "No Data"
End With
Next c
End Sub
 
Last edited:
Upvote 0
This might be better, assumes a max of 5000 rows in each column
Code:
Private Sub CommandButton1_Click()
Dim lr As Integer, c As Integer
lr = 5000
For c = 1 To 22
With Application.WorksheetFunction
    If .CountA(Range(Cells(14, c), Cells(lr, c))) = 0 Then Cells(13, c) = "No Data"
End With
Next c
End Sub


Having it check all the columns between A and Z isn't really an issue. So I can roll with that.

This is almost perfect. I've been able to verify that it will compare Column A's last entry (example Row 900) against all other named columns to check that the data in the subsequent columns also goes down to Row 900. However, what this doesn't catch is say, Column B Row 750 is missing data at row 750, but 751-900 all have the data expected. Is there a way to also throw up an error if a row within the range set by lr is missing data?

Thank you so much for getting me to this point at least. Very helpful. I've created a second button to allow customers to clear the "No Data" from Row 13 so they can check for data integrity after fixing any columns that get a "No Data" in the first place.
 
Upvote 0
Can that be applied to ALL columns ??
So, you want a message if there is missing data ?
This would mean looping through ALL rows AND columns !!
 
Upvote 0
No I don't think I stated that properly.

So at this point the code you gave me helps me properly identify the lr value in column A. My plan would be to make sure the customer knows their A column data HAS to be correct.

But at this point the macro only checks the final cell that Column A establishes as the lr in the subsequent columns B-V. It does not seem to check any of the cells in any other column c = 1 to 22 for blanks. E.g. it can see that B5000 has data, but if B4999 is missing data, it will not throw up the "No Data" in row 13 of the applicable column.

I guess I can more accurately as this: Can I make this check loop through EVERY row defined within 14:lr by using some kind of interger? It would have to loop through every row within column B-V I guess.
 
Upvote 0
So instead of using COUNTA, why not try uisng COUNTIF with a blank as criteria ?
Would that work ?
Maybe something like this...or am I not getting it

Code:
Private Sub CommandButton1_Click()
Dim lr As Integer, c As Integer
lr = 5000
For c = 1 To 22
With Application.WorksheetFunction
    If .CountIf(Range(Cells(14, c), Cells(lr, c)), "") Then Cells(13, c) = "Data Missing"
End With
Next c
End Sub
 
Upvote 0
It looks like the CountIf grabs any cell within the possible lr range (lr=5000) with a blank... So I believe the core of the CountIf works, but the final step will be to get the lr to set the last cell for a Then statement to use a CountIf that includes "" cells.

Kind of like a 2-stage button with a conditional integer set by the lr determined in the 1st If. Is there a way to make the lr found in the 1st If set a value for the range the second CountIf that checks for blanks?

OR

Can something that uses Range.end and lr together to set the range that the If statement checks? Like
Code:
 .CountA(Cells(15, c), (Cells(Range.end, c)))

Something like this? Again, I really appreciate your help with this. I'm such a rookie with VBA I'm finding it hard to even phrase the contents of my question properly.

Code:
Private Sub CommandButton1_Click()
Dim lr As Integer, c As Integer
lr = 5000
For c = 1 To 26
With Application.WorksheetFunction
    If .CountA(Range(Cells(15, c), Cells(lr, c))) = 0 Then Cells(13, c) = "Missing Data"
          ElseIf .CountA(Cells(15, c), (Cells(Range.end, c))) = "" Then Cells(13, c) = "Missing Data"
          End If

End With
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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