Need help modifying code to make user friendly

AHoenes

New Member
Joined
Apr 14, 2009
Messages
6
I have a piece of code that will only work if the column the data is to be pulled from is column"A". I would love to have the code modified so that this will do the same thing if the data varies in location from spreadsheet to spreadsheet.

If for instance, on a spreadsheet the data is in column "E". I would like to be able to select either E1 or select the entire column and run the macro to extract the last 2 positions.

Any help would be appreciated. Here is the current code.

Code:
Sub Extract()
'
' Extract data from column A
'
'
Dim data As Range
Set data = Range(Range("A2"), Range("A65536").End(xlUp))
Application.ScreenUpdating = False
Selection.Offset(0, 1).EntireColumn.Insert
With data
    .Offset(0, 1).FormulaR1C1 = "=Right(RC[-1],2)"
    .EntireRow.Sort Key1:=data(1, 1).Offset(0, 1), key2:=data(1, 1)
End With
Range("B1").Select
Selection.Offset(0, 1).EntireColumn.Insert
Selection.EntireColumn.Select
Selection.Copy
    
Selection.Offset(0, 1).Select
        
'paste special "extract"

Selection.PasteSpecial Paste:=xlPasteValues, Operation: _
=xlNone, SkipBlanks:=False, Transpose:=False

Application.CutCopyMode = False
Selection.Offset(0, -1).Select
Selection.Delete Shift:=xlToLeft
Cells(1, Selection.Column).Select
ActiveCell.FormulaR1C1 = "Extract"
 
Application.ScreenUpdating = True
 
End Sub
 
Last edited by a moderator:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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