Home » excel » excel – how to scroll the window when InputBox dialog is displayed in VBA?

excel – how to scroll the window when InputBox dialog is displayed in VBA?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want the user to input the last filled cell number through an InputBox dialog.

numRows = InputBox("Enter number of rows in the BOM: ", "Number of Rows")

The problem is when the dialog is on the screen the user cannot scroll down to see the last row. It will be hard to find the number of filled rows dynamically since some cells are filled some are not.

Thank you.

EDIT:
i am using Excel 2007

How to&Answers:

I suggest you should find out the last filled row programmatically without asking the user. Here

http://www.mrexcel.com/td0058.html

are some different solutions for this problem (dealing also with the case where some cells are filled and some are not).

Answer:

While this has been closed out I will add these comments as an answer rather than as a comment given the length of this response and the need for formatting. The link does correctly note that the SpecialCells approach that you have selected can be problematic, I have added a fix below. Plus I have added code that will look at the actual number of rows in xl2007, the link (written back in 2002) currently fixes at 65536 rows as this was the only row number in existence for Excel at this time

  • SpecialCells

LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

can be applied like this to ensure the usedrange is reset properly to the last cell

 ActiveSheet.UsedRange
 LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
  • Using row count from bottom

    LastRowColA = Range("A65536").End(xlUp).Row
    

to cater for rows in all excel versions (xl07 and xl10 have 1.048 Million Rows)

    LastRowColA = Cells(Rows.Count,"A").End(xlUp).Row
  • Using Find

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

this will error out if the sheet is blank (ie has no values to dfind)

    Dim rng1 As Range
    Set rng1 = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    If Not rng1 Is Nothing Then
        LastRow = rng1.Row
    Else
        'sheet is blank
    End I