VBA Code to add data to multiple columns in a row

Status
Not open for further replies.

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 and whatever cells I highlight will be the cells that the macro updates after the data is put in the box.

1599495889000.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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Status
Not open for further replies.

Forum statistics

Threads
1,214,858
Messages
6,121,956
Members
449,057
Latest member
FreeCricketId

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