Change my sheets

juneau730

Board Regular
Joined
Jun 7, 2018
Messages
111
Trying a little humor with the subject. :)

I recorded the following macro in excel, then shortened/simplified it. If I am in Developer Mode and run it from there, it works great, but.. I am trying to assign the code to a button. When I do and click the button, it appears to be running on the sheet where the button is, not the sheet where my data is.

Searching, I was not able to find how to correct this.

Code:
Sub Configure_Dates()    Range("E2:E7500").Select
    Selection.Replace What:=",", Replacement:=", ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("F2:F7500").Select
    Selection.Replace What:="@", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("E2:E7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("G2:G7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("H2:H7500").Select
    Selection.NumberFormat = "m/d/yyyy"
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You'll need to go to the sheet first, change "sheettorunmacroon" to "Sheet1" or whatever the sheet name is.

Code:
Sub Configure_Dates()    

Sheets("sheettorunmacroon").Select
Range("E2:E7500").Select
    Selection.Replace What:=",", Replacement:=", ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("F2:F7500").Select
    Selection.Replace What:="@", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("E2:E7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("G2:G7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("H2:H7500").Select
    Selection.NumberFormat = "m/d/yyyy"
End Sub
 
Upvote 0
Good morning @mrshl9898, thank you for your help, that did the trick. For some reason, adding that line is causing an error when executed at this part of the marco

Code:
[COLOR=#333333]Sub Configure_Dates()    [/COLOR]    Range("E2:E7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("G2:G7500").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("H2:H7500").Select
    Selection.NumberFormat = "m/d/yyyy"
End Sub

It doesn't seem to like the Selection.NumberFormat string. If I remove the above, the macro works and my date to whole number formulas still work, though I would like to change the date format.
 
Last edited:
Upvote 0
How about
Code:
Sub Configure_Dates()
   With Sheets("[COLOR=#ff0000]Sheetname[/COLOR]")
      .Range("E2:E7500").Replace What:=",", Replacement:=", ", LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
      .Range("F2:F7500").Replace What:="@", Replacement:=" ", LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
      .Range("E2:E7500").NumberFormat = "m/d/yyyy"
      .Range("G2:G7500").NumberFormat = "m/d/yyyy"
      .Range("H2:H7500").NumberFormat = "m/d/yyyy"
   End With
End Sub
Change name in red to suit
 
Upvote 0
Thanks @Fluff, tried that and for whatever reason, it still errors at

Code:
[COLOR=#333333]      .Range("E2:E7500").NumberFormat = "m/d/yyyy"[/COLOR]

It works without those three lines, so I can at least move forward with it, until it's figured out why it doesn't like those lines.
 
Upvote 0
What error message do you get?
 
Upvote 0
Yes, the sheet is, I have three columns with formulas in them I was protecting.

Interesting, even though the columns with the dates needing the format change were not protected/locked and could be altered manually, formatting was not allowed. Once I removed protection it works, which raises a dilemma, protect the sheet to preserve the formulas or allow date formatting.

Thanks your your help with this, you guys are awesome!
 
Upvote 0
You can unprotect/protect the sheet in code
Code:
Sub Configure_Dates()
   With Sheets("pcode")
      .Unprotect Password:="[COLOR=#ff0000]password[/COLOR]"
      .Range("E2:E7500").Replace What:=",", Replacement:=", ", LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
      .Range("F2:F7500").Replace What:="@", Replacement:=" ", LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
      .Range("E2:E7500").NumberFormat = "m/d/yyyy"
      .Range("G2:G7500").NumberFormat = "m/d/yyyy"
      .Range("H2:H7500").NumberFormat = "m/d/yyyy"
      .Protect Password:="[COLOR=#ff0000]password[/COLOR]"
   End With
End Sub
You may need to record a macro of you protecting the sheet, to find the protection you want.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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