Ciao Aldo,
Scusatemi, forse non ho formulato bene la mia domanda. Intendevo riferirmi all'inserimento di un opportuno codice Visual Basic con condizione, probabilmente IF condizione THEN, ELSE, rispetto ad un determinato RANGE. Spiego meglio cosa vorrei ottenere, precisando
che il programma di cui dispongo è Excel 2010 e che il sistema operativo è Windows 7 Professional a 64 bit.
In un file Excel l' impostazione iniziale prevede, dalla cella D1 alla cella D41, l'inserimento di 41 formule "E" che, ognuna nel proprio rigo, restituisce risultato "VERO", quando tutte le formule "SE" collegate, restituiscono "VERO". Quindi:
In D1, il risultato della formula "E" è "VERO", se tutte le formule "SE" collegate restituiscono "VERO";
In D2, il risultato della formula "E" è "VERO", se tutte le formule "SE" collegate restituiscono "VERO";
In D3, il risultato della formula "E" è "VERO", se tutte le formule "SE" collegate restituiscono "VERO";
Così via per tutti i 41 righi.
Attraverso l'inserimento di adeguato codice Visual Basic vorrei:
l'emissione di un beep di sistema o l'apertura di un file sonoro con formato .wav, residente in Hard-disk, sfruttando Playsound, tutte le volte che le celle da D1 a D41 restituiscono "VERO".
Vi ringrazio la cortesia e per la disponibilità che vorrete dimostrare.
Prova qualcosa del genere:
- Fai clic dx sulla linguetta del foglio di interesse
- Seleziona l'opzione Visualizza Codice dal ****
menu contestuale risultante
- Incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range
Dim arrOld As Variant, arrNew As Variant, arrNew2 As Variant
Dim i As Long
Dim bFlag As Boolean
Const sIntervalloFormule As String = "D1:D41" '<<=== Modifica
Const sWavFile As String = _
"C:\Windows\Media\Windows Ringout.wav" '<<=== Modifica
Set Rng = Me.Range(sIntervalloFormule)
On Error Resume Next
Set Rng2 = Rng.Precedents
Set Rng3 = Intersect(Rng2, Target)
On Error GoTo 0
If Not Rng3 Is Nothing Then
Set Rng4 = Intersect(Rng3.Dependents, Rng)
End If
If Not Rng4 Is Nothing Then
arrNew = Rng3.Formula
arrNew2 = Rng.Value
On Error GoTo XIT
With Application
.EnableEvents = False
.Undo
End With
With Rng
arrOld = .Value
.Interior.ColorIndex = xlNone
For i = 1 To UBound(arrOld)
If arrOld(i, 1) = False And arrNew2(i, 1) = True Then
bFlag = True
.Cells(i).Interior.Color = vbRed
End If
Next i
End With
End If
XIT:
Rng3.Formula = arrNew
If bFlag Then
For i = 1 To 2
Call DingDong(sWavFile)
Next i
End If
Application.EnableEvents = True
End Sub
'<<=========
- Alt+F11 per aprire l'editor di VBA
- Alt+IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
#If VBA7 And Win64 Then
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As LongPtr, _
ByVal dwFlags As LongPtr) _
As Long
#Else
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) _
As Long
#End If
'--------->>
Public Sub DingDong(sFile)
PlaySound sFile, &H0, &H8
End Sub
'<<=========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel
- Salva il file con l’estensione xlsm
Potresti scaricare il mio file di prova Aldo20170312 a:
https://www.dropbox.com/s/wkr55s51khnhq9e/Aldo20170312.xlsm?dl=0
===
Regards,
Norman
