Notifications
Clear all

Chamar macro ao alterar worksheets

5 Posts
2 Usuários
0 Reactions
1,085 Visualizações
(@teteuvec)
Posts: 6
Active Member
Topic starter
 

Prezados boa noite,
Estou com dificuldade para passar as variáveis a, b e c para função "calcinverter" onde:

a = Worksheets("Inverter Spec (MIGDI").Range("b4").Value = 1600/3000/5000/12000/20000
b = Worksheets("Inverter Spec (MIGDI").Range("b5").Value = 220/220/220/380/380
c = Worksheets("Inverter Spec (MIGDI").Range("b6").Value = 1500/2700/4000/10000/18000

a função "calcinverter" funciona perfeitamente quando chamada por um célula, porém a intensão é fazê-la atualizar quando algum valor é modificado dentro do workbook. O erro apresentado é TIPO INCOMPATÍVEL DE ARGUMENTO BYREF - ERRO DE COMPILAÇÃO

Sendo assim peço auxílio aos senhores para fazer o link entre o sub e a função!

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    a = Worksheets("Inverter Spec (MIGDI").Range("b4").Value
    b = Worksheets("Inverter Spec (MIGDI").Range("b5").Value
    c = Worksheets("Inverter Spec (MIGDI").Range("b6").Value
    Call calcinverter(a, b, c)
End Sub


Function calcinverter(a As String, b As String, c As String)
    Dim xvi() As String
    Dim yvi() As String
    Dim zvi() As String
    Dim i As Integer
    Dim ninv(0 To 9) As Single
    Dim av() As Single
    Dim bv() As Single
    Dim cv() As Single
    Dim maxpower As Single
    Dim minpower As Single

    Sheets("System Specification").Select
    ninv(0) = Range("B3")
    
    xvi = Split(a, "/")
    yvi = Split(b, "/")
    zvi = Split(c, "/")

    ReDim av(4, UBound(xvi))
    ReDim bv(UBound(yvi))
    ReDim cv(UBound(zvi))

    For i = LBound(xvi) To UBound(xvi)
        av(0, i) = CSng(xvi(i))
        bv(i) = CSng(yvi(i))
        cv(i) = CSng(zvi(i))
        av(1, i) = ninv(0)  av(0, i)
        av(2, i) = ninv(0) Mod av(0, i)
            If i > 0 Then
                If av(1, i - 1) > av(1, i) And av(1, i) >= 1 Then
                    ninv(1) = av(1, i)
                    ninv(2) = av(0, i)
                    ninv(3) = av(2, i)
                    ninv(8) = cv(i)
                End If
            Else
                ninv(1) = av(1, i)
                ninv(2) = av(0, i)
                ninv(3) = av(2, i)
                ninv(8) = cv(i)
            End If
    Next i

    If ninv(3) > 0 Then
        For i = LBound(xvi) To UBound(xvi)
            av(3, i) = ninv(3)  av(0, i)
            av(4, i) = ninv(3) Mod av(0, i)
                If i > 0 Then
                    If av(4, i - 1) > av(4, i) And av(4, i - 1) > 0 Then
                        ninv(4) = av(3, i)
                        ninv(5) = av(0, i)
                        ninv(6) = av(4, i)
                        ninv(9) = cv(i)
                    End If
                Else
                    ninv(4) = av(3, i) + 1
                    ninv(5) = av(0, i)
                    ninv(6) = av(4, i)
                    ninv(9) = cv(i)
                End If
        Next i
    End If

    maxpower = ninv(1) * ninv(2) + ninv(4) * ninv(5)
    minpower = ninv(1) * ninv(8) + ninv(4) * ninv(9)
    ninv(7) = ninv(4) + ninv(1)
    Worksheets("System Specification").Range("B13").Value = ninv(7)
End Function
 
Postado : 04/05/2017 10:32 pm
Wagner Morel
(@wagner-morel-vidal-nobre)
Posts: 0
Illustrious Member
 

teteuvec,

Bom dia!

Já tentou assim:

Function calcinverter(ByVal a As String, ByVal b As String, ByVal c As String)

Desenvolvo pequenas soluções em VBA Excel a valores que variam entre R$ 50,00 a R$ 200,00. Se te interessar, entre no meu instagran (vba_excel_desenvolvimento)

Atenciosamente
Wagner Morel

 
Postado : 05/05/2017 5:40 am
(@teteuvec)
Posts: 6
Active Member
Topic starter
 

