Excel vba command button code (userform)

tariq096

New Member
Joined
Dec 13, 2019
Messages
7
Office Version
  1. 2010
Platform
  1. Mobile
When I click a location and then the “ok” command button I want it display values from the entire column of the location I selected . can someone please help me figure out what is the code to do this ?
 

Attachments

  • B0957C59-0B75-4FF3-B953-D3F7211846A9.jpeg
    B0957C59-0B75-4FF3-B953-D3F7211846A9.jpeg
    29.5 KB · Views: 23

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
when I select the location and click ok I want it displayed on the same worksheet. Thank you for responding . I need to figure out the code :(
 
Upvote 0
For example if I click a specific location and then hit the “ok” button , I just want to see the entire column as well as the years in the first column displayed side by side
 
Upvote 0
i know but I have to use a vba component in my project. So for example if I click “Acre” which is a location and then hit the ok button , I would like to see the years in the first column (1988-2013) and the values for each year in the column acre side by side . having difficulty coming up with this code . Any help will be greatly appreciated
 
Upvote 0
Create a new class module & call it ClsOptBtn then put this code in it.
VBA Code:
Option Explicit
Public WithEvents Optbtn As MSForms.OptionButton
Private Sub OptBtn_Click()
    Dim Fnd As Range
    Set Fnd = Range("2:2").Find(Optbtn.Caption, , , xlWhole, , , False, , False)
    If Not Fnd Is Nothing Then
        Cells.EntireColumn.Hidden = False
        If Fnd.Column > 2 Then Range("B1", Cells(1, Fnd.Column - 1)).EntireColumn.Hidden = True
    End If
End Sub
Then in the UserForm module put
Rich (BB code):
Private OptBtnEvent As Collection

Private Sub UserForm_Initialize()
    Dim Ctrl As MSForms.Control
    Dim Obtn As ClsOptBtn

    Set OptBtnEvent = New Collection
    For Each Ctrl In Me.Controls
        If TypeOf Ctrl Is MSForms.OptionButton Then
            Set Obtn = New ClsOptBtn
            Set Obtn.Optbtn = Ctrl
            OptBtnEvent.Add Obtn
        End If
    Next
End Sub
The line in blue must be at the very top of the module, before any code.
When you click an option button, it will hide the relevant columns
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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