
Option Explicit
Option Compare Text
Sub ricerca()
Dim UR, R, CF, X, Test, Nome As String, Rg As Object
CF = InputBox("Inserire codice Fiscale 16 caratteri", , 0)
If Len(CF) = 16 Then
Set Rg = Sheets("Foglio1").Range("B:B").Find(CF, LookIn:=xlValues, LookAt:=xlWhole)
If Rg Is Nothing Then
MsgBox "Nessuna codice Fiscale trovato " & CF
Exit Sub
Else
R = Rg.Row
Nome = Sheets("Foglio1").Cells(R - 9, 2) & Sheets("Foglio1").Cells(R - 8, 2)
If Nome = "" Then MsgBox "Manca Cognome&Nome": Exit Sub
For X = 2 To Sheets.Count
If Sheets(X).Range("B10") = CF Then MsgBox "Il codice Fiscale esiste già nel foglio " & Sheets(X).Name: Exit Sub
Next X
For X = 2 To Sheets.Count
If Sheets(X).Range("B1") & Sheets(X).Range("B2") = Nome And Sheets(X).Range("B10") <> CF Then
MsgBox "Non posso creare due fogli " & Nome & " uguali" & vbCrLf & "anche se il codice Fiscale è diverso " & CF
Exit Sub
End If
Next X
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = Nome
Sheets("Foglio1").Range("A" & R - 9 & ":B" & R + 2).Copy
Sheets(Nome).Range("A1").PasteSpecial
Sheets(Nome).Move After:=Sheets(Worksheets.Count)
MsgBox "Creato foglio " & Nome
End If
Else
MsgBox "Numero caratteri codice Fiscale errati"
End If
End Sub
|
Option Explicit
Option Compare Text
Sub ricerca2()
Dim sh1 As Worksheet: Set sh1 = Worksheets("Foglio1") ' da cambiare casomai
Dim Ur, R, CF, X, Y, Nome As String
Ur = sh1.Range("A" & Rows.Count).End(xlUp).Row
CF = InputBox("Inserire codice Fiscale 16 caratteri", , 0)
If Len(CF) = 16 Then
For Y = 10 To Ur
If sh1.Cells(Y, 2).Value = CF Then
Nome = sh1.Cells(Y - 9, 2) & sh1.Cells(Y - 8, 2)
If Nome = "" Then MsgBox "Manca Cognome&Nome": Exit Sub
For X = 2 To Sheets.Count
If Sheets(X).Range("B10").Value = CF Then MsgBox "Il codice Fiscale esiste già nel foglio " & Sheets(X).Name: Exit Sub
Next X
For X = 2 To Sheets.Count
If Sheets(X).Range("B1") & Sheets(X).Range("B2") = Nome And Sheets(X).Range("B10") <> CF Then
MsgBox "Non posso creare due fogli " & Nome & " uguali" & vbCrLf & "anche se il codice Fiscale è diverso " & CF
Exit Sub
End If
Next X
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = Nome
sh1.Range("A" & Y - 9 & ":B" & Y + 2).Copy
Sheets(Nome).Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(Nome).Move After:=Sheets(Worksheets.Count)
MsgBox "Creato foglio " & Nome
End If
Y = Y + 11
Next Y
Else
MsgBox "Numero caratteri codice Fiscale errati"
End If
Set sh1 = Nothing
End Sub |
