Input Box Column and Row Offse

wyh

New Member
Joined
Sep 8, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I can't for the life of me figure out how to adjust my code to offset the location of where the answer to my second input box question goes. How my macro is supposed to work. . . if A4 (for example) is my active cell and where the macro starts, that's where I want the answer to the very first question to go. Then, when the second question is asked I want that answer to go to in column c of the same row. After the two questions are asked, I want to populate information for the next row (columns a and c again). When I'm done entering information I just hit enter and the macro stops.

However, what happens is that the answers to the questions only post to the first column no matter what I change the "r" values to.

VBA Code:
Sub infoEntry()
Set c = ActiveCell
 
For r = 0 To 1
For s = 0 To 4
 
val1 = InputBox("Enter item 1:", "Quantity?")

If val1 = "" Then
Exit Sub
Else
c.Offset(s, r).Value = val1
End If
 
Next s
Next r
 
End Sub

What happens:
1631127748128.png


What I'm trying to accomplish:
1631127827017.png


For the description, I've got that covered with a vlookup formula so that doesn't need to be addressed in the code at all.

Thank you in advance for your time : )
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Here is one way:
VBA Code:
Sub infoEntry()

    Dim r As Long
    Dim c As Long
    Dim val1 As String
    Dim ic As Long
    
'   Get active row and column
    r = ActiveCell.Row
    c = ActiveCell.Column
'   Set initial column
    ic = c
    
'   Start loop
    Do
'       Ask for input
        val1 = InputBox("Enter item 1:", "Quantity?")
'       Populate cell
        If val1 = "" Then
            Exit Sub
        Else
            Cells(r, c) = val1
        End If
'       Increment rows and columns
        If c = ic Then
            c = c + 2
        Else
            r = r + 1
            c = ic
        End If
    Loop
 
End Sub
 
Upvote 0
Solution
Here is one way:
VBA Code:
Sub infoEntry()

    Dim r As Long
    Dim c As Long
    Dim val1 As String
    Dim ic As Long
   
'   Get active row and column
    r = ActiveCell.Row
    c = ActiveCell.Column
'   Set initial column
    ic = c
   
'   Start loop
    Do
'       Ask for input
        val1 = InputBox("Enter item 1:", "Quantity?")
'       Populate cell
        If val1 = "" Then
            Exit Sub
        Else
            Cells(r, c) = val1
        End If
'       Increment rows and columns
        If c = ic Then
            c = c + 2
        Else
            r = r + 1
            c = ic
        End If
    Loop
 
End Sub
Works like a charm! Thank you so much for your help :)
 
Upvote 0
You are welcome!

Hopefully my logic makes sense (I tried adding some documentation to it to help explain it).
If you have any questions about any part, please do not hesitate to ask.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top