domingo, 1 de julio de 2018

VBA FORMULARIOS Práctica 9



























CÓDIGO DEL FORMULARIO
Private Sub UserForm_Activate()
Dim i As Integer
Sheets("Registrar Pedidos").Select

ult = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To ult Step 1
   ComboBox1.AddItem (Cells(i, 1))
Next

ult = Cells(Rows.Count, 2).End(xlUp).Row
For i = 3 To ult Step 1
   ListBox1.AddItem (Cells(i, 2))
Next

ult = Cells(Rows.Count, 3).End(xlUp).Row
For i = 3 To ult Step 1
   ComboBox2.AddItem (Cells(i, 3))
Next
End Sub
Private Sub ComboBox1_Change()
Dim plato As String
Dim precio1 As Double
ult = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To ult
    Sheets("Plato").Select
    plato = Cells(i, 1)
    precio1 = Cells(i, 2)
    If ComboBox1.Text = plato Then
        TextBox3.Text = precio1
    End If
Next
End Sub
Private Sub ListBox1_Change()
Dim bebida As String
Dim precio2 As Double
ult = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To ult
    Sheets("Bebidas").Select
    bebida = Cells(i, 1)
    precio2 = Cells(i, 2)
    If ListBox1.Text = bebida Then
        TextBox4.Text = precio2
    End If
Next
End Sub
Private Sub ComboBox2_Change()
Dim postre As String
Dim precio3 As Double
ult = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To ult
    Sheets("Postres").Select
    postre = Cells(i, 1)
    precio3 = Cells(i, 2)
    If ComboBox2.Text = postre Then
        TextBox5.Text = precio3
    End If
Next
End Sub
Private Sub CommandButton3_Click()
    TextBox6.Text = CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text)
End Sub
Private Sub CommandButton1_Click()
    Unload UserForm1
    UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
    Unload UserForm1
End Sub
Private Sub CommandButton4_Click()
Sheets("Ultimo Registro").Select
Cells(2, 1) = TextBox1.Text
Cells(2, 2) = TextBox2.Text
Cells(2, 3) = ComboBox1.Text
Cells(2, 4) = ListBox1.Text
Cells(2, 5) = ComboBox2.Text
Cells(2, 6) = TextBox6.Text
If OptionButton1.Value = True Then
    Cells(2, 7) = OptionButton1.Caption
End If
If OptionButton2.Value = True Then
    Cells(2, 7) = OptionButton2.Caption
End If
'Codigo obtenido del grabador de macros
Sheets("Historial de Pedidos").Select
Rows("2:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Sheets("Ultimo Registro").Select
Range("A2:G2").Select
Selection.Copy
Range("A2").Select
Sheets("Historial de Pedidos").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Registrar Pedidos").Select
End Sub

No hay comentarios.:

Publicar un comentario