2

Here is my entire code and I will explain it and what I want to add.

The first function is calling two other functions.

The second function is used to calculate JMBG, which is unique number of citizen in my country. The third one is calculating PIB, which is registered number for companies.

Those two functions are OK and they don't need to be moved or anything like that.

We need to change this first function. As you can see, in the first function I am checking whether the length of the input string is OK. If the length is 13 numbers I call JMBG and if it is 8 I call PIB function. That is OK.

But I must check other types of validation in this first function. As I said, my Excel cell contains 13 numbers or 8 numbers. I want to make some rules in this first function that will tell me if my cell is filled with anything else except those 8 numbers or 13, then send me msg telling me that there is error in the cell and those 2 other functions then won't be called. As you can see, I need validation.

Example: Cell A1: 1234567891234...there is 13 numbers and JMBG will be called 08058808...there is 8 numbers and PIB will be called 1234567890123aSdf~...error because small and big letters and other characters are in the field.

As sum of all this, I need for 8 numbers to call PIB, for 13 numbers to call JMBG and for anything else except that to send me error.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ProvjeraID(ID As String) As String

If Len(ID) = 13 Then
ProvjeraID = Provjeri_JMBG(ID)
 'Exit Function
ElseIf Len(ID) = 8 Then
 ProvjeraID = ProvjeriPIB(ID)
 'Exit Function
 Else
 ProvjeraID = "Duzina je razlicita od 8 i od 13"
 'Exit Function
End If

End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Provjeri_JMBG(JMBG As String) As String
' Funkcija vraca tekst sa opisom ispravnosti JMBG
' Primijeniti na radnom listu uz pomoc komande: =Proveri_JMBG(adresa)

' Inicijalizacija promenljivih koje se koriste prilikom izrade koda
Dim duzina As Integer, zbir As Integer
Dim cifra(1 To 13) As Integer
Dim dan As Integer, mesec As Integer, godina As String

' Inicijalizacija konstanti
Const ERR_dan = "GREŠKA: podatak o datumu neispravan!"
Const ERR_mesec = "GREŠKA: podatak o mesecu neispravan!"
Const ERR_godina = "GREŠKA: podatak o godini neispravan!"
Const ERR_duzina = "GREŠKA: dužina razlicita od 13!"
Const ERR_kont = "GREŠKA: neispravan kontrolni broj!"
Const OK_JMBG = "JMBG je ispravan"

' Preuzimanje ulaznih vrednosti sa kojima ce se vrsiti operacije
duzina = Len(JMBG)
dan = Int(Left(JMBG, 2))
mesec = Int(Mid$(JMBG, 3, 2))
godina = Mid$(JMBG, 5, 3)

' Provjera dužine JMBG
If (duzina <> 13) Then
  Provjeri_JMBG = "GREŠKA: dužina razlicita od 13!"
  Exit Function
End If

' Provjera datuma
If dan < 1 Then
  Provjeri_JMBG = "GREŠKA: podatak o datumu neispravan!"
  Exit Function
End If

' Provjera mjeseca i dana u mjesecu
Select Case mesec
  Case 1, 3, 5, 7, 8, 10, 12
    If dan > 31 Then
      Provjeri_JMBG = "GREŠKA: podatak o datumu neispravan!"
      Exit Function
    End If
  Case 4, 6, 9, 11
    If dan > 30 Then
      Provjeri_JMBG = "GREŠKA: podatak o datumu neispravan!"
      Exit Function
    End If
  Case 2
    If ((godina Mod 4 = 0) And dan > 29) Or _
       ((godina Mod 4 <> 0) And dan > 28) Then
      Provjeri_JMBG = "GREŠKA: podatak o datumu neispravan!"
      Exit Function
    End If
  Case Else
    Provjeri_JMBG = "GREŠKA: podatak o mesecu neispravan!"
    Exit Function
End Select

' Provjera godine: ispravne su od 1899 do tekuce godine
If (godina > Right(Str(Year(Now)), 3)) And (godina < "899") Then
  Provjeri_JMBG = "GREŠKA: podatak o godini neispravan!"
  Exit Function
End If

' Provjera kontrolnog broja
For i = 1 To 13
  cifra(i) = Int(Mid$(JMBG, i, 1))
Next i

zbir = cifra(13) + cifra(1) * 7 + cifra(2) * 6
zbir = zbir + cifra(3) * 5 + cifra(4) * 4
zbir = zbir + cifra(5) * 3 + cifra(6) * 2
zbir = zbir + cifra(7) * 7 + cifra(8) * 6
zbir = zbir + cifra(9) * 5 + cifra(10) * 4
zbir = zbir + cifra(11) * 3 + cifra(12) * 2

If (zbir Mod 11) <> 0 Then
  Provjeri_JMBG = "GREŠKA: neispravan kontrolni broj!"
Else
  Provjeri_JMBG = "JMBG je ispravan"
