l'obiettivo è quello di modificare il testo della formula nel range P4:Q75 se il mese indicato in I21 è diverso da quello presente in I23.
Ho prima provato a sviluppare una mio script:
- Codice: Seleziona tutto
Sub cambia_mese0()
Dim xFind As Range, xRep As Range, xRg As Range
On Error Resume Next
xRg = Range("P4:Q75") 'Cells
xFind = Range("I21")
xRep = Range("I23")
'If xRep <> xFind Then
If xFind = “False” Or xRep = “False” Then Exit Sub
If xRep <> xFind Then
xRg.Replace xFind, xRep, xlPart, xlByRows, False, False, False, False
End Sub
Risultato:
Poi ho provato (rinunciando al controllare se i mesi fossero differenti) ad adattare una macro di Anthony:
- Codice: Seleziona tutto
Sub CambiaMese2()
Dim fCell As Range, aForm As String, oPart As Variant, nPart As Variant
Dim Ws As Worksheet
'
oPart = ("I23")
If Len(oPart) = 0 Or oPart = False Then
MsgBox ("Stringa non valida; la macro viene terminata")
Exit Sub
End If
nPart = ("I21")
If Len(oPart) = 0 Or oPart = False Then
MsgBox ("Stringa non valida; la macro viene terminata")
Exit Sub
End If
'
Dim Rng As Range
Set Rng = Range("P4:Q75")
Application.DisplayAlerts = False
Application.EnableEvents = False
If IsNull(Rng.HasFormula) Or Rng.HasFormula Then 'MMM
For Each fCell In Rng
aForm = fCell.Formula
If InStr(1, aForm, oPart, vbTextCompare) > 0 Then
aForm = Replace(aForm, oPart, nPart, , , vbTextCompare)
fCell.Formula = aForm
End If
DoEvents
Next fCell
End If
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
Risultato:
Cosa....tra le tante cose... non ho capito!!
https://www.dropbox.com/s/farwvv0v937px7e/cambiaMese_forum.xlsm?dl=0
Grazie per l'aiuto.