Estou com um pequeno desafio no meu código, gostaria de saber se alguém pode me ajudar.
Tenho uma situação em que no Formulário 01 são selecionados diversos itens para um lançamento de dados.
Esses itens são armazenados em variáveis.
A dificuldade que tenho é saber se consigo atualizar o nome da variável dentro de um loop e, consequentemente, o valor da variável.
No exemplo a seguir, o modelo que tentei e outras diversas variações não funcionaram.
Seria fácil resolver com um IF, mas no meu projeto o número de loops é imprevisível e alto e não ficaria interessante nem possível fazer tantos IFs.
Segue o código base:
Código: Selecionar todos
Option Explicit
Public PROD1, PROD2, PROD3 as String
Public CICLO as Integer
Código: Selecionar todos
Private Sub Carregar_Dados()
If ckprod1.value = true and PROD1.value <> Empty then
MsgBox "Selecione o Produto1!"
Elseif ckprod2.value = true and PROD2.value <> Empty then
MsgBox "Selecione o Produto2!"
Elseif ckprod3.value = true and PROD3.value <> Empty then
MsgBox "Selecione o Produto3!"
End if
PROD1 = txtprod1.value
PROD2 = txtprod2.value
PROD3 = txtprod3.value
If PROD1.value <> Empty then
CICLO = 1
Elseif PROD2.value <> Empty then
CICLO = 2
Elseif PROD3.value <> Empty then
CICLO = 3
End if
End Sub
Código: Selecionar todos
Private Sub Carregar_Dados()
Dim L As Integer
Dim LINHA As Long
'Procurar a primeira célula vazia
Worksheets("Lançamentos").Range("A1").Select
Do
If Not (IsEmpty(ActiveCell)) Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
LINHA = ActiveCell.Row
'Atribui um valor à variável
For L = 1 To CICLO
PRODUTO = "PROD" & L
'Carregar os dados digitados nas caixas de texto para a planilha
ActiveCell.Offset((LINHA + L), 0).Value = VBA.Format(txtdata.Value, "mm/dd/yyyy")
ActiveCell.Offset((LINHA + L), 1).Value = txtnumerodanota.Value
ActiveCell.Offset((LINHA + L), 2).Value = PRODUTO
Next L
End Sub