• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Making a Macro independent of worksheet

AlexAsh

New Member
Hello,

I have written a macro that works as I want in the worksheet it was written in. But, when I try to run the macro from the 2nd worksheet, the macro just runs in the original.
I think I must be missing an ActiveWorksheet, but I can't find where to insert it.

Thanks.

Code:
Sub SRRemoval()
Dim LR As Long, i As Long
LR = Range("D" & Rows.Count).End(xlUp).Row

For i = 2 To LR
 
  With Range("D" & i)
  If Mid(.Value, 4, 1) = "/" Then
  .Value = Right(.Value, Len(.Value) - 6)
  End If
  End With
  
Next i
End Sub
 
Because your code doesn't define where it is running it will assume the active sheet is the sheet to act on

This may be due to you saving the code in the worksheet circled

upload_2015-6-11_23-3-21.png

The code should be in a code module highlighted in Yellow

Right click anywhere on your project and Insert Code Module
 
Back
Top