Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

Add good amout of rows in Grid

Hello all,

I need your help to adjust a code that i have.

With the code bellow, i export an excel spreadsheet into a grid. The only problem that i have is that i have to put tell the grid manually how many row i need to import all lines from the excel spreadsheet.

I would like the code to automatically adjust himself with the good amount of rows.

How can i do this please?

Thanks again for your help.
Dim xlObject    As Excel.Application
Dim xlWB        As Excel.Workbook
On Error GoTo MyErrHandler

    With CommonDialog1
        .CancelError = True
        .Filter = "Microsoft Excel files (xlam, xlsx, xltm, xlt, xlsm, xltx, xls, txt, csv)"
        '.InitDir = App.Path
        .InitDir = "C:\Documents and Settings\all users\Desktop"
        .ShowOpen
        If Not .FileName = "" Then
            Set xlObject = New Excel.Application
            Set xlWB = xlObject.Workbooks.Open(.FileName)
          
   Clipboard.Clear
  
   With xlObject.ActiveWorkbook.ActiveSheet
       .Range("A1:Bj10000").Copy 'Set selection to Copy
   End With
   With fg
       .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 xlWB = Nothing
    Set xlObject = Nothing
    
     End If
    End With
    
    Exit Sub
    


MyErrHandler:
    Err.Clear
    
    End If

Open in new window

Avatar of Karen
Karen
Flag of Australia image


See below for a good way to find the "last" cell.
You would then call this function:

Dim xlRange  As Excel.Range

Set xlRange = LastCell(xlObject.ActiveWorkbook.ActiveSheet)
xlRange.Copy 'Set selection to Copy


=======

Function LastCell(ws As Excel.Worksheet) As Excel.Range
  Dim LastRow&, LastCol%

' Error-handling is here in case there is not any
' data in the worksheet

  On Error Resume Next

  With ws

  ' Find the last real row

    LastRow& = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

  ' Find the last real column

    LastCol% = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column

  End With

' Finally, initialize a Range object variable for
' the last populated row.

  Set LastCell = ws.Cells(LastRow&, LastCol%)

End Function

Avatar of Wilder1626

ASKER

What do i need to do with the code?

Do i need to combine it to the code that i have?
Well...I was assuming that since you posted with VB6 in the tag, that you were writing in vb6? Where do you have your code? Within the code you gave, you would put the function call like this:

Dim xlObject    As Excel.Application
Dim xlWB        As Excel.Workbook
Dim xlRange  As Excel.Range

On Error GoTo MyErrHandler

    With CommonDialog1
        .CancelError = True
        .Filter = "Microsoft Excel files (xlam, xlsx, xltm, xlt, xlsm, xltx, xls, txt, csv)"
        '.InitDir = App.Path
        .InitDir = "C:\Documents and Settings\all users\Desktop"
        .ShowOpen
        If Not .FileName = "" Then
            Set xlObject = New Excel.Application
            Set xlWB = xlObject.Workbooks.Open(.FileName)
         
   Clipboard.Clear
 
   ' New code here
   Set xlRange = LastCell(xlObject.ActiveWorkbook.ActiveSheet)
   xlRange.Copy 'Set selection to Copy


   With fg
       .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 xlWB = Nothing
    Set xlObject = Nothing
   
     End If
    End With
   
    Exit Sub
   


MyErrHandler:
    Err.Clear
   
    End If
Good, i don't have any error.

But, Once i select the excel spreadsheet, it does not transfer to my Msflexgrig (fg).

I'm sorry, I have made that far more complicated than it needs to be.... you do not need the new function I gave you.

Dim xlObject    As Excel.Application
Dim xlWB        As Excel.Workbook
On Error GoTo MyErrHandler

    With CommonDialog1
        .CancelError = True
        .Filter = "Microsoft Excel files (xlam, xlsx, xltm, xlt, xlsm, xltx, xls, txt, csv)"
        '.InitDir = App.Path
        .InitDir = "C:\Documents and Settings\all users\Desktop"
        .ShowOpen
        If Not .FileName = "" Then
            Set xlObject = New Excel.Application
            Set xlWB = xlObject.Workbooks.Open(.FileName)
         
   Clipboard.Clear

 
' This is all you need to do
xlObject.Cells.Copy        'Set selection to Copy


   With fg
       .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 xlWB = Nothing
    Set xlObject = Nothing
   
     End If
    End With
   
    Exit Sub
   


MyErrHandler:
    Err.Clear
   
    End If
ASKER CERTIFIED SOLUTION
Avatar of Karen
Karen
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh yes.

Perfect and thanks a lot.