Вы находитесь на странице: 1из 1

Public Sub export2excel()

On Error GoTo er
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")
Dim oWorkBook As Object
Dim oWorkSheet As Object
Dim i As Integer, k As Integer
Dim lRow As Long
Dim LastRow As Long
Dim LastCol As Long
oExcel.Visible = False
oExcel.Workbooks.Open App.Path & "\Nirmal.xls"
Set oWorkSheet = oExcel.Workbooks("Nirmal.xls").sheets("Batch")
i = 2 'Row in Excel
LastRow = frmFind.DataGrid1.Row 'Save Current row
LastCol = frmFind.DataGrid1.Col 'and column
frmFind.DataGrid1.Row = 0 'Fixed Row is -1
Do While frmFind.DataGrid1.Row <= frmFind.DataGrid1.VisibleRows - 1
For k = 1 To frmFind.DataGrid1.Columns.Count - 1
frmFind.DataGrid1.Col = k 'Fixed Column is -1
oWorkSheet.Cells(i, k).Font.Bold = False
oWorkSheet.Cells(i, k).Font.Color = vbBlack
oWorkSheet.Cells(i, k).value = frmFind.DataGrid1.Text
Next
i = i + 1
If frmFind.DataGrid1.Row < frmFind.DataGrid1.VisibleRows - 1 Then
frmFind.DataGrid1.Row = frmFind.DataGrid1.Row + 1
Else
Exit Do
End If
Loop
frmFind.DataGrid1.Row = LastRow 'Restore original Row
frmFind.DataGrid1.Col = LastCol 'and Column
oExcel.Workbooks("Nirmal.xls").Save
oExcel.Workbooks("Nirmal.xls").Close savechanges:=True
oExcel.Quit
er:
If Err.Number = 1004 Then
Exit Sub
End If
End Sub

Вам также может понравиться