End If

End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function ProvjeriPIB(PIB As String)
Dim c0 As Integer
Dim c1 As Integer
Dim c2 As Integer
Dim c3 As Integer
Dim c4 As Integer
Dim c5 As Integer
Dim c6 As Integer
Dim c7 As Integer
Dim c8 As Integer
Dim zadnji As String
zadnji = Right(PIB, 1)
PIB = Left(PIB, 8)
If Len(PIB) <> 8 Then
   ProvjeriPIB = "PIB je OK"
Else
       c8 = (CInt(Mid(PIB, 1, 1)) + 10) Mod 10
       If c8 = 0 Then
         c8 = 10
       End If
       c8 = (c8 * 2) Mod 11
       c7 = (CInt(Mid(PIB, 2, 1)) + c8) Mod 10
       If c7 = 0 Then
         c7 = 10
       End If
       c7 = (c7 * 2) Mod 11
       c6 = (CInt(Mid(PIB, 3, 1)) + c7) Mod 10
       If c6 = 0 Then
         c6 = 10
       End If
       c6 = (c6 * 2) Mod 11
       c5 = (CInt(Mid(PIB, 4, 1)) + c6) Mod 10
       If c5 = 0 Then
         c5 = 10
       End If
       c5 = (c5 * 2) Mod 11
       c4 = (CInt(Mid(PIB, 5, 1)) + c5) Mod 10
       If c4 = 0 Then
         c4 = 10
       End If
       c4 = (c4 * 2) Mod 11
       c3 = (CInt(Mid(PIB, 6, 1)) + c4) Mod 10
       If c3 = 0 Then
         c3 = 10
       End If
       c3 = (c3 * 2) Mod 11
       c2 = (CInt(Mid(PIB, 7, 1)) + c3) Mod 10
       If c2 = 0 Then
         c2 = 10
       End If
       c2 = (c2 * 2) Mod 11
       c1 = (CInt(Mid(PIB, 8, 1)) + c2) Mod 10
       If c1 = 0 Then
         c1 = 10
       End If
       c1 = (c1 * 2) Mod 11
       c0 = (11 - c1) Mod 10
       If c0 <> zadnji Then
        ProvjeriPIB = "PIB je OK"
       Else
        ProvjeriPIB = "PIB nije OK"
       End If
       'return(pib || to_char(c0));

End If
End Function
6
  • 3
    Can you show us your original text and the expected results based on that? Please define at least two examples given the proper rules to be applied.
    – bonCodigo
    Jan 29, 2013 at 9:36
  • Here is example. My code is checking whether in excel cell is entered string containing of 13 numbers. Everything except that needs to be returned as error saying an example: ''Your cell is not containing only numbers, there are other invalid characters''
    – MarkoD
    Jan 29, 2013 at 9:55
  • 2
    Use Data Validation to prevent non numeric entries
    – brettdj
    Jan 29, 2013 at 9:56
  • 1
    Only if I saw your comment here............. it's great if you had indicated that you need to check length as well... and doesn't want anything other than numbers
    – bonCodigo
    Jan 29, 2013 at 10:00
  • 1
    I put all my code up. See it.
    – MarkoD
    Jan 29, 2013 at 11:19

4 Answers 4

2

This solution is based on regex from Scripting library. I have used 3 objects, but code definitely be trimmed to use just one object to check for all three conditions that you required. Since you wanted information about the text that you are inserting I have merely used 3 different regex rules.

Option Explicit

Sub TextNature()
Dim str  As String
Dim strMsg As String
Dim objRegEx1 As Object, objRegEx2 As Object
Dim objRegEx3 As Object

str = Sheets(1).Range("A2").Value

'--check length
If Len(str) <> 13 Then
   Exit Sub
   strMsg = "Too lengthy...limit should be 13"
End If

Set objRegEx1 = CreateObject("VBScript.RegExp")
Set objRegEx2 = CreateObject("VBScript.RegExp")
Set objRegEx3 = CreateObject("VBScript.RegExp")
objRegEx1.IgnoreCase = False
objRegEx1.Global = True
objRegEx2.IgnoreCase = False
objRegEx2.Global = True
objRegEx3.IgnoreCase = False
objRegEx3.Global = True


objRegEx1.Pattern = "^\d+$" '-- only numbers
objRegEx2.Pattern = "^[a-zA-Z]+$" '-- only lower upper letters
objRegEx3.Pattern = "^[a-zA-Z\d]+$" '-- numbers and lower upper letters

If objRegEx1.Test(str) Then
    strMsg = "Contain only numbers"
ElseIf objRegEx2.Test(str) Then
    strMsg = "Contain only lower upper letters"
ElseIf objRegEx3.Test(str) Then
    strMsg = "Contain numbers and lower upper letters"
Else
     strMsg = "not satisfying"
End If

End Sub

Results : used the sub as a function:

enter image description here


OP requests for a function, and length limit to be 8:

