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..
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: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
Code:Set xlWB = xlObject.Workbooks.Open("C:\Book1.xls")




Reply With Quote