
Option Explicit
Sub cercaRange_Textomb()
Dim String_A As String, RangeF As Range, cell As Range, i As Long
Dim String_B As String, c As Range, Mr()
'memorizzo il range da cercare del foglio1 in una unica stringa di testo
For Each cell In Foglio1.Range("b2:d5")
String_A = String_A & cell
Next
'Costruisco una matrice di stringhe contenute nel foglio 2
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(4, 3)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
'Infine verifico se esiste una corrispondenza tra la stringa_A del foglio 1 e l'insieme delle Stringhe_B del foglio2
For i = 1 To UBound(Mr)
If String_A = Mr(i, 1) Then MsgBox "La prima corrispondenza si trova nell'area " & _
Foglio2.Range(Mr(i, 2)).Resize(4, 3).Address: Exit Sub
Next
MsgBox "Non ci sono corrispondenze "
Set RangeF = Nothing
Erase Mr
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub cercaRangeS_Textomb()
' Questa routine scansiona l'intero foglio2 e se ci sono più occorrenze riferite al Range B2:D5 del foglio 1 ne restituisce
' gli indirizzi in un unico messaggio di testo.
Dim String_A As String, RangeF As Range, cell As Range, i As Long
Dim String_B As String, c As Range, Mr() As String, Strings As String, MyStr
'memorizzo il range da cercare del foglio1 in una unica stringa di testo
For Each cell In Foglio1.Range("b2:d5")
String_A = String_A & cell
Next
'Costruisco una matrice di stringhe contenute nel foglio 2
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(4, 3)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
'Individuo tutte le corrispondenze tra la stringa_A del foglio 1 e l'insieme delle Stringhe_B del foglio2
i = 1
Do
If String_A = Mr(i, 1) Then Strings = Strings & Chr(13) & Foglio2.Range(Mr(i, 2)).Resize(4, 3).Address
i = i + 1
Loop Until i = UBound(Mr)
If Strings = "" Then
MsgBox "Non sono state trovate occorrenze"
Else
MyStr = Split(Strings, Chr(13))
MsgBox "Le corrispondenze si trovano in " & Chr(13) & Join(MyStr, vbCr)
End If
Set RangeF = Nothing
Erase Mr
End Sub
|
Sub cercaRangeS_Textomb()
' Questa routine non si ferma alla prima occorrenza ma scansiona l'intero foglio2 e se ci sono più occorrenze restituisce
' gli indirizzi in un unico messaggio di testo.
Dim String_A As String, RangeF As Range, cell As Range, i As Long
Dim String_B As String, c As Range, Mr() As String, Strings As String
'memorizzo il range da cercare del foglio1 in una unica stringa di testo
For Each cell In Foglio1.Range("b2:d5")
String_A = String_A & cell
Next
'Costruisco una matrice di stringhe contenute nel foglio 2
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(4, 3)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
'Individuo tutte le corrispondenze tra la stringa_A del foglio 1 e l'insieme delle Stringhe_B del foglio2
i = 1
Do
If String_A = Mr(i, 1) Then Strings = Strings & "," & Foglio2.Range(Mr(i, 2)).Resize(4, 3).Address
i = i + 1
Loop Until i = UBound(Mr)
If Strings = "" Then
MsgBox "Non sono state trovate occorrenze"
Else
Strings = Right(Strings, Len(Strings) - 1)
Foglio2.Select
Range(Strings).Select
MsgBox "Le corrispondenze si trovano in :" & vbCr & Replace(Strings, ",", vbCr)
End If
Set RangeF = Nothing
Erase Mr
End Sub |
Sub cercaRange_seleziona()
Dim String_A As String, RangeF As Range, cell As Range, i As Long
Dim String_B As String, c As Range, Mr(), Strings As String
Set rngs = Foglio1.Range("a1:b3")
nrows = rngs.Rows.Count
ncols = rngs.Columns.Count
For Each cell In rngs
String_A = String_A & cell
Next
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(nrows, ncols)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
i = 1
Do
If String_A = Mr(i, 1) Then Strings = Strings & "," & Foglio2.Range(Mr(i, 2)).Resize(nrows, ncols).Address
i = i + 1
Loop Until i = UBound(Mr)
If Strings = "" Then
MsgBox "Non sono state trovate occorrenze"
Else
ls = Len(Strings)
Strings = Right(Strings, ls - 1)
Foglio2.Select
Range(Strings).Select
MsgBox "Le corrispondenze si trovano in :" & vbCr & Replace(Strings, ",", vbCr)
End If
Set RangeF = Nothing
Erase Mr
End Sub |
range("A1")=string(300,"*")
?len(range("A1"))
300
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub cercaRange_scossa()
Dim rngTarget As Range
Dim rngFound As Range
Dim RngEval As Range
Dim nstart As Single
On Error GoTo scossa_handler
Application.Calculation = xlCalculationManual
nstart = Timer
Set rngTarget = Foglio1.Range("B2:D5")
Set RngEval = Foglio3.Range("A1")
RngEval.FormulaArray = "=IF(Foglio1!$B$2:$D$5 = Foglio2!A1:D4,1,"""")"
RngEval.Copy RngEval.Offset(0, 1).Resize(1, 19)
RngEval.Resize(1, 20).Copy RngEval.Offset(1, 0).Resize(3000, 1)
Foglio3.Calculate
Set rngFound = Foglio3.Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
If rngFound Is Nothing Then
Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
Else
Err.Raise vbObjectError + 513, Description:="corrispondenze trovate in:" & vbCrLf & rngFound.Address(0, 0)
End If
scossa_handler:
Foglio3.UsedRange.ClearContents 'eventualmente commentare
Application.Calculation = xlCalculationAutomatic
If Err.Number <> 0 Then
MsgBox Err.Description & vbCrLf & "tempo impiegato: " & Timer - nstart & " secondi"
End If
Set rngTarget = Nothing
Set RngEval = Nothing
Set rngFound = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub cercaRangeS_Textomb()
' Questa routine non si ferma alla prima occorrenza ma scansiona l'intero foglio2 e se ci sono più occorrenze restituisce
' gli indirizzi in un unico messaggio di testo.
Dim String_A As String, RangeF As Range, cell As Range, i As Long, Nr As Long
Dim String_B As String, c As Range, Mr() As String, StrRange As Range
'memorizzo il range da cercare del foglio1 in una unica stringa di testo
For Each cell In Foglio1.Range("b2:d5")
String_A = String_A & cell
Next
'Costruisco una matrice di stringhe contenute nel foglio 2
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(4, 3)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
'Individuo tutte le corrispondenze tra la stringa_A del foglio 1 e l'insieme delle Stringhe_B del foglio2
i = 1
Do
If String_A = Mr(i, 1) Then
If StrRange Is Nothing Then
Set StrRange = Range(Mr(i, 2)).Resize(4, 3)
Nr = 1
Else
Set StrRange = Union(StrRange, Range(Mr(i, 2)).Resize(4, 3))
Nr = Nr + 1
End If
End If
i = i + 1
Loop Until i = UBound(Mr)
If StrRange Is Nothing Then
MsgBox "Non sono state trovate occorrenze"
Else
Foglio2.Select
StrRange.Select
MsgBox "Sono state trovate Nr. " & Nr & " occorrenze.", vbInformation
End If
Set RangeF = Nothing
Set StrRange = Nothing
Erase Mr
End Sub |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
'---------------------------------------------------------------------------------------
' Procedure : cercaRange_scossa
' Author : scossa
' Date : 07/05/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
Sub cercaRange_scossa()
Dim rngTarget As Range
Dim rCellaFound As Range
Dim rngFound As Range
Dim RngEval As Range
Dim rngUnion As Range
Dim nStart As Single
Dim nStop As Single
Dim vWhat As Variant
Dim cAddress As String
Dim nCols As Long
Dim nRows As Long
Dim nOffR As Long
Dim nOffC As Long
Dim bFound As Boolean
Dim nErr As Long
Dim sErr As String
On Error GoTo scossa_handler
nStart = Timer
Set rngTarget = Foglio1.Range("B2:D5")
nRows = rngTarget.Rows.Count
nCols = rngTarget.Columns.Count
Set RngEval = Foglio2.UsedRange
vWhat = rngTarget.Cells(1, 1).Value
With RngEval
Set rCellaFound = .Find(vWhat, _
after:=.Cells(.Rows.Count, .Columns.Count), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
searchorder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If rCellaFound Is Nothing Then Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
Set rngUnion = rCellaFound
cAddress = rCellaFound.Address
Do
Application.StatusBar = rCellaFound.Address(0, 0)
bFound = True
If rCellaFound.Row <= (Rows.Count - nRows) And rCellaFound.Column <= (Columns.Count - nCols) Then
Set rngFound = rCellaFound.Resize(nRows, nCols)
For nOffR = 1 To nRows
For nOffC = 1 To nCols
If rngFound.Cells(nOffR, nOffC) <> rngTarget.Cells(nOffR, nOffC) Then
bFound = False
Exit For
End If
Next
If bFound = False Then Exit For
Next
If bFound Then
Set rngUnion = Union(rngUnion, rngFound)
bFound = False
End If
End If
Set rCellaFound = RngEval.FindNext(rCellaFound)
Loop While Not rCellaFound Is Nothing And rCellaFound.Address <> cAddress
If rngFound.Cells.Count < rngTarget.Cells.Count Then
Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
Else
Err.Raise vbObjectError + 514, Description:=rngUnion.Areas.Count & " corrispondenze trovate" & vbCrLf _
& "(" & Replace(rngUnion.Address(0, 0), ",", "; ") & ")"
End If
scossa_handler:
nStop = Timer - nStart
Application.StatusBar = False
nErr = Err.Number - vbObjectError
sErr = Err.Description
Foglio1.Range("H1").Value = nStop
If Err.Number <> 0 Then
If nErr = 514 Then
rngUnion.Parent.Activate
rngUnion.Select
End If
MsgBox sErr & vbCrLf & vbCrLf & "tempo impiegato: " & nStop & " secondi"
End If
Set rngTarget = Nothing
Set RngEval = Nothing
Set rngFound = Nothing
Set rCellaFound = Nothing
Set rngUnion = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
'---------------------------------------------------------------------------------------
' Procedure : cercaRange_scossa
' Author : scossa
' Date : 07/05/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
Sub cercaRange_scossa()
Dim rngTarget As Range
Dim rCellaFound As Range
Dim rngFound As Range
Dim RngEval As Range
Dim rngUnion As Range
Dim nStart As Single
Dim nStop As Single
Dim vWhat As Variant
Dim cAddress As String
Dim nCols As Long
Dim nRows As Long
Dim nOffR As Long
Dim nOffC As Long
Dim bFound As Boolean
Dim nErr As Long
Dim sErr As String
On Error GoTo scossa_handler
nStart = Timer
Set rngTarget = Foglio1.Range("B2:D5")
nRows = rngTarget.Rows.Count
nCols = rngTarget.Columns.Count
Set RngEval = Foglio2.UsedRange
vWhat = rngTarget.Cells(1, 1).Value
With RngEval
Set rCellaFound = .Find(vWhat, _
after:=.Cells(.Rows.Count, .Columns.Count), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
searchorder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If rCellaFound Is Nothing Then Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
'Set rngUnion = rCellaFound
cAddress = rCellaFound.Address
Do
Application.StatusBar = rCellaFound.Address(0, 0)
bFound = True
If rCellaFound.Row <= (Rows.Count - nRows) And rCellaFound.Column <= (Columns.Count - nCols) Then
Set rngFound = rCellaFound.Resize(nRows, nCols)
For nOffR = 1 To nRows
For nOffC = 1 To nCols
If rngFound.Cells(nOffR, nOffC) <> rngTarget.Cells(nOffR, nOffC) Then
bFound = False
Exit For
End If
Next
If bFound = False Then Exit For
Next
If bFound Then
If rngUnion Is Nothing Then
Set rngUnion = rngFound
Else
Set rngUnion = Union(rngUnion, rngFound)
End If
bFound = False
End If
End If
Set rCellaFound = RngEval.FindNext(rCellaFound)
Loop While Not rCellaFound Is Nothing And rCellaFound.Address <> cAddress
If rngUnion.Cells.Count < rngTarget.Cells.Count Then
Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
Else
Err.Raise vbObjectError + 514, Description:=rngUnion.Areas.Count & " corrispondenze trovate" & vbCrLf _
& "(" & Replace(rngUnion.Address(0, 0), ",", "; ") & ")"
End If
scossa_handler:
nStop = Timer - nStart
Application.StatusBar = False
nErr = Err.Number - vbObjectError
sErr = Err.Description
Foglio1.Range("H1").Value = nStop
If Err.Number <> 0 Then
If nErr = 514 Then
rngUnion.Parent.Activate
rngUnion.Select
End If
MsgBox sErr & vbCrLf & vbCrLf & "tempo impiegato: " & nStop & " secondi"
End If
Set rngTarget = Nothing
Set RngEval = Nothing
Set rngFound = Nothing
Set rCellaFound = Nothing
Set rngUnion = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
'---------------------------------------------------------------------------------------
' Procedure : cercaRange_scossa
' Author : scossa
' Date : 07/05/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
Sub cercaRange_scossa()
Dim rngTarget As Range
Dim rCellaFound As Range
Dim rngFound As Range
Dim RngEval As Range
Dim rngUnion As Range
Dim nStart As Single
Dim nStop As Single
Dim vWhat As Variant
Dim cAddress As String
Dim nCols As Long
Dim nRows As Long
Dim nOffR As Long
Dim nOffC As Long
Dim bFound As Boolean
Dim nErr As Long
Dim sErr As String
On Error GoTo scossa_handler
nStart = Timer
Set rngTarget = Foglio1.Range("B2:D5")
nRows = rngTarget.Rows.Count
nCols = rngTarget.Columns.Count
Set RngEval = Foglio2.UsedRange
vWhat = rngTarget.Cells(1, 1).Value
With RngEval
Set rCellaFound = .Find(vWhat, _
after:=.Cells(.Rows.Count, .Columns.Count), _
LookAt:=xlWhole, _
LookIn:=xlFormulas, _
searchorder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=True)
End With
If rCellaFound Is Nothing Then Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
cAddress = rCellaFound.Address
Do
Application.StatusBar = rCellaFound.Address(0, 0)
bFound = True
If rCellaFound.Row <= (Rows.Count - nRows) And rCellaFound.Column <= (Columns.Count - nCols) Then
Set rngFound = rCellaFound.Resize(nRows, nCols)
For nOffR = 1 To nRows
For nOffC = 1 To nCols
If rngFound.Cells(nOffR, nOffC) <> rngTarget.Cells(nOffR, nOffC) Then
bFound = False
Exit For
End If
Next
If bFound = False Then Exit For
Next
If bFound Then
If rngUnion Is Nothing Then
Set rngUnion = rngFound
Else
Set rngUnion = Union(rngUnion, rngFound)
End If
bFound = False
End If
End If
Set rCellaFound = RngEval.FindNext(rCellaFound)
Loop While Not rCellaFound Is Nothing And rCellaFound.Address <> cAddress
If Not rngUnion Is Nothing Then
Err.Raise vbObjectError + 514, Description:=rngUnion.Areas.Count & " corrispondenze trovate" & vbCrLf _
& "(" & Replace(rngUnion.Address(0, 0), ",", "; ") & ")"
Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
Else
Err.Raise vbObjectError + 513, Description:="corrispondenze non trovate"
End If
scossa_handler:
nStop = Timer - nStart
Application.StatusBar = False
nErr = Err.Number - vbObjectError
sErr = Err.Description
Foglio1.Range("H1").Value = nStop
If Err.Number <> 0 Then
If nErr = 514 Then
rngUnion.Parent.Activate
rngUnion.Select
End If
MsgBox sErr & vbCrLf & vbCrLf & "tempo impiegato: " & nStop & " secondi"
End If
Set rngTarget = Nothing
Set RngEval = Nothing
Set rngFound = Nothing
Set rCellaFound = Nothing
Set rngUnion = Nothing
End Sub
Sub cercaRangeS_Textomb()
' Questa routine non si ferma alla prima occorrenza ma scansiona l'intero foglio2 e se ci sono più occorrenze restituisce
' gli indirizzi in un unico messaggio di testo.
Dim String_A As String, RangeF As Range, cell As Range, i As Long, Nr As Long
Dim String_B As String, c As Range, Mr() As String, StrRange As Range
Dim nStart As Single, nStop As Single
nStart = Timer
'memorizzo il range da cercare del foglio1 in una unica stringa di testo
For Each cell In Foglio1.Range("b2:d5")
String_A = String_A & cell
Next
'Costruisco una matrice di stringhe contenute nel foglio 2
ReDim Mr(1 To Foglio2.UsedRange.Count, 1 To 2)
For Each cell In Foglio2.UsedRange
String_B = ""
Set RangeF = cell.Resize(4, 3)
For Each c In RangeF
String_B = String_B & c
Next
i = i + 1
Mr(i, 1) = String_B
Mr(i, 2) = cell.Address
Next
'Individuo tutte le corrispondenze tra la stringa_A del foglio 1 e l'insieme delle Stringhe_B del foglio2
i = 1
Do
If String_A = Mr(i, 1) Then
If StrRange Is Nothing Then
Set StrRange = Range(Mr(i, 2)).Resize(4, 3)
Nr = 1
Else
Set StrRange = Union(StrRange, Range(Mr(i, 2)).Resize(4, 3))
Nr = Nr + 1
End If
End If
i = i + 1
Loop Until i = UBound(Mr)
nStop = Timer - nStart
Foglio1.Range("G1").Value = nStop
If StrRange Is Nothing Then
MsgBox "Non sono state trovate occorrenze"
Else
Foglio2.Select
StrRange.Select
MsgBox "Sono state trovate Nr. " & Nr & " occorrenze." & _
vbCrLf & vbCrLf & "tempo impiegato: " & nStop & " secondi", vbInformation
End If
Set RangeF = Nothing
Set StrRange = Nothing
Erase Mr
End Sub
|
If StrRange Is Nothing Then
Set StrRange = Foglio2.Range(Mr(i, 2)).Resize(4, 3)
Nr = 1
Else
Set StrRange = Union(StrRange, Foglio2.Range(Mr(i, 2)).Resize(4, 3))
Nr = Nr + 1
End If
|
