Solved

Excel VBA use input box to change a row number

Posted on 2014-04-06
6
574 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:murbro
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 46

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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39981668
I stand corrected.
0
 

Author Closing Comment

by:murbro
ID: 39981730
Thanks for  collective answer
0
 
LVL 46

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

810 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