Fórmula do Excel 2010, Como escrevo esta fórmula? (VBA)

0

Preciso de um pouco de ajuda dos usuários mais experientes do Excel VBA.

Basicamente eu tenho 2 grupos de 3 células (6 células no total) Se um grupo de 3 células for preenchido, ele precisará aceitá-lo e não retornar um messagebox.

Se um dos dois grupos não estiver preenchido (em branco) ou retornar um erro, ele precisará mostrar um "msgbox de erro"

Os dois grupos de 3 células são: (A39, A40, A41) e (E39, E40, E41)

Isso está atualmente na minha fórmula:

If IsError(Range("E39, E40, E41")) Then MsgBox ("error msgbox"): Exit Sub
If Range("E39, E40, E41") = Blank Then MsgBox ("error msgbox"): Exit Sub

Como adiciono A39, A40, A41 a essa fórmula para que, se o grupo 1 (A39, A40, A41) ou o grupo grupo 2 (E39, E40, E41) são preenchidos, não retorna um "msgbox de erro"?

Eu mesmo tentei mexer com isso, mas ele retornaria um "erro msgbox" se todas as 6 células não estivessem preenchidas.

Eu tentei ver as instruções AND, OR, mas não consigo obter a função que desejo.

Aqui está uma foto que pode ajudar

Qualquer ajuda seria muito apreciada

    
por 12022014 12.02.2014 / 10:17

2 respostas

1

Você tem alguns problemas com seu código. Em primeiro lugar, Range("E39, E40, E41") não se refere a essas três células. Você precisaria separá-los com vírgulas fora das aspas como Range("E39", "E40", "E41") .

Em segundo lugar, para avaliar corretamente se essas células estão vazias, é necessário separá-las na instrução IF , desta forma:

If Range("E39") = "" Or Range("E40") = "" Or Range("E41") = "" Then
    'code here
End If

O mesmo vale para o IsError:

If IsError(Range("E39")) Or IsError(Range("E40")) ...

No entanto, para agrupar a lógica, você precisa pensar de uma maneira diferente, porque isso gerará o erro quando alguma dessas células estiver vazia / contiver um erro. Então, o que você pode considerar é adicionar duas novas variáveis que você define como True ou False dependendo se o grupo está preenchido corretamente ou não. Exemplo:

Dim isFilled1 as Boolean = False
Dim isFilled2 as Boolean = False

If Not Range("E39") = "" Or Not Range("E40") = "" Or Not Range("E41") = "" Then
    'All the cells in this range contain a value so set isFilled1 to True
    isFilled1 = True
End If

If IsError(Range("E39")) Or IsError(Range("E40")) Or IsError(Range("E40")) Then
    'The cells may contain values, but one or more evaluated as an error therefore set isFilled1 to False
    isFilled1 = False
End If

If Not Range("A39") = "" Or Not Range("A40") = "" Or Not Range("A41") = "" Then
    'All the cells in this range contain a value so set isFilled2 to True
    isFilled2 = True
End If

If IsError(Range("A39")) Or IsError(Range("A40")) Or IsError(Range("A40")) Then
    'The cells may contain values, but one or more evaluated as an error therefore set isFilled2 to False
    isFilled2 = False
End If

'Now check if isFilled1 or isFilled2 are True

If isFilled1 = True or isFilled2 = True Then
    'Hooray, one of the groups is properly filled
Else
    'Neither group is properly filled; show msgbox
    MsgBox("error msgbox")
End If

Você pode precisar ajustar esse código para se adequar à sua situação, mas é assim que eu abordaria isso.

    
por 12.02.2014 / 11:07
0

Eu tive que ajustar um pouco para que funcionasse bem com o arquivo excel que eu estava usando.

Com agradecimentos a exantas (pelo código) e Dave Rook (por editar meu post) , este é o código final:

Dim isFilled1 As Boolean
Dim isFilled2 As Boolean

If Not Range("E39") = "" And Not Range("E40") = "" And Not Range("E41") = "" Then
'Set isFilled1 To True if these cells don't return an empty cell
isFilled1 = True
End If
If IsEmpty(Range("E39")) Or IsEmpty(Range("E40")) Or IsEmpty(Range("E41")) Then
'If these cells don't have a value, set isFilled1 to False
isFilled1 = False
End If

If Not Range("A39") = "" And Not Range("A40") = "" And Not Range("A41") = "" Then
'Set isFilled1 To True if these cells don't return an empty cell
isFilled1 = True
End If
If IsEmpty(Range("A39")) Or IsEmpty(Range("A40")) Or IsEmpty(Range("A41")) Then
'If these cells don't have a value, set isFilled1 to False
isFilled1 = False
End If

If isFilled1 = True Or isFilled2 = True Then
'One of the groups is properly filled
Else
'Neither group is properly filled; show msgbox
MsgBox ("error msgbox"): Exit Sub

Eu tive que substituir a fórmula nas células E39, E40, E41, A39, A40, A41 com:

= IF (ISERROR (old_formula); ""; (old_formula) para exibir uma célula vazia / vazia quando um erro é retornado.

    
por 13.02.2014 / 14:13