Solved

Excel VBA use input box to change a row number

Posted on 2014-04-06
6
567 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 26

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 45

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 26

Expert Comment

by:MacroShadow
ID: 39981668
I stand corrected.
0
 

Author Closing Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

707 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now