logo
Vai ai contenuti

Codici VBA

Macro per logout
Sub btnLogout_Click()
   

   ThisWorkbook.Sheets("Riservato").Visible = xlSheetVeryHidden
   

End Sub


Macro per visualizzare form
Sub login_Click()

login.Show
   
End Sub


Pulsante Accedi
Private Sub btnLogin_Click()

        Dim nomeUtente As String
        Dim password As String

   
   nomeUtente = txtNomeUtente.Text
   password = txtPassword.Text

   
   If VerificaCredenziali(nomeUtente, password) Then

       
       ThisWorkbook.Sheets("Riservato").Visible = xlSheetVisible
Unload Me

   Else
     
       MsgBox "Nome utente o password errati. Riprova.", vbExclamation, "Accesso negato"

   End If

End Sub

Function VerificaCredenziali(nomeUtente As String, password As String) As Boolean
  

   Const nomeUtenteCorretto As String = "testExcel"
   Const passwordCorretta As String = "testExcel"
   

   If nomeUtente = nomeUtenteCorretto And password = passwordCorretta Then
       VerificaCredenziali = True

   Else
       VerificaCredenziali = False

   End If

End Function



Macro - Maiuscolo
Sub minuscolo ()
Dim celle As Range
Set celle = Selection
For Each celle In celle
celle.value = LCase(celle)
Next celle       
End Sub

Macro - Maiuscolo
Sub maiuscolo ()
Dim celle As Range
Set celle = Selection
For Each celle In celle
celle.value = UCase(celle)
Next celle       
End Sub

Macro - Motore di ricerca
Private Sub TextBox1_Change()
filtro = "*" & Sheets("Ricerca").TextBox1.Text & "*"
Range("c7").AutoFilter field:=3, Criteria1:=filtro       
End Sub

Funzione - Somma celle in base al colore
Function SommaCellePerColore(rData As Range, cellRefColor As Range)
   Dim indRefColor As Long
   Dim cellaCorrente As Range
  Dim sumRes

   Application.Volatile
   sumRes = 0
   indRefColor = cellRefColor.Cells(1,  1).Interior.Color
   For Each cellaCorrente In rData
       If indRefColor = cellaCorrente.Interior.Color Then
   sumRes = WorksheetFunction.Sum(cellaCorrente, sumRes)
         End If
   Next cellaCorrente

SommaCellePerColore = sumRes
End Function

Funzione - Conta celle in base al colore
Function ContaCellePerColore(rData As Range, cellRefColor As Range) As Long
   Dim indRefColor As Long
   Dim cellaCorrente As Range
  Dim cntRes As Long

   Application.Volatile
  cntRes = 0
   indRefColor = cellRefColor.Cells(1,  1).Interior.Color
   For Each cellaCorrente In rData
       If indRefColor = cellaCorrente.Interior.Color Then
         cntRes = cntRes + 1
         End If
   Next cellaCorrente

ContaCellePerColore = cntRes
End Function

Funzione - Somma celle in base al colore del carattere
Function SommaCellePerColoreCarattere(rData As Range, cellRefColor As Range)
   Dim indRefColor As Long
   Dim cellaCorrente As Range
   Dim sumRes

   Application.Volatile
   sumRes = 0
   indRefColor = cellRefColor.Cells(1,  1).Font.Color
   For Each cellaCorrente In rData
       If indRefColor = cellaCorrente.Font.Color Then
   sumRes = WorksheetFunction.Sum(cellaCorrente, sumRes)
         End If
   Next cellaCorrente

SommaCellePerColoreCarattere = sumRes
End Function

Funzione - Conta celle in base al colore del carattere
Function ContaCellePerColoreCarattere(rData As Range, cellRefColor As Range) As Long
   Dim indRefColor As Long
   Dim cellaCorrente As Range
  Dim cntRes As Long

   Application.Volatile
  cntRes = 0
   indRefColor = cellRefColor.Cells(1,  1).Font.Color
   For Each cellaCorrente In rData
       If indRefColor = cellaCorrente.Font.Color Then
         cntRes = cntRes + 1
         End If
   Next cellaCorrente

ContaCellePerColoreCarattere = cntRes
End Function

Torna ai contenuti