Condividi:        

EXCEL Usare macro aggiornando riferimenti

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

EXCEL Usare macro aggiornando riferimenti

Postdi Francesco53 » 28/05/10 16:26

Un saluto a tutto il forum, ho questa difficoltà:
Ho creato una macro abbinata ad un pulsante, in cui ho dei riferimenti precisi.
Il mio problema è quello di dover inserire il pulsante ogni 40 righe, e deve in quel contesto,
aggiornare i riferimenti alla riga in cui si trova, per cui avrò Esempio 10 pulsanti,
abbinati ad una macro che è identica, cambiano solo i riferimenti.
Volevo sapere se esiste una forma per evitare di cambiare in tutti i pulsanti i vari
riferimenti.
Sono nuovo al vba e sicuramente farà pena la macro che includo, ma non sò fare meglio.
Copiando il Range da A1 a BC35, poi mi posiziono alla riga 40,80,120,160e 200, e copio
tutto, compreso il pulsante, ma dovrei cambiare tutti i riferimenti.
Codice: Seleziona tutto
Sub RisultatiGioco()
For tempo = 1 To 1000000
    If tempo = 30000 Then
        Call Ammonizione01
    End If
        If tempo = 50000 Then
            Call Ammonizione02
        End If
            If tempo = 70000 Then
            Call Ammonizione02
            Call Ammonizione01
        End If
Next
Call Infortuni01
Call Infortuni02
For colonna = 9 To 55 Step 46
riga = 2
Randomize
Num = Int(Rnd() * 4)
Cells(riga, colonna) = Num
Next
Call Reti01
Call Reti02
End Sub
Sub Ammonizione01()
col01 = 6
Randomize
Caus01 = Int(Rnd() * 2)
For Prob01 = 0 To Caus01
Num01 = Int(Rnd() * 15)
    If Cells(Num01 + 5, 7) = 1 Then
        Num01 = 0
    End If
    If Cells(Num01 + 5, col01) = 0 Then
       Cells(Num01 + 5, col01) = 1
    Else
       Cells(Num01 + 5, col01) = 1
       Cells(Num01 + 5, col01) = 2
    End If
    If Cells(Num01 + 5, col01) = 2 Then
       Cells(Num01 + 5, col01 + 1) = 1
    End If
    If Cells(Num01 + 5, col01 + 1) = 1 Then
       Cells(Num01 + 5, col01) = 0
    End If
Next
End Sub
Sub Ammonizione02()
col02 = 52
Randomize
Caus02 = Int(Rnd() * 2)
For Prob02 = 0 To Caus02
Num02 = Int(Rnd() * 15)
    If Cells(Num02 + 5, 53) = 1 Then
       Num02 = 0
    End If
    If Cells(Num02 + 5, col02) = 0 Then
       Cells(Num02 + 5, col02) = 1
    Else
       Cells(Num02 + 5, col02) = 1
       Cells(Num02 + 5, col02) = 2
    End If
        If Cells(Num02 + 5, col02) = 2 Then
            Cells(Num02 + 5, col02 + 1) = 1
        End If
            If Cells(Num02 + 5, col02 + 1) = 1 Then
                Cells(Num02 + 5, col02) = 0
        End If
Next
End Sub
Sub Infortuni01()
col01 = 8
Randomize
Num01 = Int(Rnd() * 15)
    If Cells(Num01 + 5, 7) = 1 Then
        Num01 = 0
    End If
    If Cells(Num01 + 5, col01) = 0 Then
       Cells(Num01 + 5, col01) = 1
    End If
End Sub

Sub Infortuni02()
col02 = 54
Randomize
Num02 = Int(Rnd() * 15)
    If Cells(Num02 + 5, 54) = 1 Then
        Num02 = 0
    End If
    If Cells(Num02 + 5, col02) = 0 Then
       Cells(Num02 + 5, col02) = 1
    End If
End Sub
Sub Reti01()
Goal01 = Cells(2, 9).Value
col01 = 9
For ReteA = 0 To Goal01 - 1
Randomize
Num01 = Int(Rnd() * 10)
    Ret01 = Cells(Num01 + 6, col01).Value
    If Cells(Num01 + 6, col01) = 0 Then
       Cells(Num01 + 6, col01) = 1
    Else
    Cells(Num01 + 6, col01) = Ret01 + 1
    End If
    Next
End Sub

Sub Reti02()
Goal02 = Cells(2, 55).Value
col02 = 55
For ReteB = 0 To Goal02 - 1
Randomize
Num02 = Int(Rnd() * 10)
    Ret02 = Cells(Num02 + 6, col02).Value
    If Cells(Num02 + 6, col02) = 0 Then
       Cells(Num02 + 6, col02) = 1
    Else
    Cells(Num02 + 6, col02) = Ret02 + 1
    End If
    Next
End Sub

Ringrazio chi può aiutarmi e consigliarmi.
Francesco
S.O. Windows 10 e Office 2007
Avatar utente
Francesco53
Utente Senior
 
Post: 811
Iscritto il: 20/02/10 18:45

Sponsor
 

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Francesco53 » 29/05/10 16:01

