Vous êtes sur la page 1sur 2

Sub ExtrairDados() ' Create a connection object. Dim cnPubs As ADODB.Connection Set cnPubs = New ADODB.

Connection ' Provide the connection string. Dim strConn As String 'Use the SQL Server OLE DB Provider. strConn = "PROVIDER=SQLOLEDB;" 'Connect to the Pubs database on the local server. strConn = strConn & "DATA SOURCE=Melchior;INITIAL CATALOG=P10_Prod;" 'Use an integrated login. 'strConn = strConn & " INTEGRATED SECURITY=sspi;" strConn = strConn & " UId=sa;Password=M1CR0$1GA;" 'Now open the connection. cnPubs.Open strConn Dim Query As String Query = "" Query = Query & " SELECT" Query = Query & " B.B8_PRODUTO, B.B8_LOCAL, B.B8_DTVALID, B.B8_SALDO, B.B8_L OTECTL," Query = Query & " A.BF_LOCAL, A.BF_LOCALIZ, A.BF_QUANT, A.BF_LOTECTL" Query = Query & " FROM" Query = Query & " [P10_Prod].[dbo].[SB8010] AS B," Query = Query & " [P10_Prod].[dbo].[SBF010] AS A" Query = Query & " where (B.B8_SALDO > 0 AND B.[D_E_L_E_T_] = '') AND" Query = Query & " (A.[D_E_L_E_T_] = '') AND" Query = Query & " (B.B8_PRODUTO = BF_PRODUTO AND B.B8_LOCAL = A.BF_LOC AL AND A.BF_LOTECTL= B.B8_LOTECTL )" Query = Query & " order by B.B8_LOCAL, B.B8_PRODUTO, A.BF_LOCALIZ, A.BF_LOTE CTL" ' Create a recordset object. Dim rsPubs As ADODB.Recordset Set rsPubs = New ADODB.Recordset Sheets("Validade").Select Range("A6", "J99999").Delete Range("J3").Value = Now With rsPubs ' Assign the Connection object. .ActiveConnection = cnPubs ' Extract the required records. .Open Query ' Copy the records into cell A1 on Sheet1. Range("B6").CopyFromRecordset rsPubs ' Tidy up .Close End With

cnPubs.Close Set rsPubs = Nothing Set cnPubs = Nothing FormataDados End Sub Sub FormataDados() Range("B6").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End Sub

Vous aimerez peut-être aussi