VBA newbie - need help with code to delete certain cells, rename sheets and ignore master sheet

Hayley Howells

New Member
Joined
May 20, 2014
Messages
6
Hi there, thanks for reading - I'm a total newbie to VBA so this is a steep learning curve for me. I am working on a file for person identifiable information to be collected, but the file needs code to anonymise the information before it can be returned to me. Essentially I have a "Master" sheet to collect data for each person, which is hidden, and another sheet called "Instructions".

I already have code for the master sheet to be replicated using a button for adding a new patient, which includes a message box to write the patient's name into the tab name, so whilst data is collected, the workbook will contain a variable number of worksheets all named after individual people.

What I want to do is create another button to be able to clear the contents of cells b2 to c7 in each of the sheets named after a patient, and also to rename all the patient sheets, something unique like patient 1, patient 2 etc, all the while ignoring the master and instruction sheets. Is this do-able?

Any help appreciated. :)
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Welcome to the board. This code should get you started, though you should make a copy of your file before testing it:
Code:
Sub FormatWorkbook()

Dim i as Long, j as Long
j = 1

Application.ScreenUpdating = False

For i = 1 to Worksheets.Count
  With Sheets(i)
    Select Case .Name
      Case "Master", "Instructions"
        'Do Nothing
      Case Else
         .Range("B2:C7").ClearContents
         .Name = "Patient " & j
         j = j + 1
    End Select
Next i

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hiya, thank you so much for your help. I have tried this code in my workbook but it returns an error "compile error: next without for", and the "Sub FormatWorkbook" text on line 1 is highlighted in yellow.... hoping you can help... so close!
 
Upvote 0
My bad for trying to write out the code without testing it. Missing line in red, try:
Rich (BB code):
Sub FormatWorkbook()

Dim i As Long, j As Long
j = 1

Application.ScreenUpdating = False

For i = 1 To Worksheets.Count
    With Sheets(i)
        Select Case .Name
            Case "Master", "Instructions"
                'Do Nothing
            Case Else
                .Range("B2:C7").ClearContents
                .Name = "Patient " & j
                j = j + 1
        End Select
    End With
Next i

Application.ScreenUpdating = True

End Sub
 
Upvote 0
I think Jack'code missed to close "With". You just need to add "End with".
He fixed it already :D

I dont understand the problem with "the "Sub FormatWorkbook" text on line 1 is highlighted in yellow"
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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