VBA: Choose with drop down list column A or B to calculate

zeno

Board Regular
Joined
Feb 16, 2012
Messages
71
Hello,
I have 2 columns with a variable number of rows in Excel. With VBA I want to perform a calculation on either column A or B.
I make that selection through another specific cell where I define a drop-down list with titles of each column to choose from. Through the selection of a column in that cell VBA will make its calculation.
How can I indicate this?
I hope my question is clear.
Thanks!
 

tigeravatar

Well-known Member
Joined
Aug 12, 2011
Messages
760
One way, assuming the cell containing the drop-down list with the A1 and B1 headers is E1:
Code:
Sub tgr()
 
    Dim rng As Range
 
    Set rng = Range("A1:B1").Find(Range("E1").Value)
    If Not rng Is Nothing Then
        Set rng = Range(rng, Cells(Rows.Count, rng.Column).End(xlUp))
        'Perform calculation
        MsgBox rng.Address
    End If
 
End Sub
 

zeno

Board Regular
Joined
Feb 16, 2012
Messages
71
Thank you for your response.
I get however message 'run-time error 1004: range of object _Global failed' on line

Set rng = Range(rng, Cells(Rows.Count, rng.Column).End(xlUp))
Why would that happen?
 

Forum statistics

Threads
1,081,415
Messages
5,358,533
Members
400,502
Latest member
price83

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top