Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
... una correzione e qualche miglioramento:
Public Sub TrovaNomiFraValori()
Const cPrc = "TrovaNomiFraValori"
On Error GoTo ErrH
Const cstrPrompt = "Immetti un numero," & vbNewLine & _
"oppure due numeri separati da uno spazio."
Const xlTypeText = 2
Const cstrDat = "B10:B200"
Const cstrNum = "E10:E200"
Dim app As Excel.Application
Dim wsh As Excel.Worksheet
Dim strPrompt As String
Dim vntInput As Variant
Dim strInput() As String
Dim lngTmp As Long
Dim lngBgn As Long
Dim lngEnd As Long
Dim e As Long
Dim avntNum As Variant
Dim avntDat As Variant
Dim i As Long
Dim strResult As String
Dim lngCount As Long
Set app = GetObject(Class:="Excel.Application")
Set wsh = app.ActiveSheet
If Not IsDate("2000-" & wsh.Name & "-01") Then
Err.Raise vbObjectError + 1, _
cPrc, _
"Il nome del foglio attivo non è il nome di un mese."
End If
strPrompt = cstrPrompt
vntInput = ""
Do
vntInput = app.InputBox(Prompt:=strPrompt, _
Title:=cPrc, _
Default:=vntInput, _
Type:=xlTypeText)
If VarType(vntInput) = vbBoolean Then GoTo ExtP
strInput = Split(Trim$(vntInput), " ")
On Error Resume Next
lngBgn = CLng(strInput(LBound(strInput)))
lngEnd = CLng(strInput(UBound(strInput)))
e = Err.Number
On Error GoTo ErrH
If e Then
strPrompt = "ATTENZIONE! Immettere numeri." & _
vbNewLine & vbNewLine & cstrPrompt
Else
If lngBgn > lngEnd Then
lngTmp = lngBgn
lngBgn = lngEnd
lngEnd = lngTmp
End If
Exit Do
End If
Loop
With wsh
avntNum = .Range(cstrNum).Value
avntDat = .Range(cstrDat).Value
End With
For i = LBound(avntNum) To UBound(avntNum)
If lngBgn <= avntNum(i, 1) And avntNum(i, 1) <= lngEnd Then
If Not (LenB(avntDat(i, 1)) = 0 And avntNum(i, 1) = 0) Then
lngCount = lngCount + 1
strResult = strResult & vbNewLine & _
avntNum(i, 1) & vbTab & _
"'" & avntDat(i, 1) & "'"
End If
End If
Next
strPrompt = " con valore"
If lngBgn = lngEnd Then
strPrompt = strPrompt & _
" uguale a " & CStr(lngBgn)
Else
strPrompt = strPrompt & _
" compreso fra " & CStr(lngBgn) & _
" e " & CStr(lngEnd)
End If
Select Case lngCount
Case 0
strPrompt = "Nessun nominativo presente" & strPrompt
Case 1
strPrompt = "Trovato un nominativo presente" & strPrompt
Case Else
strPrompt = "Trovato " & CStr(lngCount) & _
" nominativi presenti" & strPrompt
End Select
strPrompt = strPrompt & ":" & strResult
MsgBox strPrompt, vbInformation, cPrc
ExtP: On Error Resume Next
Set wsh = Nothing
Set app = Nothing
On Error GoTo 0
Exit Sub
ErrH:
MsgBox Err.Description
Resume ExtP
End Sub