VBA or Formula to Subtract Fixed Value from Another Column for Huge Data

arielina

New Member
Joined
Jun 9, 2023
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I have a spreadsheet where for each value in column B (eg B2), I need to subtract it from each of the next 10 value in column A (A3-A12) and reflect each answer in column C (C3-12). I will also need to repeat the same for the next value in column B (B3) and reflect the answers in column D.

This will continue till cell B10000 . So the number of columns to house the subtracted values will go to 10,000 too. I think Excel should be able to have more than 10,000 columns.

Is there a VBA or formula to do this?

Book1.xlsx
ABCDEF
1InOut
26521215
325326452317
4326534230502620
53652634343730073310
646841214469403943424050
756653425450502053235031
85522325307487751804888
955363645321489151944902
1032156343000257028732581
116621536406597662795987
121232241017587890598
13513124448647894497
1421327817901498
1556123252704978
1695333428899
17125335
18652135
19253212
203265324
213652352
224684215
Sheet3
Cell Formulas
RangeFormula
C3:C12C3=A3-B$2
D4:D13D4=A4-B$3
E5:E15E5=A5-B$4
F6:F16F6=A6-B$5
 
OK, I am not sure why you didn't show us what you really wanted from the start, seeing as how you even went through all the trouble of posting an example in your original post.
But try this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim r As Long
    Dim c As Long
   
    Application.ScreenUpdating = False
   
'   Set first column to paste to (col "C" is column number 3)
    c = 3
   
'   Find last row with data in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Loop through all rows starting with row 2
    For r = 2 To lr
'       Populate formula
        Range(Cells(r + 1, c), Cells(r + 10, c)).Formula = "=(A" & r + 1 & "-B$" & (c - 1) & ")/B$" & (c - 1)
'       Format cells
        Range(Cells(r + 1, c), Cells(r + 10, c)).NumberFormat = "0.0%"
'       Increment column counter by 1
        c = c + 1
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
Sorry for not thinking it through initially and your VBA works perfectly! Thank you very much!
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Here is an array version I came up with that is very fast

VBA Code:
Sub arielina_V1()
'
    Dim StartTime               As Double
    StartTime = Timer
'
    Dim ArrayColumn             As Long
    Dim ArrayRow                As Long
    Dim ColumnARow              As Long
    Dim ColumnBRow              As Long
    Dim LoopCount               As Long
    Dim ColumnANumbersArray     As Variant
    Dim ColumnBNumbersArray     As Variant
    Dim ResultArray()           As Variant
    Dim TempValue               As Variant
'
    ColumnANumbersArray = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    ColumnBNumbersArray = Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row - 10)
'
'  Check if ResultArray is an array
    If Not IsArray(ColumnBNumbersArray) Then
'  Convert inputVariable to a 2D array
        TempValue = ColumnBNumbersArray
        ReDim ColumnBNumbersArray(1 To 1, 1 To 1)
        ColumnBNumbersArray(1, 1) = TempValue
    End If
'
    ReDim ResultArray(1 To UBound(ColumnBNumbersArray, 1) + 11, 1 To UBound(ColumnBNumbersArray, 1))
'
'    End If
'
    For ArrayColumn = LBound(ColumnBNumbersArray, 1) To UBound(ColumnBNumbersArray, 1)
        ColumnARow = ArrayColumn + 1
        ColumnBRow = ColumnBRow + 1
'
        For LoopCount = 1 To 10
            ColumnARow = ColumnARow + 1
            ResultArray(ColumnARow, ArrayColumn) = FormatPercent((ColumnANumbersArray(ColumnARow, 1) - ColumnBNumbersArray(ColumnBRow, 1)) / ColumnBNumbersArray(ColumnBRow, 1), 1)
        Next
    Next
'
    Range("C1").Resize(UBound(ResultArray, 1), UBound(ResultArray, 2)) = ResultArray
'
    ActiveSheet.UsedRange.EntireColumn.AutoFit
Debug.Print "Time to complete = " & Timer - StartTime & "seconds."
MsgBox "Time to complete = " & Timer - StartTime & "seconds."

End Sub
Thanks for the code but i get this error message, what could be possibly wrong?
1687015388881.png
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,616
Members
452,661
Latest member
Nonhle

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