Un saluto a tutti,
ho modificato la prima macro postata con varie prove lo resa più semplice.
Allego nuova annulla la precedente, ma la richiesta rimane uguale .
Codice: Seleziona tutto
Sub RisultatiGioco()
For tempo = 1 To 1000000
    If tempo = 30000 Then
        Call Ammonizioni
    End If
        If tempo = 50000 Then
            Call Infortuni
        End If
            If tempo = 70000 Then
                Call Ammonizioni
            End If
Next
For colonna = 9 To 55 Step 46
riga = 2
Randomize
Num = Int(Rnd() * 4)
Cells(riga, colonna) = Num
Next
Call Reti
End Sub
Sub Ammonizioni()
Pos = 5
For Ris = 7 To 53 Step 46
For col = 6 To 52 Step 46
Randomize
Caus = Int(Rnd() * 2)
For Prob = 0 To Caus
Num = Int(Rnd() * 15)
    If Cells(Num + Pos, Ris) = 1 Then
        Num = 0
    End If
    If Cells(Num + Pos, col) = 0 Then
       Cells(Num + Pos, col) = 1
    Else
       Cells(Num + Pos, col) = 1
       Cells(Num + Pos, col) = 2
    End If
    If Cells(Num + Pos, col) = 2 Then
       Cells(Num + Pos, col + 1) = 1
    End If
    If Cells(Num + Pos, col + 1) = 1 Then
       Cells(Num + Pos, col) = 0
    End If
Next
Next
Next
End Sub
Sub Infortuni()
Pos = 5
For col = 8 To 54 Step 46
Randomize
Num = Int(Rnd() * 15)
    If Cells(Num + Pos, col) = 1 Then
        Num = 0
    End If
    If Cells(Num + Pos, col) = 0 Then
       Cells(Num + Pos, col) = 1
    End If
Next
End Sub
Sub Reti()
Pos = 6
For col = 9 To 55 Step 46
Goal = Cells(2, col).Value
For Rete = 0 To Goal - 1
Randomize
Num = Int(Rnd() * 10)
    Rete01 = Cells(Num + Pos, col).Value
    If Cells(Num + Pos, col) = 0 Then
       Cells(Num + Pos, col) = 1
    Else
    Cells(Num + Pos, col) = Rete01 + 1
    End If
Next
Next
End Sub

Grazie a chi mi può aiutare.
Buon fine settimana a tutti,
Francesco
S.O. Windows 10 e Office 2007
Avatar utente
Francesco53
Utente Senior
 
Post: 811
Iscritto il: 20/02/10 18:45

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Anthony47 » 31/05/10 02:25

Non ho capito bene la tua domanda, ma nel caso ti possa servire questa istruzione ti restituisce (nella variabile PosizPuls) l' indirizzo della cella su cui giace l' angolo alto/sx del pulsante che ha attivato una macro:
Codice: Seleziona tutto
PosizPuls = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address

Se questa informazione non ti e' utile allora scrivi ancora con qualche informazione in piu' sull' esatto problema.

Ciao
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Francesco53 » 31/05/10 07:55

Grazie Anthony,
è quello che cercavo.
Buona giornata
Francesco
S.O. Windows 10 e Office 2007
Avatar utente
Francesco53
Utente Senior
 
Post: 811
Iscritto il: 20/02/10 18:45

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Francesco53 » 31/05/10 17:23

Ciao Anthony un'altra richiesta sempre sullo stesso argomento:
PosizPuls = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
mi da il riferimento $C$26, chiedo se è possibile avere il riferimento in
formato VBA Cells( 3, 26), per poter fare poi operazioni matematiche col
valore trovato, Es: Cells( 3 - 2, 26 - 16).
Grazie
Francesco
S.O. Windows 10 e Office 2007
Avatar utente
Francesco53
Utente Senior
 
Post: 811
Iscritto il: 20/02/10 18:45

Re: EXCEL Usare macro aggiornando riferimenti

Postdi ricky53 » 31/05/10 23:08

Ciao,
la colonna si ottiene con
ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column

e la riga con
ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row

ossia sostituendo "Address" con ...

facile no!!!

Ciao da Ricky53
Dice il vecchio saggio provare e riprovare è l'unica strada per imparare

Più chiara è la vostra spiegazione
Più immediata sarà la nostra soluzione


. . . . . . . . . .
S.O. W10; Office 2003-10-13-16-19
Avatar utente
ricky53
Utente Senior
 
Post: 4565
Iscritto il: 11/04/09 19:29
Località: Italia

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Anthony47 » 01/06/10 00:32

facile no!!!
Troooppo facile; ma ne puoi fare a meno inserendo le variazioni tramite "offset"; es il tuo Cells( 3 - 2, 26 - 16) e' equivalente a ActiveSheet.Shapes(Application.Caller).TopLeftCell.Offset(-2, -16)

Ciao a tutti.
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Re: EXCEL Usare macro aggiornando riferimenti

Postdi Francesco53 » 01/06/10 13:34

Grazie,
Francesco
S.O. Windows 10 e Office 2007
Avatar utente
Francesco53
Utente Senior
 
Post: 811
Iscritto il: 20/02/10 18:45


Torna a Applicazioni Office Windows


Topic correlati a "EXCEL Usare macro aggiornando riferimenti":


Chi c’è in linea

Visitano il forum: systemcrack e 41 ospiti