Results 1 to 4 of 4

Thread: [RESOLVED] How to load whole data from excel to msflexgrid

Threaded View

  1. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to load whole data from excel to msflexgrid

    Good point about the Range, i don't know why i didn't use UsedRange in the first place. So, about your Questions (1) and (2), the solution is using UsedRange, try this new version..
    Code:
    Private Sub ExcelToFlexgrid()
    Dim xlObject    As Excel.Application
    Dim xlWB        As Excel.Workbook
    Dim xSheet      As Excel.Worksheet
    
        Set xlObject = New Excel.Application
        Set xlWB = xlObject.Workbooks.Open("C:\Book1.xls") 'Open your book here
    
        Clipboard.Clear
        Set xSheet = xlObject.ActiveWorkbook.ActiveSheet
        MSFlexGrid1.Rows = xSheet.UsedRange.Rows.Count
        MSFlexGrid1.Cols = xSheet.UsedRange.Columns.Count
        xSheet.UsedRange.Copy
        
        With MSFlexGrid1
            .Redraw = False 'Dont draw until the end, so we avoid that flash
            .Row = 0 'Paste from first cell
            .Col = 0
            .RowSel = .Rows - 1 'Select maximum allowed (your selection shouldnt be greater than this)
            .ColSel = .Cols - 1
            .Clip = Replace(Clipboard.GetText, vbNewLine, vbCr) 'Replace carriage return with the correct one
            .Col = 1 'Just to remove that blue selection from Flexgrid
            .Redraw = True 'Now draw
        End With
        
        xlObject.DisplayAlerts = False 'To avoid "Save woorkbook" messagebox
        
        'Close Excel
        xlWB.Close
        xlObject.Application.Quit
        Set xSheet = Nothing
        Set xlWB = Nothing
        Set xlObject = Nothing
    End Sub
    About your question (3) you just need to add the component "Microsoft Common Dialog" to your project and some code, there many examples about Common Dialog here in the forums you'll find them with a Search. When you finally have the path where the xls file is, you just need to replace part in red in this line with the path selected in Common Dialog (CommonDialog1.Filename)
    Code:
    Set xlWB = xlObject.Workbooks.Open("C:\Book1.xls")
    Last edited by jcis; Jan 10th, 2010 at 10:31 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