ramses28
Member
Hi, I'm trying to export data from a DataGrid to Excel using VBA, but I'm getting an error "Object doesn't support this property or method". The code is the following:
Private Sub Button1_Released()
Dim ExcelApp As Object
Dim ExcelWorkbook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
' Crear una instancia de Excel
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True ' Hacer visible Excel
' Crear un nuevo libro en Excel
Set ExcelWorkbook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelWorkbook.Sheets(1)
' Copiar datos del DataGrid a Excel
For i = 0 To DataGrid1.RowCount - 1
For j = 0 To DataGrid1.ColumnCount - 1
ExcelSheet.Cells(i + 1, j + 1).Value = DataGrid1.Rows(i).Cells(j).Value
Next j
Next i
Dim sFileName As String
sFileName = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Guardar como")
If sFileName <> "False" Then
ExcelWorkbook.SaveAs sFileName
ExcelWorkbook.Close
ExcelApp.Quit
MsgBox "Exportación completada."
Else
ExcelWorkbook.Close False
ExcelApp.Quit
MsgBox "Exportación cancelada."
End If
' Limpiar variables
Set ExcelSheet = Nothing
Set ExcelWorkbook = Nothing
Set ExcelApp = Nothing
End Sub
According to what I have searched on Google, it is because the object does not have the property. So I wanted to know the correct way to export this data.
Thank you for your attention and help.
Private Sub Button1_Released()
Dim ExcelApp As Object
Dim ExcelWorkbook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
' Crear una instancia de Excel
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True ' Hacer visible Excel
' Crear un nuevo libro en Excel
Set ExcelWorkbook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelWorkbook.Sheets(1)
' Copiar datos del DataGrid a Excel
For i = 0 To DataGrid1.RowCount - 1
For j = 0 To DataGrid1.ColumnCount - 1
ExcelSheet.Cells(i + 1, j + 1).Value = DataGrid1.Rows(i).Cells(j).Value
Next j
Next i
Dim sFileName As String
sFileName = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Guardar como")
If sFileName <> "False" Then
ExcelWorkbook.SaveAs sFileName
ExcelWorkbook.Close
ExcelApp.Quit
MsgBox "Exportación completada."
Else
ExcelWorkbook.Close False
ExcelApp.Quit
MsgBox "Exportación cancelada."
End If
' Limpiar variables
Set ExcelSheet = Nothing
Set ExcelWorkbook = Nothing
Set ExcelApp = Nothing
End Sub
According to what I have searched on Google, it is because the object does not have the property. So I wanted to know the correct way to export this data.
Thank you for your attention and help.