Run a macro in active (current cell)

wtom0412

Board Regular
Joined
Jan 3, 2015
Messages
176
Office Version
  1. 365
Platform
  1. Windows
Hi all, I hope my question is a simple one. I need to run the following code on many cells in my worksheet.

What I would like to do is click in a cell - run the macro, click in the next cell - run the macro etc etc etc. until all of the cells I need to effect are done.

When I recorded the macro, I hard coded the cell reference in it (F6).

Is there something I can change in the code that will tell it to run in the active cell?

Code:
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+u
'
    Selection.TextToColumns Destination:=Range("F6"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True

End Sub

Thank you all as always for your brilliance.

Cheers WT
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
try
Code:
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+u
'
    Selection.TextToColumns Destination:=activecell.value, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True

End Sub
 
Upvote 0
Thank you Michael,

When I run the macro, I get...

Run-time error '1004':
TextToColumns method of Range class failed

The macro works if I change the cell reference in my original code to the new cell.

Cheers WT
 
Upvote 0
F6 is the destination of the the text to columns.....is that what you want to change ??
Sound like you'd be better of with a loop OR selecting the entire range that you want to Text to Columns !
 
Upvote 0
Hi Michael,

Yes, I figured it out, it was better to do it by the column.

Thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,203,534
Messages
6,055,954
Members
444,839
Latest member
laurajames

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