Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entering a value in next empty cell

Status
Not open for further replies.

szynszyl

MIS
Dec 30, 2007
11
IT
Hi,
I'm newbie about VBA Excel 2007 , i've found this example on J.Walkenbach book but when I insert the text in inputbox the the value in cell is not update , it remain blank, what could it be the problem?

Sub getdata()
Dim nextrow As Long
Dim entry1 As String, entry2 As String

Do
nextrow = Cells(Rows.Count, 1).End(xlUp).Row + 1

entry1 = InputBox("enter the name")
If entry1 = " " Then Exit Sub
entry2 = InputBox("enter the amount")
If entry2 = " " Then Exit Sub

Cells(nextrow, 1) = entry1
Cells(nextrow, 2) = entry2

Loop
End Sub
 

hi,

your code does work. but I would not do it quite that way.

First, get into the habit of referencing ALL objects to their parent. If you will have multiple workbooks open when you run this procedure, then your code ought to reference the proper workbook as well.
Code:
Sub getdata()
    Dim nextrow As Long
    Dim entry1 As String, entry2 As String
[b]    
    With Worksheets("Sheet1")[/b]
        Do
            nextrow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1  '[b]
'notice the DOT       ^      ^
'refers to the        ^      ^
'   With Worksheets("Sheet1") statement above[/b]
            entry1 = InputBox("enter the name")
            If entry1 = " " Then Exit Sub
            entry2 = InputBox("enter the amount")
            If entry2 = " " Then Exit Sub
            
            .Cells(nextrow, 1) = entry1
            .Cells(nextrow, 2) = entry2 '[b]
'           ^
'           ^[/b]
        Loop
    End With
End Sub

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
The code works, are you sure you entered it correctly?

Also notice that the code expects the entry to be a "blank or space" before the loop ends.

If you simply leave the entry empty the loop will not end.

And yes Skip's version is the correct way to write this code.

Good Luck

sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top