Variaveis em excel vba

rdvision

New Member
Joined
May 22, 2015
Messages
1
Olá a todos!
Estou com um simples, mas sério problema.
Tenho tentado compreender as variáveis no código a seguir.. eita dor de cabeça!

Sub TESTE_1()
'Nesta a macro funciona perfeitamente, pois não faço menção a variável "resultado" da linha 5
'E sim a conotação Cells(2, 1) na linha 7. (OBS: inserido em A1=4 em B1=5, valor obitido em A2: 4)


Maior = WorksheetFunction.Max(Cells(1, 1), Cells(1, 2))
Menor = WorksheetFunction.Min(Cells(1, 1), Cells(1, 2))

Valor1 = Cells(1, 1)
Valor2 = Cells(1, 2)
Resultado = Cells(2, 1)

If Maior > 3 Then
Cells(2, 1) = Valor1
End If

End Sub

Sub TESTE_2()
'Nesta a macro NÃO funciona, pois faço menção a variável "resultado" na linha 7. (OBS: inserido em A1=4 em B1=5, valor obitido em A2: empty ou seja "nada")

Maior = WorksheetFunction.Max(Cells(1, 1), Cells(1, 2))
Menor = WorksheetFunction.Min(Cells(1, 1), Cells(1, 2))

Valor1 = Cells(1, 1)
Valor2 = Cells(1, 2)
Resultado = Cells(2, 1)

If Maior > 3 Then
Resultado = Valor1
End If

Alguém pude me ajudar a dizer porque não funciona a macro "Teste_2"
Muito obrigado
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Olá a todos!
Estou com um simples, mas sério problema.
Tenho tentado compreender as variáveis no código a seguir.. eita dor de cabeça!

Sub TESTE_1()
'Nesta a macro funciona perfeitamente, pois não faço menção a variável "resultado" da linha 5
'E sim a conotação Cells(2, 1) na linha 7. (OBS: inserido em A1=4 em B1=5, valor obitido em A2: 4)


Maior = WorksheetFunction.Max(Cells(1, 1), Cells(1, 2))
Menor = WorksheetFunction.Min(Cells(1, 1), Cells(1, 2))

Valor1 = Cells(1, 1)
Valor2 = Cells(1, 2)
Resultado = Cells(2, 1)

If Maior > 3 Then
Cells(2, 1) = Valor1
End If

End Sub

Sub TESTE_2()
'Nesta a macro NÃO funciona, pois faço menção a variável "resultado" na linha 7. (OBS: inserido em A1=4 em B1=5, valor obitido em A2: empty ou seja "nada")

Maior = WorksheetFunction.Max(Cells(1, 1), Cells(1, 2))
Menor = WorksheetFunction.Min(Cells(1, 1), Cells(1, 2))

Valor1 = Cells(1, 1)
Valor2 = Cells(1, 2)
Resultado = Cells(2, 1)

If Maior > 3 Then
Resultado = Valor1
End If

Alguém pude me ajudar a dizer porque não funciona a macro "Teste_2"
Muito obrigado

Olá rdvision,
Talvez você já tenha solucionado, mas vai a dica:

Quando você quiser atribuir um valor à uma variável, o valor deve estar à direita do sinal de "=".

Na linha: < Resultado = Cells(2, 1) > a variável "Resultado" recebe o valor de "A2", ou seja, "nada".
Neste caso, deve-se inverter: Cells(2, 1) = Resultado
Aí sim a célula "A2" recebe o valor de "Resultado".

Mas ainda não está pronto, pois "Resultado" ainda está vazio.

Esta atribuição deve aparecer dentro do "If".
Já que só vai verificar qual é o número maior dentro desta instrução.

Acrescente na última linha dentro da instrução "If":
Cells(2, 1) = Resultado 'preenche a célula "A2" com o valor do resultado.

O código ficará assim:

Sub TESTE_2()


maior = WorksheetFunction.Max(Cells(1, 1), Cells(1, 2))
menor = WorksheetFunction.Min(Cells(1, 1), Cells(1, 2))


Valor1 = Cells(1, 1)
Valor2 = Cells(1, 2)


If maior > 3 Then
Resultado = Valor1
Cells(2, 1) = Resultado 'preenche a célula "A2" com o valor do resultado.
End If


End Sub


Espero ter ajudado!
Adriano Bortoloto
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top