VB Export Data to Excel

Source: Internet
Author: User

Option Explicit

Private Sub Form_Load ()
Dim I as Long, J as Long

Me.MSHFlexGrid1.Rows = 2000
Me.MSHFlexGrid1.Cols = 10
For i = 0 to Me.mshflexgrid1.rows-1
For j = 0 to Me.mshflexgrid1.cols-1
Me.MSHFlexGrid1.TextMatrix (i, j) = i & "Rows" & J & "Columns"
Next
Next
Debug.Print Me.MSHFlexGrid1.TextArray (100)
End Sub

Private Sub Cmdexport_click ()
Dim I as Long, J as Long
Dim Cellsdata () as String

Dim objapp as Excel.Application
Dim Objworkbook as Excel.Workbook
Dim Objworksheet as Excel.Worksheet
Dim Objrange as Excel.Range

' Construct a two-dimensional array
ReDim Cellsdata (1 to Me.MSHFlexGrid1.Rows, 1 to Me.MSHFlexGrid1.Cols)
For i = 1 to Me.MSHFlexGrid1.Rows
For j = 1 to Me.MSHFlexGrid1.Cols
Cellsdata (i, j) = Me.MSHFlexGrid1.TextMatrix (i-1, j-1)
Next
Next

' Export to Excel
Set objapp = New Excel.Application
objapp.screenupdating = False ' Disable screen refresh
Set Objworkbook = ObjApp.Workbooks.Add
Set Objworksheet = ObjWorkbook.Sheets.Add
Set objrange = Objworksheet.range (Objworksheet.cells (1, 1), Objworksheet.cells (Me.MSHFlexGrid1.Rows, Me.MSHFlexGrid1.Cols))
Objrange.value = Cellsdata
objApp.Visible = True
objapp.screenupdating = True

' Destroy two-dimensional arrays
Erase Cellsdata

Me.setfocus
MsgBox "Export Complete"
End Sub

VB Export Data to Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.