Problema con vb y excel!!!
-
Este es el codigo, funciona la primera vez perfecto pero la segunda me tira error y no se como solucionarlo, por favor ayuda!!!
Código:Private Sub cmdMostrar_Click() Dim planilla As New excel.Application Dim rs As New ADODB.Recordset Dim rs2 As New ADODB.Recordset Dim fs As ADODB.field Dim i As Integer Dim total() As Double Dim total2 As Double Dim porcentaje() As Double Dim acumporc As Double Dim conporc As Boolean planilla.Workbooks.Add planilla.Visible = True planilla.Cells(1, 1) = "Listado de ventas desde " & txtdesde.value & " hasta " & txthasta.value & "" planilla.Cells(1, 1).Select With planilla.Selection.Font .Size = 14 .Bold = True End With planilla.Range("A1:G1").Select planilla.Selection.Merge rs.Open "SELECT COUNT(*) FROM Clientes", conn ReDim total(rs(0)) ReDim porcentaje(rs(0)) rs.Close rs2.Open "SELECT * FROM Clientes", conn i = 0 While Not rs2.EOF = True rs.Open "SELECT * FROM Factura_Cliente WHERE Cliente=" & rs2!codigo & " AND Fecha BETWEEN #" & txtdesde.value & "# AND #" & txthasta.value & "#", conn While Not rs.EOF = True total(i) = total(i) + Round(rs!total, 2) total2 = Round(total2, 2) + Round(rs!total, 2) rs.MoveNext Wend rs.Close rs2.MoveNext i = i + 1 Wend If (rs2.EOF And rs2.BOF) Then MsgBox "No hay datos", vbInformation + vbOKOnly, mdiPrincipal.caption Else For i = 0 To UBound(porcentaje()) If (total2 <> 0) Then porcentaje(i) = ((total(i) * 100) / total2) porcentaje(i) = Round(porcentaje(i), 2) acumporc = acumporc + porcentaje(i) End If Next renglon = 3 planilla.Cells(renglon, 1) = "Cliente" planilla.Cells(renglon, 2) = "$ Ventas" planilla.Cells(renglon, 3) = "Porcentaje" renglon = renglon + 1 rs2.MoveFirst i = 0 Do While Not rs2.EOF planilla.Cells(renglon, 1) = rs2.Fields("Nombre").value planilla.Cells(renglon, 2) = total(i) planilla.Cells(renglon, 3) = porcentaje(i) renglon = renglon + 1 i = i + 1 rs2.MoveNext Loop rs2.Close planilla.Cells(renglon, 1) = "TOTAL" End If planilla.Range("A:O").EntireColumn.AutoFit planilla.Range("A" & (UBound(total()) + 4) & ":C" & (UBound(total()) + 4) & "").Select Selection.Font.Bold = True planilla.Range("A4:C" & (UBound(total()) + 3) & "").Select planilla.Range("C" & (UBound(total()) + 3) & "").Activate planilla.Range("B" & (UBound(total()) + 4) & "").Select planilla.ActiveCell.FormulaR1C1 = "=SUM(R[-" & UBound(total()) & "]C:R[-1]C)" planilla.Charts.Add planilla.ActiveChart.ChartType = xl3DPie planilla.ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A4:C" & (UBound(total()) + 3) & ""), PlotBy:=xlColumns planilla.ActiveChart.Location Where:=xlLocationAsObject, Name:="Hoja1" planilla.ActiveSheet.Shapes("Gráfico 1").IncrementLeft -30.75 planilla.ActiveSheet.Shapes("Gráfico 1").IncrementTop 13.5 planilla.ActiveChart.ApplyDataLabels AutoText:=True, LegendKey:=False, _ HasLeaderLines:=True, ShowSeriesName:=False, ShowCategoryName:=False, _ ShowValue:=False, ShowPercentage:=True, ShowBubbleSize:=False Set planilla = Nothing planilla.Application.Workbooks.Close End Sub -
Fijate haciendo asi:
Private Sub cmdMostrar_Click()
Dim planilla As Object
..
..
Set planilla = CreateObject("Excel.Application")
..
..
etc.
Me suena q te falta ese primer "set". Por eso funca una vez y depues no, cuando le haces set = nothing, llamás al destructor de la instancia y fue.
Conta si te sirvió. Chau!