Option Explicit

Function TextNature(ByRef rng As Range) As String
Dim str  As String, strMsg As String
Dim objRegEx1 As Object, objRegEx2 As Object, objRegEx3 As Object

str = rng.Value
If Len(str) <> 8 Then
    TextNature = "Limit is not correct. It should be 8."
    Exit Function
End If

Set objRegEx1 = CreateObject("VBScript.RegExp")
Set objRegEx2 = CreateObject("VBScript.RegExp")
Set objRegEx3 = CreateObject("VBScript.RegExp")
objRegEx1.IgnoreCase = False
objRegEx1.Global = True
objRegEx2.IgnoreCase = False
objRegEx2.Global = True
objRegEx3.IgnoreCase = False
objRegEx3.Global = True


objRegEx1.Pattern = "^\d+$" '-- only numbers
objRegEx2.Pattern = "^[a-zA-Z]+$" '-- only lower/upper letters
objRegEx3.Pattern = "^[a-zA-Z\d]+$" '-- numbers and lower/upper letters

If objRegEx1.Test(str) Then
    strMsg = "Contain only numbers"
ElseIf objRegEx2.Test(str) Then
    strMsg = "Contain only lower upper letters"
ElseIf objRegEx3.Test(str) Then
    strMsg = "Contain numbers and lower upper letters"
Else
     strMsg = "Not Satisfying"
End If

TextNature = strMsg
End Function
18
  • @MarkoD The code I have provided is a Sub, you can change it to a function by this: Function TextNature(ByRef rng as Range) as String, add TextNature = strMsg. then the rng will point to A1 and function will return strMsg which is a string. Please show us your code as well
    – bonCodigo
    Jan 29, 2013 at 10:12
  • You may go your question, click edit then append the code to the text you already have. If you don't now how to format, we can help you
    – bonCodigo
    Jan 29, 2013 at 10:35
  • I will translate some parts in english if you cant figure out all
    – MarkoD
    Jan 29, 2013 at 11:10
  • @brettdj Thanks. I have to admit that I was lazy ;)
    – bonCodigo
    Jan 29, 2013 at 11:31
  • @MarkoD sorry I can't read it, but have you tried the code I have provided? If you only want to check that cell value is a number/numbers, then you may simple remove all the other regex objects, just keep objRegEx1 :) Let me know how I can help you, if you have any other issues with the code execution..
    – bonCodigo
    Jan 29, 2013 at 11:32
0

Something like this should help - you can define the criteria in the select statement. It's a UDF so put the code into a module and enter =checkcell(A1) into a cell.

Public Function CheckCell(ByVal CheckRange As Range) As String
Dim strChr As String, rngCheck As Range
Dim i As Integer, NPC As Integer, UC As Integer, LC As Integer, OT As Integer
Set rngCheck = Range("A1")
For i = 1 To rngCheck.Characters.Count
    strChr = rngCheck.Characters(i, 1).Text
    Select Case Asc(strChr)
        Case 0 To 31
            NPC = NPC + 1
        Case 96 To 122
            LC = LC + 1
        Case 65 To 90
            UC = UC + 1
        Case Else
            OT = OT + 1
    End Select
Next
CheckCell = "NPC: " & NPC & " UC: " & UC & " LC: " & LC & " Others: " & OT
End Function
1
  • It gives me returned value as: #Value!
    – MarkoD
    Jan 29, 2013 at 10:04
0

In case formula-based solution is OK - use this ARRAY formula (assuming string for checking is in A1):

=IF(OR(NOT(ISERROR(SEARCH(ROW($1:$10)-1,A1)))),"Has digits","No digits")

and press CTRL+SHIFT+ENTER instead of usual ENTER - this will define an ARRAY formula and will result in {} brackets around it (but do NOT type them manually!).

String length and any other chars do not matter. Hope that was helpful)

0

Replace your first function with something like the following, and call it in a cell using =ProvjeraID2(A1) to evaluate the contents of cell A1:

Function ProvjeraID2(oRng As Range) As String
    Dim sRet As String

    If Not oRng Is Nothing Then
        If IsNumeric(oRng.Value) Then
            If Len(oRng.Value) = 13 Then
                sRet = Provjeri_JMBG(CStr(oRng.Value))
            ElseIf Len(oRng.Value) = 8 Then
                sRet = ProvjeriPIB(CStr(oRng.Value))
            Else
                sRet = "Numeric but wrong length (" & Len(oRng.Value) & ")"
            End If
        Else
            sRet = "Not a number"
        End If
    End If

    ProvjeraID2 = sRet
End Function
2
  • Great job !!! That is what I want to have!!! Thank you all for help. This forum is really good.
    – MarkoD
    Jan 29, 2013 at 11:50
  • @MarkoD in case you're happy with any answer - please tick it as accepted, this will mark thread solved.
    – Peter L.
    Jan 29, 2013 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.