Create a new worksheet and rename tab

united2017

New Member
Joined
Jun 17, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to create a macro where a new worksheet tab is created, and the macro to rename the tab as per Cell F4, the values in column H - 'This Period' is added to Column G - "Cumulative Previous" and then column H is blanked out.

Also with the claim number, Is there a way of automatically populating that field instead of manually typing it in?

Many Thanks,

P
 

Attachments

  • Screenshot.png
    Screenshot.png
    94 KB · Views: 13
Version 2

VBA Code:
Sub ReplicateSheet_v02()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim strToFind As String
    Dim rngSubTot1 As Range, rngSubTot2 As Range
    Dim rngSect1 As Range, rngSect2 As Range, rCell As Range
    
    Set srcSht = Worksheets(1)
    strToFind = "SubTotal"
    
    srcSht.Copy before:=Worksheets(1)
    Set destSht = ActiveSheet
    
    With destSht
        .Unprotect
        .Range("F4").Value = .Range("F4").Value + 1
        .Name = .Range("F4")
    End With
    
    With destSht.Columns("C")
        Set rngSubTot1 = .Find(What:=strToFind, after:=.Cells(1, 1), _
                        LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        Set rngSubTot2 = .FindNext(after:=rngSubTot1)
    End With
    
    With destSht
        Set rngSect1 = .Range(.Cells(10, "G"), .Cells(rngSubTot1.Row - 1, "G"))
        Set rngSect2 = .Range(.Cells(rngSubTot1.Row + 2, "G"), .Cells(rngSubTot2.Row - 1, "G"))
        
        rngSect1.Value = rngSect1.Offset(, 2).Value
        rngSect2.Value = rngSect2.Offset(, 2).Value
        rngSect1.Offset(, 1).ClearContents
        rngSect2.Offset(, 1).ClearContents
    End With
    
    destSht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                    False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
                    AllowInsertingRows:=True, AllowDeletingRows:=True
End Sub
 
Upvote 0
Solution

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Version 2

VBA Code:
Sub ReplicateSheet_v02()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim strToFind As String
    Dim rngSubTot1 As Range, rngSubTot2 As Range
    Dim rngSect1 As Range, rngSect2 As Range, rCell As Range
   
    Set srcSht = Worksheets(1)
    strToFind = "SubTotal"
   
    srcSht.Copy before:=Worksheets(1)
    Set destSht = ActiveSheet
   
    With destSht
        .Unprotect
        .Range("F4").Value = .Range("F4").Value + 1
        .Name = .Range("F4")
    End With
   
    With destSht.Columns("C")
        Set rngSubTot1 = .Find(What:=strToFind, after:=.Cells(1, 1), _
                        LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        Set rngSubTot2 = .FindNext(after:=rngSubTot1)
    End With
   
    With destSht
        Set rngSect1 = .Range(.Cells(10, "G"), .Cells(rngSubTot1.Row - 1, "G"))
        Set rngSect2 = .Range(.Cells(rngSubTot1.Row + 2, "G"), .Cells(rngSubTot2.Row - 1, "G"))
       
        rngSect1.Value = rngSect1.Offset(, 2).Value
        rngSect2.Value = rngSect2.Offset(, 2).Value
        rngSect1.Offset(, 1).ClearContents
        rngSect2.Offset(, 1).ClearContents
    End With
   
    destSht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
                    False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
                    AllowInsertingRows:=True, AllowDeletingRows:=True
End Sub
Brilliant, you legend! thank you!
 
Upvote 0
Brilliant, you legend! thank you!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.

Two other things:
  1. I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

  2. When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you in post #5. Compare the look and readability of that code to your code in post #3. :)
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

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