Ver Mensaje Individual
  #7 (permalink)  
Antiguo 07/01/2010, 15:56
Avatar de Carlojas
Carlojas
 
Fecha de Ingreso: junio-2007
Ubicación: Shikasta
Mensajes: 1.272
Antigüedad: 17 años, 8 meses
Puntos: 49
Respuesta: Ejecutar Macro Al Cambiar Valor De Una Celda

Que tal compañeros buenas tardes para todos, al fin pude solucionar el problema que tenía de la siguiente forma, no se si será las mas adecuada pero por ahora es la que me ha funcionado:

El Macro quedo así:

Código vb:
Ver original
  1. Sub Consulta_Condicional(ByVal Dato As String)
  2. Dim cnMax As ADODB.Connection
  3. Set cnMax = New ADODB.Connection
  4. Dim RMServer As String
  5. RMServer = "SERVIDOR\INSTANCIA"
  6. Dim sUID As String
  7. sUID = "xxxx"
  8. Dim sPassword As String
  9. sPassword = "xxxx"
  10.  
  11. Dim strConn As String
  12. strConn = "Provider=sqloledb;Data Source=" & RMServer & ";database=BD;UID=" & sUID & ";Pwd=" & sPassword & ";"
  13. cnMax.Open strConn
  14.  
  15. Dim rsMax As ADODB.Recordset
  16. Set rsMax = New ADODB.Recordset
  17. Dim cOrden As String
  18. cOrden = Trim(Mid(Dato, 1, 8))
  19. Dim Posicion As String
  20. Posicion = Trim(Mid(Dato, 10, 10))
  21.  
  22. If cOrden <> vbNullString Then
  23.     With rsMax
  24.       .ActiveConnection = cnMax
  25.       .Open "SELECT CAST(CURQTY_10 AS INT) AS CURQTY_10, RTRIM(PRTNUM_10) AS PRTNUM_10, RTRIM(PLANID_01) AS PLANID_01, RTRIM(PMDES1_01) AS PMDES1_01 " & _
  26.       " FROM Order_Master INNER JOIN Part_Master ON PRTNUM_10 = PRTNUM_01 WHERE ORDNUM_10 = '" & cOrden & "'"
  27.      
  28.       If Not rsMax.EOF Then
  29.          Range(Posicion).Offset(0, -2) = rsMax.Fields(0).Value
  30.          Range(Posicion).Offset(0, -1) = rsMax.Fields(1).Value
  31.       Else
  32.          MsgBox "No Existen Criterios Relacionados Con El Número De Orden Que ha Digitado", vbInformation, "Consulta De Ordenes"
  33.          Exit Sub
  34.       End If
  35.       .Close
  36.     End With
  37.     cnMax.Close
  38.     Set rsMax = Nothing
  39. Else
  40.     MsgBox "La Condición De Busqueda No Puede Estar Vacia", vbInformation, "Consulta De Ordenes"
  41. End If
  42. End Sub

Y lo ejecuto en el evento Change de la hoja:
Código vb:
Ver original
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Celdas = "D2:D70,G2:G70"
  3. If Not Application.Intersect(Target, Range(Celdas)) Is Nothing Then
  4. If Target.Value <> vbNullString Then
  5.     Consulta_Condicional (Target & "-" & Target.Address)
  6. End If
  7. End If
  8. End Sub

Muchas Gracias a todos.


Saludos.
__________________
"SELECT * FROM Mujeres WHERE situacion NOT IN ('CASADAS','CON HIJOS','ATORMENTADAS','CUASI-ENNOVIADAS') AND personalidad <> 'INTENSA'"