How can I use a command button in excel to set the value of multiple cells in one click

0 votes

I'm trying to set up a spreadsheet with an activex command button that, when clicked, modifies the value of many cells. I feel like I'm missing something very basic here. However, it always seems to simply run the first line that affects a cell value when clicked, ignoring the others. I'm not sure how to get all of the lines to function, but each one works on its own if I put it at the top.

My code is as follows:

Private Scan As Integer

Private Sub CommandButton1_Click()                      
Scan = Sheet1.Cells(5, "A") + 1                         
Sheet1.Cells(5, "A") = Scan                             
Sheet1.Cells(8, "A") = Scan + 1
Sheet2.Cells(Scan + 4, "A") = Scan                      
Sheet2.Cells(Scan + 4, "A") = Sheet1.Cells(19, "B")     
End Sub

The button modifies cell A5 as desired if I use the above, but it has no effect on cell A8 or any other cell on sheet 2 if I use the above. Since if I comment out the line altering cell A5 suddenly the one changing cell A8 works, I'm quite sure it's not a problem with the individual lines.

Oct 24 in Others by Kithuzzz
• 20,660 points
65 views

1 answer to this question.

0 votes

Try this:

Private Scan As Integer

Private Sub CommandButton1_Click()                      
    Scan = Sheet1.Cells(5, "A").Value + 1                         
    Sheet1.Cells(5, "A").Value = Scan                             
    Sheet1.Cells(8, "A").Value = Scan + 1
    Sheet2.Cells(Scan + 4, "A").Value = Scan                      
    Sheet2.Cells(Scan + 4, "A").Value = Sheet1.Cells(19, "B")     
End Sub
answered Oct 24 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How to retrieve the value of a text field in Flutter App?

Hi@akhtar, In your TextField, you can call one ...READ MORE

answered Sep 3, 2020 in Others by MD
• 95,380 points
1,027 views
0 votes
1 answer
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,720 points
454 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
2,227 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9 in Others by gaurav
• 22,040 points
71 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
210 views
0 votes
1 answer

Excel - How can I get the average of cells where the value in one column is X and the value in another column is Y?

Use AVERAGEIFS ... =AVERAGEIFS(C2:C13,A2:A13,"Yellow Typ ...READ MORE

answered Nov 11 in Others by narikkadan
• 37,660 points
43 views
0 votes
1 answer

How can I sort one set of data to match another set of data in Excel?

In addition, INDEX MATCH is a more ...READ MORE

answered Oct 29 in Others by narikkadan
• 37,660 points
46 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP