VBA finding column number and add specific values in column

Newbie06

New Member
Joined
Apr 6, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a sheet named "FinalOverview":

Enter Year: <Ex: User inputs "2020" in field B1>
201920202021
Final Amount<Value to be computed by macro><Value to be computed by macro><Value to be computed by macro>

another sheet called "Data". The "Data" sheet has N columns and each column has specific numerical values. Ex:
Year201920202021
Legend1100020003000
Legend2101215
Legend3344598

The user enters values such as "2020" or "2021" in the Sheet "FinalOverview" and will click a btn to trigger macro. The macro then searches for column having "2020" and then uses the corresponding row values to do some calculation such as "0.85*2000+(12-45)" and then the value needs to be put into the cell of FinalOverview below 2020 (C3)

I am fully confused as to how to extract the column ID, use it to do calculation and put it back into the cell.

Can you please guide me
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Got it to work with another user's help, posting the code here in case anyone needs it in future!

VBA Code:
Option Explicit

Sub NewbieMacro()
Dim intYear As Integer
Dim intCol As Integer
Dim lngOut As Long

intYear = Sheets("FinalOverview").Cells(1, 2)
'Debug.Print "Looking for Year " & intYear
Sheets("FinalOverview").Cells(2, 2) = "I am lost"

With Sheets("Data")
    intCol = 2
    Do Until .Cells(1, intCol).Value = ""
        If .Cells(1, intCol).Value = intYear Then
            '"0.85*2000+(12-45)"
            lngOut = 0.85 * .Cells(2, intCol).Value
            lngOut = lngOut + (.Cells(3, intCol).Value - .Cells(4, intCol).Value)
            Sheets("FinalOverview").Cells(2, 2) = lngOut
            Exit Do
        End If
        intCol = intCol + 1
    Loop

End With

End Sub
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,215,262
Messages
6,123,935
Members
449,134
Latest member
NickWBA

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