move the next information to the row below

Sandler

Board Regular
Joined
Sep 15, 2011
Messages
165
The code below shows where i want to start placing information that I get from my form.
After the information is inputted via the button click, how do i get the next information to go into
the appropriate place?


For example, when Melissa is selected the listbox value will go to A16 and the txt value will go to B16.
If Melissa continues to be selected and I pick another listbox and txt, it needs to go to A17 and B17.


Private Sub btnAdd_Click()

If opnMelissa.Value = True Then Worksheets("Our Data").Range("A16").Value = Me.lbxNutrition.Value
If opnMelissa.Value = True Then Worksheets("Our Data").Range("B16").Value = Me.txtServings.Value

If opnDaniel.Value = True Then Worksheets("Our Data").Range("P16").Value = Me.lbxNutrition.Value
If opnDaniel.Value = True Then Worksheets("Our Data").Range("Q16").Value = Me.txtServings.Value

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Do you mean:
When Melissa is selected from the listbox the value Melissa will go to A16?
And the txt value will go to B16. What text value?
 
Upvote 0
maybe this...
Code:
Private Sub btnAdd_Click()
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
If opnMelissa.Value = True Then Worksheets("Our Data").Range("A" & lr + 1).Value = Me.lbxNutrition.Value
If opnMelissa.Value = True Then Worksheets("Our Data").Range("B" & lr + 1).Value = Me.txtServings.Value

If opnDaniel.Value = True Then Worksheets("Our Data").Range("P" & lr + 1).Value = Me.lbxNutrition.Value
If opnDaniel.Value = True Then Worksheets("Our Data").Range("Q" & lr + 1).Value = Me.txtServings.Value

End Sub
 
Upvote 0
See if following does what you want?

Code:
Private Sub btnAdd_Click()
    Dim lr As Long, c As Long
    
    With Worksheets("Our Data")

    lr = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    If lr < 16 Then lr = 16
    
    c = IIf(opnMelissa.Value, 1, 16)
    
    .Cells(lr, c).Resize(1, 2).Value = Array(Me.lbxNutrition.Value, Me.txtServings.Value)
    
    End With


End Sub


Dave
 
Last edited:
Upvote 0
Hi dmt32, your code works perfectly for Melissa.
But I also need to add code if opnDaniel is selected. Would that be 12, 16 instead of the 1,16 for Melissa?
How can i modify the code for Daniel?

Thanks :)
 
Last edited:
Upvote 0
Hi dmt32, your code works perfectly for Melissa.
But I also need to add code if opnDaniel is selected. Would that be 12, 16 instead of the 1,16 for Melissa?
How can i modify the code for Daniel?

Thanks :)

Should not need any adjustment for second optionbutton Daniel - this line:

Code:
c = IIf(opnMelissa.Value, 1, 16)

Sets Variable c to 1 (Column A) if Melissa is selected (value True) or c to 16 (Column P) if Melissa is not selected (value False)

Post back with changes you made.

Dave
 
Upvote 0
Hi Dave,
The code works perfectly for Melissa, but is a little off for Daniel.
In each instance i tried to put two different pieces of food for each individual and this was my results.
Both of Melissa's items show up, but only the last item shows up for Daniel, 3 rows down.

Our Data

*ABCDEFGHIJOPQRSTUVWXY
15Melissaservings*Protein*Carbs*Fat*Calories*Danielservings*Protein*Carbs*Fat*Calories
16Wild Blueberry Jelly - FullCircle1*0*8*0*32***********
17White Cheddar Corn Puffs - TJ's2*6*42*10*282***********
18***********WWCheeseString3*18*0*7.5*139.5

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:309px;"><col style="width:64px;"><col style="width:64px;"><col style="width:65px;"><col style="width:19px;"><col style="width:84px;"><col style="width:19px;"><col style="width:78px;"><col style="width:19px;"><col style="width:64px;"><col style="width:37px;"><col style="width:122px;"><col style="width:64px;"><col style="width:19px;"><col style="width:61px;"><col style="width:19px;"><col style="width:64px;"><col style="width:19px;"><col style="width:70px;"><col style="width:19px;"><col style="width:64px;"></colgroup><tbody>
</tbody>
 
Upvote 0
This is the modification to the code I ended up using for it to work. I don't full understand what i did, but I guess it's right.
It is using dmt32 code and inputting some opposing items for a separate daniel part.
See code below and if someone wants to explain to me why HTML Jeanie shows asterisks in my screen code, I would appreciate it.

Private Sub btnAdd_Click()
With Worksheets("Our Data")

If Me.opnMelissa.Value Then

'you clicked on melissa....do the code for melissa

lr = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
If lr < 16 Then lr = 16

c = IIf(opnMelissa.Value, 1, 16)

.Cells(lr, c).Resize(1, 2).Value = Array(Me.lbxNutrition.Value, Me.txtServings.Value)


Else

'you clicked on daniel....do the code for daniele

dr = .Cells(.Rows.Count, "P").End(xlUp).Row + 1
If dr < 16 Then dr = 16

d = IIf(opnDaniel.Value, 16, 1)

.Cells(dr, d).Resize(1, 2).Value = Array(Me.lbxNutrition.Value, Me.txtServings.Value)


End If

End With

End Sub
 
Upvote 0
Hi,
See if this slight change to my original code does what you want:

Code:
Private Sub btnAdd_Click()
    Dim lr As Long, c As Long
    
    With Worksheets("Our Data")
    
    c = IIf(opnMelissa.Value, 1, 16)
    
    lr = .Cells(.Rows.Count, c).End(xlUp).Row + 1
    If lr < 16 Then lr = 16
    
    .Cells(lr, c).Resize(1, 2).Value = Array(Me.lbxNutrition.Value, Me.txtServings.Value)
    
    End With


End Sub


Dave
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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