Registrar formulario en diferentes hojas al seleccionar un combobox

Cuento con el siguiente código, lo que quisiera es que al seleccionar el datos"MODERNO" del combobox2, los datos se registren en la hoja "SMK FY20", la cual tiene las misma características que la hoja en la cual se están copiando los datos

Private Sub CommandButton1_Click()
   ult = Sheets("ON TRADE- MAYORISTAS FY20").Cells(Rows.Count, 6).End(xlUp).Row
If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox5 = "" Or TextBox6 = "" Or ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3 = "" Or ComboBox4 = "" Or ComboBox5 = "" Then
    MsgBox "Escriba todos los datos"
Else
    Select Case ComboBox4.ListIndex
        Case 0: col = "J" ' soles
        Case 1: col = "K" ' dolares
    End Select
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 1) = TextBox1
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 2) = CDate(TextBox2)
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 3) = ComboBox1
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 4) = TextBox6
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 5) = ComboBox2
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 6) = TextBox3
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 7) = ComboBox3
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 8) = TextBox4
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 9) = ComboBox4
    If col = "J" Then
        Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, col).NumberFormat = "[$S/-es-PE]* #,##0.00"
    Else
        Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, col).NumberFormat = "[$$-en-US]* #,##0.00"
    End If
    '
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, col) = Val(TextBox5.Text) ' soles o dólares
    Sheets("ON TRADE- MAYORISTAS FY20").Cells(ult + 1, 12) = ComboBox5
    x = ult + 1
    rango = "A" & x & ":L" & x
    Sheets("FORMULARIO").Select
    Range(rango).Select
MsgBox "Se ha escrito correctamente su registro"
response = MsgBox("¿Desea añadir otro registro?", _
vbYesNo)
If response = vbYes Then
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
ComboBox4.Text = ""
ComboBox5.Text = ""
TextBox1.SetFocus
Else
Unload Me
UserForm1.Show
End If
End If
End Sub

2 Respuestas

Respuesta
1

Prueba lo siguiente:

Private Sub CommandButton1_Click()
  Dim col As String, sh As Worksheet, ult As Long, rango As String
  Dim response As String, i As Long
  '
  If TextBox1 = "" Or TextBox2 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox5 = "" Or TextBox6 = "" Or ComboBox1 = "" Or ComboBox2 = "" Or ComboBox3 = "" Or ComboBox4 = "" Or ComboBox5 = "" Then
    MsgBox "Escriba todos los datos"
    Exit Sub
  End If
  '
  Select Case ComboBox4.ListIndex
    Case 0: col = "J" ' soles
    Case 1: col = "K" ' dolares
  End Select
  '
  Select Case ComboBox2.Value
    Case "MODERNO": Set sh = Sheets("SMK FY20")
    Case "OTRO": Set sh = Sheets("ON TRADE- MAYORISTAS FY20")
  End Select
  '
  ult = sh.Cells(Rows.Count, "F").End(xlUp).Row + 1
  sh.Cells(ult, 1) = TextBox1
  sh.Cells(ult, 2) = CDate(TextBox2)
  sh.Cells(ult, 3) = ComboBox1
  sh.Cells(ult, 4) = TextBox6
  sh.Cells(ult, 5) = ComboBox2
  sh.Cells(ult, 6) = TextBox3
  sh.Cells(ult, 7) = ComboBox3
  sh.Cells(ult, 8) = TextBox4
  sh.Cells(ult, 9) = ComboBox4
  If col = "J" Then
    sh.Cells(ult, col).NumberFormat = "[$S/-es-PE]* #,##0.00"
  Else
    sh.Cells(ult, col).NumberFormat = "[$$-en-US]* #,##0.00"
  End If
  '
  sh.Cells(ult, col) = Val(TextBox5.Text) ' soles o dólares
  sh.Cells(ult, 12) = ComboBox5
  'x = ult
  rango = "A" & ult & ":L" & ult
  Sheets("FORMULARIO").Select
  Range(rango).Select
  MsgBox "Se ha escrito correctamente su registro"
  response = MsgBox("¿Desea añadir otro registro?", vbYesNo)
  If response = vbYes Then
    For i = 1 To 5
      Controls("TextBox" & i).Text = ""
      Controls("ComboBox" & i).Text = ""
    Next
    TextBox6.Text = ""
    TextBox1.SetFocus
  Else
    Unload Me
    UserForm1.Show
  End If
End Sub

Muchas gracias estimado

amigo quisiera tener la opción que al ingresar los  datos, se coloree la celda del dato copiado del textbox1 y que sea de color celeste 

Después de esta línea

sh.Cells(ult, 1) = TextBox1

Pon esta:

sh.Cells(ult, 1).Interior.ColorIndex = 28
Respuesta
1

Esto te puede aportar algo más

https://macrosenexcel.com/macro-vba-busca-datos-coincidentes-con

aca tienes mas acerca de combobox

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas