?
Solved

Excel VBA use input box to change a row number

Posted on 2014-04-06
6
Medium Priority
?
597 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 668 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 664 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 49

Accepted Solution

by:
Martin Liss earned 668 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 49

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

Industry Leaders: 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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

764 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