Results 1 to 7 of 7

Thread: (RESOLVED) Exporting from MSFlexgrid to Excel. This is taking ages..

Threaded View

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved (RESOLVED) Exporting from MSFlexgrid to Excel. This is taking ages..

    I have a flexgrid with rows merged. The content is in this format:

    Row1 - TITLE ROW - (merged) contains a Title: "Item1 - Item2"
    Row2: Row for Item1
    Row3: Row Item2

    Row 4 - TITLE ROW - (merged) contains a Title: "Item3 - Item4"
    Row5: Row for Item3
    Row6: Row Item4

    ..and so on, Its: Title, 1 item, another Item.. and repeating.

    When I export to Excel I need to preserve that format, using diferente colors for Titles, Formating numbers, change some gridlines, else, it would be illegible.

    Well, the problem is its take to long to fill the worksheet, i.e. if my flexgrid has 200 - 300 rows, it takes between 1 and 2 minutes to fill the worksheet
    I've done this based on the Macro code.
    Is there a faster way to do it?

    VB Code:
    1. Private Sub subExportToExcel(pGrilla As Integer)
    2. Dim i        As Long
    3. Dim p        As Long
    4. Dim newCell  As String
    5. Dim xl       As Object
    6. Dim lcont    As Integer
    7. Dim v        As Integer
    8.  
    9. On Error GoTo Error_Here
    10.    
    11.     Screen.MousePointer = vbHourglass
    12.     lcont = 0
    13.     With Flex.Item(pGrilla) 'this Flexgrid from the Array of Flexgrids
    14.    
    15.         If .Rows <= 2 Then
    16.             Screen.MousePointer = vbDefault
    17.             Exit Sub
    18.         End If
    19.        
    20.         Set xl = CreateObject("excel.application")
    21.             xl.Workbooks.Add
    22.             DoEvents
    23.                
    24.         'Title row settings: bold, color, alignment
    25.         xl.Worksheets(1).Range("A1:AB1").Select
    26.         With xl.Selection
    27.                 .HorizontalAlignment = xlCenter
    28.                 .VerticalAlignment = xlBottom
    29.                 .Interior.ColorIndex = 55
    30.                 .Interior.Pattern = xlSolid
    31.                 .Font.ColorIndex = 2
    32.                 .Font.Bold = True
    33.            
    34.             'Gridlines format
    35.             For v = 7 To 10
    36.                 With .Borders(v)
    37.                     If v <= 10 Then
    38.                         .LineStyle = xlContinuous
    39.                         .Weight = xlThin
    40.                     End If
    41.                 End With
    42.             Next
    43.         End With
    44.        
    45.         'Add column numbers one by one
    46.         For p = 0 To 27
    47.             If p < 26 Then newCell = Chr(p + 65) & "1" Else newCell = "A" & Chr(p - 26 + 65) & "1"
    48.             xl.Worksheets(1).Range(newCell).Value = .TextMatrix(0, p)
    49.             xl.Worksheets(1).Range(newCell).Select
    50.             If p = 0 Then
    51.                 xl.Selection.ColumnWidth = 20
    52.             Else
    53.                 xl.Selection.ColumnWidth = Len(.TextMatrix(0, p)) + 3
    54.             End If
    55.         Next
    56.        
    57.         For i = 2 To .Rows
    58.             'Format cells (if its not a Title row)
    59.             If lcont <> 0 Then
    60.                 'Number format from column E
    61.                 xl.Worksheets(1).Range("E" & CStr(i) & ":AB" & CStr(i)).Select
    62.                 With xl.Selection
    63.                     .NumberFormat = "0.000"
    64.                 End With
    65.                
    66.                 'Center align, all columns except the first
    67.                 xl.Worksheets(1).Range("B" & CStr(i) & ":AB" & CStr(i)).Select
    68.                 With xl.Selection
    69.                     .HorizontalAlignment = xlCenter
    70.                     .VerticalAlignment = xlBottom
    71.                 End With
    72.             End If
    73.                    
    74.             For p = 0 To .Cols - 1
    75.                     .Row = i - 1
    76.                     .Col = p
    77.                     If p < 26 Then newCell = Chr(p + 65) & i Else newCell = "A" & Chr(p - 26 + 65) & i
    78.                     If lcont = 0 Then
    79.                         xl.Worksheets(1).Range(newCell).Value = .Text
    80.                     Else
    81.                         xl.Worksheets(1).Range(newCell).Value = Replace(.Text, ",", ".")
    82.                     End If
    83.                                        
    84.                     If lcont = 0 Then
    85.                         xl.Worksheets(1).Range(newCell).Font.Bold = True
    86.                         xl.Worksheets(1).Range(newCell).Font.Color = vbBlue
    87.                         lcont = 3
    88.                         Exit For
    89.                     End If
    90.             Next
    91.            
    92.             If lcont = 3 Then
    93.                 xl.Worksheets(1).Range("A" & CStr(i) & ":AB" & CStr(i)).Select
    94.                 With xl.Selection.Interior
    95.                     .ColorIndex = 37
    96.                     .Pattern = xlSolid
    97.                 End With
    98.                 For v = 7 To 10
    99.                     With xl.Selection.Borders(v)
    100.                         .LineStyle = xlContinuous
    101.                         .Weight = xlThin
    102.                         .ColorIndex = xlAutomatic
    103.                     End With
    104.                 Next
    105.             End If
    106.            
    107.             lcont = lcont - 1
    108.         Next
    109.     End With
    110.    
    111.     'Finishes leaving 1st cell selected
    112.     xl.Worksheets(1).Range("A1").Select
    113.     Screen.MousePointer = vbDefault
    114.     xl.Visible = True 'Show workbook
    115.  
    116. Exit Sub
    117. Error_Here:
    118.     If xl Is Nothing Then
    119.         Call MsgBox("Error initializing Microsoft Excel", vbExclamation + vbOKOnly, "Error")
    120.     End If
    121.     Screen.MousePointer = vbDefault
    122. End Sub
    Last edited by jcis; Nov 29th, 2005 at 08:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width