Macros no me Carga Datos a HOJA

Estoy DESESPERADO con Este TEMA, Caso de VIDa o MUERTE pero más de MUERTE : Tengo un Excel con 3 HOJAS ( Hoja1=Lima, Hoja=Provincia y HOJA9= Total ) con una MACROS con el Botón Procesar con un rango de Fechas. EL resultado me tiene que botar los datos de una DB Sql "PrdSAP" a mi Libro de EXCEL. Me Bota los Datos para la "HOja LIMA" pero NO me muestra los DATOS a la "HOJA PROVINCIA" para que en la "HOJA TOTAL" me de la SUMA de las 2 HOJAS... Por favor HELP!... :(

Private Sub CmdProcesar_Click()

Dim CMDVENTA As ADODB.Command
Dim CMDCOMBOS As ADODB.Command
Dim RSVENTA As New ADODB.Recordset
Dim RSCOMBOS As New ADODB.Recordset

fecini = TxtFecIni.Value
fecfin = TxtFecfin.Value

SUBCC = CboSubCanalCtrl.Value

Set cn = New ADODB.Connection
With cn
.CursorLocation = adUseClient
.ConnectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=AJINOMOTONT2;INITIAL CATALOG=prdsap;USER ID=PLANINF; PASSWORD=INFORMACION;"
.Open

If CboSubCanalCtrl.Value <> "" Then

SQL1 = "select dmtdi_fecha.anno, dmtdi_fecha.mes, dmtdi_materiales.cod_jerarquia_mat AS ID_PRESENTACION, dmtfa_ventas.cod_canal, "
SQL1 = SQL1 + "dmtdi_canal.dsc_canal, dmtfa_ventas.cod_org_ventas,(left(dmtdi_zona_ventas.dsc_zona_ventas,4)) as [Desc_Zona],dmtdi_zona_ventas.dsc_zona_ventas , "
SQL1 = SQL1 + "dmtfa_ventas.cod_solicitante,dmtfa_ventas.cod_material,dmtfa_ventas.cod_jerarquia_material, "
SQL1 = SQL1 + "(left(dmtfa_ventas.cod_jerarquia_material,3)+ ' ' + left(dmtdi_materiales.dsc_material,3)) as [Cod_Clase], "
SQL1 = SQL1 + "dmtfa_ventas.cod_grupo_vendedores,dmtdi_grupo_vendedores.dsc_grupo_vendedores,dmtdi_oficina_ventas.cod_oficina_ventas, "
SQL1 = SQL1 + "dmtdi_oficina_ventas.dsc_oficina_ventas,SUM(dmtfa_ventas.ctd_neto_ventas) AS peso "
SQL1 = SQL1 + "FROM dmtfa_ventas inner join dmtdi_materiales on dmtfa_ventas.cod_MATERIAL=dmtdi_materiales.cod_MATERIAL inner join dmtdi_canal "
SQL1 = SQL1 + "on dmtfa_ventas.cod_canal=dmtdi_canal.cod_canal inner join dmtdi_zona_ventas on dmtfa_ventas.cod_zona_ventas=dmtdi_zona_ventas.cod_zona_ventas inner join dmtdi_grupo_vendedores "
SQL1 = SQL1 + "on dmtfa_ventas.cod_grupo_vendedores=dmtdi_grupo_vendedores.cod_grupo_vendedores inner join dmtdi_clase_posicion ON dmtfa_ventas.cod_tipo_posicion = dmtdi_clase_posicion.cod_clase_posicion inner join dmtdi_oficina_ventas "
SQL1 = SQL1 + "on dmtfa_ventas.cod_oficina_ventas=dmtdi_oficina_ventas.cod_oficina_ventas inner join dmtdi_fecha on dmtfa_ventas.fch_documento=dmtdi_fecha.id_fecha "
SQL1 = SQL1 + "WHERE dmtfa_ventas.fch_documento between convert(datetime,' " & fecini & " ',103) and convert (datetime,' " & fecfin & "',103) and dbo.dmtdi_clase_posicion.dsc_tipo_posicion IN ('VENTA')and "
SQL1 = SQL1 + "dmtfa_ventas.cod_canal<>'50'and dmtfa_ventas.cod_org_ventas='1100' and (left(dmtdi_zona_ventas.dsc_zona_ventas,4)) in ('prov','lima') "
SQL1 = SQL1 + "GROUP BY dmtdi_fecha.anno,dmtdi_fecha.mes,dmtdi_materiales.cod_jerarquia_mat,dmtfa_ventas.cod_canal, dmtdi_canal.dsc_canal,dmtfa_ventas.cod_org_ventas, dmtfa_ventas.cod_zona_ventas, "
SQL1 = SQL1 + "dmtdi_zona_ventas.dsc_zona_ventas, dmtfa_ventas.cod_solicitante, dmtfa_ventas.cod_material,dmtfa_ventas.cod_jerarquia_material,dmtdi_materiales.dsc_material,dmtfa_ventas.cod_grupo_vendedores, "
SQL1 = SQL1 + "dmtdi_grupo_vendedores.dsc_grupo_vendedores, dmtdi_oficina_ventas.cod_oficina_ventas, dmtdi_oficina_ventas.dsc_oficina_ventas "
SQL1 = SQL1 + "ORDER BY dmtdi_fecha.anno,dmtdi_fecha.mes,dmtdi_materiales.cod_jerarquia_mat"

Set CMDVENTA = New ADODB.Command

With CMDVENTA
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = SQL1
.CommandTimeout = 180

Set RSVENTA = CMDVENTA.Execute

X = RSVENTA.RecordCount
End With

Limpiar_mes

ULTIMO = 170
intNumeroHojas = ThisWorkbook.Sheets.Count

'LLENAR VENTAS
If X <> 0 Then
For CODV = 1 To RSVENTA.RecordCount
anno = RSVENTA.Fields("ANNO").Value
mes = RSVENTA.Fields("MES").Value
IDZONAC = RSVENTA.Fields("Desc_Zona").Value
'ZONAC = RSVENTA.Fields("ZONA_CONTROL").Value
CODIGO = RSVENTA.Fields("ID_PRESENTACION").Value

For NCOL = 4 To 90
If IsDate(Hoja9.Cells(12, NCOL)) = False Then EXFECHA = 0 Else EXFECHA = Hoja9.Cells(12, NCOL)
EXANNO = Year(EXFECHA)
EXMES = Month(EXFECHA)
If EXANNO = anno And EXMES = mes Then
COL = NCOL
Exit For
End If
Next

For i = 1 To intNumeroHojas
If IDZONAC = ThisWorkbook.Sheets(i).Range("B4") Then
For CODEX = 13 To ULTIMO
CODIGOEXCEL = ThisWorkbook.Sheets(i).Cells(CODEX, 1)
If CODIGOEXCEL = CODIGO Then
NFILA = CODEX
ThisWorkbook.Sheets(i).Cells(NFILA, COL) = ThisWorkbook.Sheets(i).Cells(NFILA, COL) + RSVENTA.Fields("peso").Value
Exit For
End If
Next
Exit For
End If
Next

RSVENTA.MoveNext

Next
End If

MsgBox "FIN DEL PROCESO", vbInformation, "PROCESO"

End If
End With

End Sub

Por favor Capo espero me ayude a Solucionar este ERROR de un MES, soy de Lima Perú. Mi Correo es = [email protected]...... Regalo PANETONNNNNNNNN jajaja. La verdad estoy desesperado. Mi Celu 986050428 LIMA, PERÚ :)

1 Respuesta

Respuesta
-1

No he trabajado con tu base de datos pero por lo general cuando tengo este tipo de problema, lo que hago es hacer de menos a más, prueba primero la conexión, has algo sencillo primero y después te vas a lo complejo, ojalá te ayuden mis palabras.

Suerte con eso!

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas