Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Giancarlo,
la mia esigenza è schematicamente qui riassunta :
1)dato un elenco di voci generiche in Fo1 p.es : olio, carciofi, pollo
- trovare per ciascuna voce la corrispondenza esatta su fo Tab_Alimenti, per es. : olio di oliva - carciofi microonde - pollo,petto,cotto
- selezionare quella di mio interesse,
4 Copiare sia la denominazione che l'intera riga su foglio1 sovrascrivendo la voce generica, oppure sotto e annullando alla fine le voci generiche .
Un po troppo complicato per le mie forze, ma le due macro nell'esempio, sapientemente assemblate, dovrebbero riuscire a risolvere il tutto.
Prova qualcosa del genere:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim WB As Workbook
Dim srcSH As Worksheet, destSH As Worksheet
Dim srcRng As Range, destRng As Range
Dim arrDatabase As Variant, arrReport As Variant
Dim Res As Variant
Dim sStr As String, sChiave_Ricerca As String
Dim sMsg As String, sTitle As String
Dim iButtons As VbMsgBoxStyle
Dim iRow As Long, jRow As Long
Dim i As Long, j As Long, k As Long
Dim iCtr As Long
Dim UB As Long, UB2 As Long
Const sFoglio_Report As String = "Foglio1" '<<=== Modifica
Const sFoglio_Database As String = "Tab_Alimenti" '<<=== Modifica
Const iPrimaRiga_Database As Long = 5 '<<=== Modifica
Const sUltimaColonna_Database As String = "Z" '<<=== Modifica
Const iPrimaRiga_Report As Long = 6 '<<=== Modifica
sChiave_Ricerca = InputBox("Cosa vuoi cercare ?")
If sChiave_Ricerca = vbNullString Then
sMsg = "L'Elaborazione termina senza effettuare la ricerca"
sTitle = "OPERAZIONE CANCELLATA"
iButtons = vbCritical
GoTo XIT
End If
Set WB = ThisWorkbook
With WB
Set srcSH = .Sheets(sFoglio_Database)
Set destSH = .Sheets(sFoglio_Report)
End With
With srcSH
iRow = LastRow(srcSH, .Columns("A:A"))
Set srcRng = .Range("A" & iPrimaRiga_Database & ":" _
& sUltimaColonna_Database & iRow)
arrDatabase = srcRng.Value
UB = UBound(arrDatabase)
UB2 = UBound(arrDatabase, 2)
ReDim arrReport(1 To UB, 1 To UB2)
End With
With destSH
jRow = LastRow(destSH, .Columns("A:A"), iPrimaRiga_Report)
Set destRng = .Range("A" & iPrimaRiga_Report & ":A" & jRow).EntireRow
destRng.ClearContents
End With
For i = 1 To UB
sStr = arrDatabase(i, 1)
Res = InStr(1, sStr, sChiave_Ricerca, vbTextCompare)
If Res Then
iCtr = iCtr + 1
For j = 1 To UB2
arrReport(iCtr, j) = arrDatabase(i, j)
Next j
End If
Next i
If iCtr Then
On Error GoTo XIT
Application.ScreenUpdating = False
destRng.Resize(iCtr, UB2).Value = arrReport
sMsg = iCtr & " risultati trovati per la query"
sTitle = "Report"
iButtons = vbInformation
Else
sMsg = "La chiave ricerca " & sChiave_Ricerca _
& " non è stata trovata!"
sTitle = "Nessuna corrispondenza trovata per la query"
iButtons = vbCritical
End If
XIT:
Application.ScreenUpdating = True
Call MsgBox( _
Prompt:=sMsg, _
Buttons:=iButtons, _
Title:=sTitle)
End Sub
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1, _
Optional sPassword As String)
Dim bProtected As Boolean
With SH
If Rng Is Nothing Then
Set Rng = .Cells
End If
bProtected = .ProtectContents = True
If bProtected Then
.Unprotect Password:=sPassword
End If
End With
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
If bProtected Then
SH.Protect Password:=sPassword, _
UserInterfaceOnly:=True
End If
End Function
'<<=========
===
Regards,
Norman