Solved

Excel VBA use input box to change a row number

Posted on 2014-04-06
6
585 Views
Last Modified: 2014-04-06
Hi

I have Excel  VBA code that finds the last row in the sheet. If this is not the row that
the user wants to loop to, I want the user to be able to enter  row number
What VBA code would I use to show an input box that facilitates this?

Thanks
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 167 total points
ID: 39981641
Something like this:
Dim ULoop as Integer

ULoop = Inputbox("Until which row would you like to loop?")

For i 1 to ULoop
     ' do your thing
Next

Open in new window

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 166 total points
ID: 39981665
Never, ever, ever us an Integer data type to hold a row number.  The moment you get to Row 32768, you are looking at an overflow error.

Always use Long for this.  Always.  Hell, I'll go further: it never makes sense to use Integer for anything.  Just use Long, especially since for many operations VBA will implicitly convert your Integer into a Long anyway, and then have to convert it back.
0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 167 total points
ID: 39981666
This corrects line 5 in the above, adds the last used line as the default and checks for invalid input.

Dim ULoop As Integer
Dim i As Long

On Error Resume Next
ULoop = InputBox("Until which row would you like to loop?", "Loop To", ActiveSheet.UsedRange.Rows.Count)
If ULoop = 0 Then
    MsgBox "Invalid or no selection"
    Exit Sub
End If
On error Goto 0
For i = 1 To ULoop
     ' do your thing
Next

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39981668
I stand corrected.
0
 

Author Closing Comment

by:Murray Brown
ID: 39981730
Thanks for  collective answer
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 39981774
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question