VBA Macro to add data to selected cells

Cquake

Board Regular
Joined
Dec 12, 2017
Messages
57
Office Version
  1. 2016
Platform
  1. Windows
The below VBA code I am using will add new figures to the old figures by inserting a number in the popup box that appears once I enter a number I want added. IE: If the totals in pic below are 7,5,4,8 and I type in 7 it will add 7 to each column. What I want to be able to do is enter a seperate add in number for each column in that row all at the same time and then once I hit ok it will add them in to the previous numbers. (IE: If I enter 4,8,7,9, the new totals would be 11,13,11,17) This would allow me to be able to do a row at a time with different add in numbers updating those columns. As it stands now I would have to select them by themselves and then enter the number total I needed added in. Here is the test sheet.

1599495373824.png



What would I need to change in the code below in order to accomplish this?

Dim WS As Worksheet
Dim RngSel As Range
Dim Num As Double
Dim i As Long
Dim j As Long
Dim Rows As Long
Dim Cols As Long
Dim Arr() As Variant
Dim strPromp As String
Set RngSel = Selection
Rows = RngSel.Rows.Count
Cols = RngSel.Columns.Count
strPrompt = "Enter number to add to selected cells"

Num = InputBox(strPrompt, "Number to add", 7)

If RngSel.Count = 1 Then
RngSel = RngSel + Num
Else
Arr = RngSel
For i = 1 To Rows
For j = 1 To Cols
Arr(i, j) = Arr(i, j) + Num
Next j
Next i
RngSel.Value = Arr
End If
'this will input any value into any cell you wish and add it to previous total
 

Attachments

  • 1599495267407.png
    1599495267407.png
    205.2 KB · Views: 29
  • 1599495293626.png
    1599495293626.png
    205.2 KB · Views: 30

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Just to add any cell(s) I select prior to hitting the VBA macro button are the cells that will update.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

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