Jump to content
manxten

[VBA] Cifre numar

Recommended Posts

Posted

Salut! Am o problema in Excel pe la care nu am gasit o solutie cu functiile predefinite. Am incercat si cu VBA, dar nu mi-a iesit(ce e drept nici nu sunt un maestru in Visual Basic). Problema suna cam asa: Intr-o celula este scris un numar(ex: 21223224) si se cere sa se afiseze de cate ori apare cifra 2 in numar. Va arasez si o imagine

img.jpg

Daca cineva imi poate oferi o solutie cat mai scurta/simpla fara sa intre in chestii avansate de VB raman recunoscator.

Posted

Solutia doar pentru cifre

Function CountDigitOccurence(Target As Long, SearchFor As Byte)
Dim number As Integer
number = 0

Do While Target <> 0
If Target Mod 10 = SearchFor Then
number = number + 1
End If
Target = Target / 10
Loop

CountDigitOccurence = number
End Function

Solutia pentru siruri de caractere (e ceva mai generala si se poate aplica si cifrelor)

Function CountOccurence(Target As String, SearchFor As Integer)
Dim number As Integer
number = 0

Do While InStr(Target, SearchFor) > 0
number = number + 1
Target = Replace(Target, SearchFor, "", 1, 1)
Loop

CountOccurence = number
End Function

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...