-
Hola chicos....baje un libro automatizado con macros VB de internet. Lo estoy aplicando para llevar una contabilidad de medicamento en una farmacia.
Bueno la cuestion es que quiero cambiar una consulta que se le hace a una tabla de excel a traves de un ComboBox1 y lo devuelve en un TextBox1.
El ComboBox1 lleva todos los codigos de los productos que obtine de la Hoja5 (STOCK), o sea en A2 en la planilla de excel; en dicho comboBox, al elegir un codigo, me cambia el TextBox1 con el nombre del medicamento correspondiente al codigo q seleccione, que se encuentra en la misma hoja en B2 (este paso se puede ver en el archivo adjunto 001.jpg).
Ahora a mi me gustaria cambiar este tipo de consulta, o sea, en vez de elegir codigo y arroge el nombre del producto, quiero elegir el prod y me arroje ese codigo en un Label por ejemplo o en un textbox.
Y nombre pasaria a ser ComboBox y los codigos en un TextBox o Label.
Bueno y mi inconveniente esta en la programacion q no se como se hace...
Les dejo el codigo de la macro VB de mi planilla excel, quizas para alguien sea pan comido este tema.....
Private Sub ComboBox1_Click()
Dim i As Integer
Dim j As Integer
Dim final As Integer
Dim FINAL2 As Integer
For i = 2 To 1000
If Hoja5.Cells(i, 1) = "" Then
final = i - 1
Exit For
End If
Next
For i = 2 To 1000
If Hoja6.Cells(i, 1) = "" Then
FINAL2 = i - 1
Exit For
End If
Next
For i = 2 To final
If ComboBox1 = Hoja5.Cells(i, 1) Then
TextBox1 = Hoja5.Cells(i, 2)
Exit For
End If
Next
For j = 1 To FINAL2
If ComboBox1 = Hoja6.Cells(j, 1) Then
TextBox8 = Hoja6.Cells(j, 3)
Exit For
End If
Next
End Sub
Private Sub ComboBox1_Enter()
Dim i As Integer
Dim j As Integer
Dim H As Integer
Dim final As Integer
Dim tareas As String
ComboBox1.BackColor = &H80000005
For i = 1 To ComboBox1.ListCount
ComboBox1.RemoveItem 0
Next i
For j = 2 To 1000
If Hoja5.Cells(j, 1) = "" Then
final = j - 1
Exit For
End If
Next
For H = 2 To final
tareas = Hoja5.Cells(H, 1)
ComboBox1.AddItem (tareas)
Next
'End If
End Sub
Private Sub CommandButton1_Click()
Dim validar As Boolean
Dim validarfecha As Boolean
If TextBox1 = "" Then
UserForm10.Show
Exit Sub
End If
If TextBox2 = "" Then
UserForm11.Show
Exit Sub
End If
If TextBox4 = "" Then
UserForm12.Show
Exit Sub
End If
If TextBox6 = "" Then
UserForm13.Show
Exit Sub
End If
validar = IsNumeric(TextBox2.Value)
If validar = False Then
UserForm22.Show
TextBox2.BackColor = &HFF00&
Exit Sub
End If
validarfecha = IsDate(TextBox6.Value)
If validarfecha = False Then
UserForm23.Show
TextBox6.BackColor = &HFF00&
Exit Sub
End If
If TextBox2 <> "" And TextBox1 <> "" And TextBox4 <> "" And TextBox6 <> "" Then
TextBox2.BackColor = -2147483643
TextBox6.BackColor = -2147483643
UserForm15.Show
End If
End Sub
Private Sub CommandButton2_Click()
UserForm2.Hide
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub UserForm_Click()
End Sub
GRACIAS!!! -
Sin el codigo (archivo de excel) se complica pero intenta, podes adjuntarlo? (eliiimandno info q creas necesaria)
Intenta cambiando:
En ComboBox1_Click()porCódigo:If ComboBox1 = Hoja5.Cells(i, 1) Then TextBox1 = Hoja5.Cells(i, 2)
YCódigo:If ComboBox1 = Hoja5.Cells(i, 2) Then TextBox1 = Hoja5.Cells(i, 1)
porCódigo:If ComboBox1 = Hoja6.Cells(j, 1) Then TextBox8 = Hoja6.Cells(j, 3)
En ComboBox1_Enter()Código:If TextBox1 = Hoja6.Cells(j, 1) Then TextBox8 = Hoja6.Cells(j, 3)
porCódigo:tareas = Hoja5.Cells(H, 1)
En el codigo no veo como guardas los valores en el stock , habria q modificar esa funcion tambien, pq sino te va a empezar a guardar el nombre con el stock y no el codigo (como debe hacerlo ahora).Código:tareas = Hoja5.Cells(H, 2)
-
Otra cosa que se me ocurre y si no es muy complicado, me gustaria poder hacerle con la ayuda de uds....
Este combobox que cambiaste walter81 y que quedo genial!!! Se puede acomodar alfabeticamente los resultados del combobox, si en la columna de excel estan desordenados??
Si sale esto hacemos un gol de media cancha!!!! Jeje...)
-
mmmm, ahi hice agua
lo q sucede, desde lo que vi, es que el combo box te carga tal cual lo tenes en el excel, y asi despues sabe que si seleccionas el 5º debe mostrarte el 5º valor de la otra columna, y si ordenas el combo solo, vas a perder la relacione entre orden en el combo y orden en el excel.
tal vez deberas ordenar antes del macro, o hacer q el macro mismo te lo ordene previamente. -
Holas, tengo otro inconveniente en esta macro que no se como resolver.
Tengo un Form en el cual puedo pedir informe de Stock ya sea, Stock Total de Productos o Stock por Referencia, o sea, seleccionando un medicamento específico arroja los movimientos del mismo. En este último tengo el problemita, ya que no me muestra todos los movimientos de entrada y de salida, sino que me muestra sólo 5 resultados y ni siquiera son los últimos 5 movimientos. Bueno me gustaría poder modificar este número de movimientos que aparece en el informe, ya sea asignando 5, 10 o 20.....ahora les paso el codigo de este form:Código:Private Sub ComboBox1_Enter() Dim i As Double Dim final As Double Dim tareas As String ComboBox1.BackColor = &H80000005 For i = 1 To ComboBox1.ListCount 'Remove an item from the ListBox. ComboBox1.RemoveItem 0 Next i For i = 2 To 1000 If Hoja5.Cells(i, 1) = "" Then final = i - 1 Exit For End If Next 'If ComboBox1.ListCount < 1 Then 'ComboBox1.AddItem "-" For i = 2 To final tareas = Hoja5.Cells(i, 2) ComboBox1.AddItem (tareas) Next 'End If End Sub Private Sub ComboBox1_Click() Dim i As Integer Dim final As Integer For i = 2 To 1000 If Hoja5.Cells(i, 1) = "" Then final = i - 1 Exit For End If Next For i = 2 To final If ComboBox1 = Hoja5.Cells(i, 2) Then TextBox1 = Hoja5.Cells(i, 1) Exit For End If Next End Sub Private Sub CommandButton1_Click() Dim NUEVO As Object Dim i As Integer Dim H As Integer Dim L As Integer Dim M As Integer Dim j As Integer Dim T As Integer Dim FINALTOTAL As Integer Dim final As Integer Dim FINAL2 As Integer Dim ORIGEN As String Dim SALDO As Double Dim VALOR As String Dim CONTAR As Double Dim CONTAR1 As Double Set NUEVO = Workbooks.Add NUEVO.Activate ORIGEN = ActiveWorkbook.Name For i = 1 To 1000 'ENTRADAS If Hoja2.Cells(i, 1) = "" Then final = i - 1 Exit For End If Next 'SALIDAS For H = 1 To 1000 If Hoja3.Cells(H, 1) = "" Then FINAL2 = H - 1 Exit For End If Next VALOR = UserForm27.TextBox1 ' ENTRADAS CONTAR = 10 ' ASIGNAR VALORES PARA EL INFORME Application.Workbooks(ORIGEN).Worksheets(1).Cells(1, 1) = "GESTIÓN DE FARMACIA HOSPITAL S.A.M.Co. SASTRE - INFORME DE MOVIMIENTOS POR REFERENCIA" Application.Workbooks(ORIGEN).Worksheets(1).Cells(2, 1) = "" Application.Workbooks(ORIGEN).Worksheets(1).Cells(3, 2) = VALOR For L = 1 To 1000 If Hoja5.Cells(L, 1) = VALOR Then Application.Workbooks(ORIGEN).Worksheets(1).Cells(4, 2) = Hoja5.Cells(L, 2) Application.Workbooks(ORIGEN).Worksheets(1).Cells(5, 2) = Hoja5.Cells(L, 3) Exit For End If Next For M = 1 To 1000 If Hoja6.Cells(M, 1) = VALOR Then Application.Workbooks(ORIGEN).Worksheets(1).Cells(5, 2) = Hoja6.Cells(M, 3) SALDO = Hoja6.Cells(M, 3) Exit For End If Next For j = 1 To final If Hoja2.Cells(j, 1) = VALOR Then CONTAR = CONTAR + 1 '***** funciona entradas Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 2) = Hoja2.Cells(j, 6) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 3) = Hoja2.Cells(j, 4) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 4) = Hoja2.Cells(j, 5) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR, 5) = 1 * Hoja2.Cells(j, 3) End If Next ' SALIDAS CONTAR1 = 10 For j = 1 To final If Hoja3.Cells(j, 1) = VALOR Then CONTAR1 = CONTAR1 + 1 Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR1, 6) = Hoja3.Cells(j, 6) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR1, 7) = Hoja3.Cells(j, 4) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR1, 8) = Hoja3.Cells(j, 5) Application.Workbooks(ORIGEN).Worksheets(1).Cells(CONTAR1, 9) = -Hoja3.Cells(j, 3) End If Next ' CALCULAR FINAL For T = 11 To 1000 If Application.Workbooks(ORIGEN).Worksheets(1).Cells(T, 5) = "" And Application.Workbooks(ORIGEN).Worksheets(1).Cells(T, 9) = "" Then FINALTOTAL = T + 1 Exit For End If Next Application.Workbooks(ORIGEN).Worksheets(1).Cells(FINALTOTAL, 8) = "TOTAL SALDO" Application.Workbooks(ORIGEN).Worksheets(1).Cells(FINALTOTAL, 9) = SALDO '*************** FORMATO NUEVO.Activate Range(Cells(FINALTOTAL, 8), Cells(FINALTOTAL, 9)).Select Selection.Font.Bold = True Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Range("H16").Select Range("A1:I1").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With With Selection.Interior .ColorIndex = 5 .Pattern = xlSolid End With Selection.Font.ColorIndex = 2 Selection.Font.Bold = True Range("A3").Select Selection.Font.Bold = True 'Windows("Libro13").Activate 'Windows("INFORME POR REFERENCIA.xls").Activate 'Windows("Libro15").Activate ActiveCell.FormulaR1C1 = "CODIGO" Range("A4").Select Selection.Font.Bold = True ActiveCell.FormulaR1C1 = "NOMBRE" Range("A5").Select Selection.Font.Bold = True ActiveCell.FormulaR1C1 = "STOCK ACTUAL" Range("B5:I5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Range("B4:I4").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Range("B3:I3").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Range("B6:I6").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Range("B8").Select 'Windows("INFORME POR REFERENCIA.xls").Activate 'Windows("Libro15").Activate ActiveCell.FormulaR1C1 = "" Range("B9").Select ActiveCell.FormulaR1C1 = "" Range("C9").Select ActiveCell.FormulaR1C1 = "PROVEEDOR" Range("D9").Select ActiveCell.FormulaR1C1 = "FECHA" Range("E9").Select ActiveCell.FormulaR1C1 = "UNIDADES" Range("F9").Select ActiveCell.FormulaR1C1 = "" Range("G9").Select ActiveCell.FormulaR1C1 = "DESPACHANTE" Range("H9").Select ActiveCell.FormulaR1C1 = "FECHA" Range("I9").Select ActiveCell.FormulaR1C1 = "UNIDADES" Range("B8:E8").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With Selection.Borders(xlInsideVertical).LineStyle = xlNone ActiveCell.FormulaR1C1 = "ENTRADA" Range("B8:E8").Select With Selection.Interior .ColorIndex = 10 .Pattern = xlSolid End With Selection.Font.ColorIndex = 2 Selection.Font.Bold = True Range("F8:I8").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection.Interior .ColorIndex = 3 .Pattern = xlSolid End With Range("F8:I8").Select ActiveCell.FormulaR1C1 = "SALIDA" Range("F8:I8").Select Selection.Font.ColorIndex = 2 Selection.Font.Bold = True Range("B9:I9").Select Selection.Font.Bold = True Range("B9:I9").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With 'With Selection.Borders(xlInsideHorizontal) '.LineStyle = xlContinuous '.Weight = xlThin '.ColorIndex = xlAutomatic 'End With Cells.Select Cells.EntireColumn.AutoFit Range("F8:I8").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Range("A2").Select '*********************************** UserForm27.ComboBox1 = "" UserForm27.TextBox1 = "" UserForm27.Hide UserForm4.Hide UserForm1.Hide Hoja2.Visible = xlSheetVeryHidden Hoja3.Visible = xlSheetVeryHidden Hoja5.Visible = xlSheetVeryHidden Hoja6.Visible = xlSheetVeryHidden Hoja1.Cells(3, 1) = "" End Sub
este código arroja una planilla así:
Agradezco cualquier info.....Saludos!!!

1