agora deu outro erro fazendo a alteração, ERRRO EM TEMPO DE EXECUÇÃO "9" - SUBSCRITO FORA DO INTERVALO na variável a.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    a = Worksheets("Inverter Spec (MIGDI").Range("b4").Value
    b = Worksheets("Inverter Spec (MIGDI").Range("b5").Value
    c = Worksheets("Inverter Spec (MIGDI").Range("b6").Value
    Call calcinverter(a, b, c)
End Sub


Function calcinverter(ByVal a As String, ByVal b As String, ByVal c As String)
    Dim xvi() As String
    Dim yvi() As String
    Dim zvi() As String
    Dim i As Integer
    Dim ninv(0 To 9) As Single
    Dim av() As Single
    Dim bv() As Single
    Dim cv() As Single
    Dim maxpower As Single
    Dim minpower As Single

    Sheets("System Specification").Select
    ninv(0) = Range("B3")
    
    xvi = Split(a, "/")
    yvi = Split(b, "/")
    zvi = Split(c, "/")

    ReDim av(4, UBound(xvi))
    ReDim bv(UBound(yvi))
    ReDim cv(UBound(zvi))

    For i = LBound(xvi) To UBound(xvi)
        av(0, i) = CSng(xvi(i))
        bv(i) = CSng(yvi(i))
        cv(i) = CSng(zvi(i))
        av(1, i) = ninv(0)  av(0, i)
        av(2, i) = ninv(0) Mod av(0, i)
            If i > 0 Then
                If av(1, i - 1) > av(1, i) And av(1, i) >= 1 Then
                    ninv(1) = av(1, i)
                    ninv(2) = av(0, i)
                    ninv(3) = av(2, i)
                    ninv(8) = cv(i)
                End If
            Else
                ninv(1) = av(1, i)
                ninv(2) = av(0, i)
                ninv(3) = av(2, i)
                ninv(8) = cv(i)
            End If
    Next i

    If ninv(3) > 0 Then
        For i = LBound(xvi) To UBound(xvi)
            av(3, i) = ninv(3)  av(0, i)
            av(4, i) = ninv(3) Mod av(0, i)
                If i > 0 Then
                    If av(4, i - 1) > av(4, i) And av(4, i - 1) > 0 Then
                        ninv(4) = av(3, i)
                        ninv(5) = av(0, i)
                        ninv(6) = av(4, i)
                        ninv(9) = cv(i)
                    End If
                Else
                    ninv(4) = av(3, i) + 1
                    ninv(5) = av(0, i)
                    ninv(6) = av(4, i)
                    ninv(9) = cv(i)
                End If
        Next i
    End If

    maxpower = ninv(1) * ninv(2) + ninv(4) * ninv(5)
    minpower = ninv(1) * ninv(8) + ninv(4) * ninv(9)
    ninv(7) = ninv(4) + ninv(1)
    Worksheets("System Specification").Range("B13").Value = ninv(7)
End Function
 
Postado : 05/05/2017 7:36 am
Wagner Morel
(@wagner-morel-vidal-nobre)
Posts: 0
Illustrious Member
 

OK.

Então... poste a planilha. Precisamos rodar e utilizar o depurador para identificar o que está havendo.

Desenvolvo pequenas soluções em VBA Excel a valores que variam entre R$ 50,00 a R$ 200,00. Se te interessar, entre no meu instagran (vba_excel_desenvolvimento)

Atenciosamente
Wagner Morel

 
Postado : 05/05/2017 9:29 am
(@teteuvec)
Posts: 6
Active Member
Topic starter
 

Resolvido!

Basta colocar o seguinte comando (Application.EnableEvents) ao chamar a função no sub:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim inv() As String
    Dim ninv As String
    Dim rangepower As String
    Dim noteinv As String
            
    Application.EnableEvents = False
    Call calcinverter
    inv = Split(calcinverter, "/")
    ninv = inv(0)
    rangepower = inv(1) & " <--> " & inv(2)
    noteinv = inv(3) & " x " & inv(4) & " and " & inv(5) & " x " & inv(6)
    Worksheets("System Specification").Range("B13") = ninv
    Worksheets("System Specification").Range("C13") = rangepower
    Worksheets("B. Cell Validation").Range("O100") = noteinv
    Application.EnableEvents = True
End Sub
 
Postado : 14/05/2017 9:58 pm