Pass what column selected to another procedure

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have many different macros doing different actions within a spreadsheet by the user selecting the column first and the macro takes action on that column.

Example:

Code:
Sub CopyColumnOfData()
    Dim r As Range: Set r = Application.InputBox("Click in the column to filter by", Type:=8)
    Dim iCol As Long: iCol = r.Column
    Columns(iCol).Copy Range("C1")
End Sub
Code:
Sub DeleteColumnOfData()
    Dim r As Range: Set r = Application.InputBox("Click in the column to filter by", Type:=8)
    Dim iCol As Long: iCol = r.Column
    Columns(iCol).Delete
End Sub
How can I use the application.InputBox as its own macro and then pass the iCol variable to the copy and/or delete procedures?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,
If you want to share it for different procedures then maybe create a function


Code:
Function GetColumn() As Variant
    On Error Resume Next
    Set GetColumn = Application.InputBox("Click in the column to filter by", Type:=8)
    If Not GetColumn Is Nothing Then GetColumn = CLng(GetColumn.Column)
    On Error GoTo 0
End Function


Your codes:

Code:
Sub CopyColumnOfData()
    Dim iCol As Variant
    iCol = GetColumn
'cancel pressed
    If iCol = False Then Exit Sub
    Columns(iCol).Copy Range("C1")
End Sub




Sub DeleteColumnOfData()
    Dim iCol As Variant
    iCol = GetColumn
'cancel pressed
    If iCol = False Then Exit Sub
    Columns(iCol).Delete
End Sub

You will note that I changed iCol data type to Variant as Cancel button returns False.

If idea does what you want you could add an argument to Function to give ability for it to return either the selected Column or Row if needed for other requirements.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,216,207
Messages
6,129,510
Members
449,514
Latest member
swillow

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