Function count if for text in textboxes VBA Excel

fher9728

New Member
Joined
Jan 9, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am doing a userform where I have 8 textboxs named ddc, example: ddc1, ddc1...ddcn.
then I have another textbox name quantcoup, that is the textbox I want to receive the count if of everyone of the ddcs that have text in it throughout a command button. Also, I want that the count if detect it as text, but the vba code I used is not working for me, the code is the next:



VBA Code:
       Private Sub CommandButton2_Click()


On Error Resume Next
quantcoup.Text = Abs((ddc1.Value = "*") + (ddc2.Value = "*") + (ddc3.Value = "*")+ (ddc4.Value = "*")+ (ddc5.Value = "*")+ (ddc6.Value = "*")+ (ddc7.Value = "*")+ (ddc8.Value = "*"))

End Sub

I´m new at vba and Ive been doing research in the internet but I can´t get a working method for this, if anyone could help it would be great.
Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi and welcome to the board!

How about:

VBA Code:
Private Sub CommandButton2_Click()
  Dim i As Long
  quantcoup.Value = ""
  For i = 1 To 8
    quantcoup.Value = Val(quantcoup.Value) + Abs(Me.Controls("ddc" & i).Value <> "")
  Next
End Sub
 
Upvote 0
Hi and welcome to the board!

How about:

VBA Code:
Private Sub CommandButton2_Click()
  Dim i As Long
  quantcoup.Value = ""
  For i = 1 To 8
    quantcoup.Value = Val(quantcoup.Value) + Abs(Me.Controls("ddc" & i).Value <> "")
  Next
End Sub
Thank you so much, is working perfect, could you explain why do you use val(quantcoup.value) & Abs(Me.Controls("ddc" & i).Value <> "") or what is the logic beneath this part of the code.
thanks.
 
Upvote 0
Thank you so much, is working perfect, could you explain why do you use val(quantcoup.value) & Abs(Me.Controls("ddc" & i).Value <> "") or what is the logic beneath this part of the code.
thanks.

This returns 1 or 0
Abs(Me.Controls("ddc" & i).Value <> "")

This returns the numerical value of the textbox
Val(quantcoup.Value)


e.g.
dd1 = "x"
dd2 = ""
dd3 = "y"

sequence:

dd1 = "x"
i = 1
quantcoup.Value = Val(0) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(0) + 1
quantcoup.Value = 0 + 1
quantcoup.Value = 1

dd2 = ""
i = 2
quantcoup.Value = Val(1) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(1) + 0
quantcoup.Value = 0 + 1
quantcoup.Value = 1

dd2 = "y"
i = 3
quantcoup.Value = Val(1) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(1) + 1
quantcoup.Value = 1 + 1
quantcoup.Value = 2
________________________________________________
 
Upvote 0
This returns 1 or 0
Abs(Me.Controls("ddc" & i).Value <> "")

This returns the numerical value of the textbox
Val(quantcoup.Value)


e.g.
dd1 = "x"
dd2 = ""
dd3 = "y"

sequence:

dd1 = "x"
i = 1
quantcoup.Value = Val(0) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(0) + 1
quantcoup.Value = 0 + 1
quantcoup.Value = 1

dd2 = ""
i = 2
quantcoup.Value = Val(1) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(1) + 0
quantcoup.Value = 0 + 1
quantcoup.Value = 1

dd2 = "y"
i = 3
quantcoup.Value = Val(1) + Abs(Me.Controls("ddc" & i).Value <> "")
quantcoup.Value = Val(1) + 1
quantcoup.Value = 1 + 1
quantcoup.Value = 2
________________________________________________
Great explanation, thank you.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

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