MsgBox to overwrite sheet - VBA

Status
Not open for further replies.

yomomo

New Member
Joined
Mar 8, 2018
Messages
11
So i recorded a macro that makes a new sheet called "Statistics" and makes some pivot tables. I changed in the macro and made it able to overwrite the sheet if there already is another sheet called "Statistics", now im trying to figure out how to make some sort of msgbox that that tells the user if he would like to overwrite the sheet?
The code i have made looks like this:
Code:
Sub Opgave3()

Dim newSheet As Worksheet


Application.DisplayAlerts = False


Set newSheet = Sheets.Add(After:=ActiveSheet)
With newSheet
    On Error Resume Next
    ThisWorkbook.Sheets("Statistics").Delete
    On Error GoTo 0
    .Name = "Statistics"
End With


Application.DisplayAlerts = True


Application.DisplayAlerts = True
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Base!R1C1:R18288C12", Version:=6).CreatePivotTable TableDestination:= _
        "Statistics!R1C1", TableName:="Pivottabel22", DefaultVersion:=6
    Sheets("Statistics").Select
    Cells(1, 1).Select
    ActiveSheet.PivotTables("Pivottabel22").AddDataField ActiveSheet.PivotTables( _
        "Pivottabel22").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
    With ActiveSheet.PivotTables("Pivottabel22").PivotFields("FACULTY_ID")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Pivottabel22").PivotFields("PROGRAM_TYPE_NAME")
        .Orientation = xlRowField
        .Position = 1
    End With
    Range("A1").Select
    ActiveSheet.PivotTables("Pivottabel22").DataPivotField.PivotItems( _
        "Antal af FACULTY_ID").Caption = "Antal"
    Range("B1").Select
    ActiveSheet.PivotTables("Pivottabel22").CompactLayoutColumnHeader = "Fakultet"
    Range("A7").Select
    ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel22").PivotCache. _
        CreatePivotTable TableDestination:="Statistics!R7C1", TableName:= _
        "Pivottabel23", DefaultVersion:=6
    Sheets("Statistics").Select
    Cells(7, 1).Select
    ActiveSheet.PivotTables("Pivottabel23").AddDataField ActiveSheet.PivotTables( _
        "Pivottabel23").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
    With ActiveSheet.PivotTables("Pivottabel23").PivotFields("PROGRAM_TYPE_NAME")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Pivottabel23").PivotFields("FACULTY_ID")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Pivottabel23").PivotFields("Antal af FACULTY_ID")
        .Calculation = xlPercentOfTotal
        .NumberFormat = "0.00%"
    End With
    Range("A7").Select
    ActiveSheet.PivotTables("Pivottabel23").DataPivotField.PivotItems( _
        "Antal af FACULTY_ID").Caption = "Procentvis"
    Range("B7").Select
    ActiveSheet.PivotTables("Pivottabel23").CompactLayoutColumnHeader = "Fakultet"
    Range("A13").Select
    ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel23").PivotCache. _
        CreatePivotTable TableDestination:="Statistics!R13C1", TableName:= _
        "Pivottabel24", DefaultVersion:=6
    Sheets("Statistics").Select
    Cells(13, 1).Select
    ActiveSheet.PivotTables("Pivottabel24").AddDataField ActiveSheet.PivotTables( _
        "Pivottabel24").PivotFields("ENROLL_LOCATION_NAME"), _
        "Antal af ENROLL_LOCATION_NAME", xlCount
    With ActiveSheet.PivotTables("Pivottabel24").PivotFields("ENROLL_LOCATION_NAME" _
        )
        .Orientation = xlRowField
        .Position = 1
    End With
    Range("B13").Select
    ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems( _
        "Antal af ENROLL_LOCATION_NAME").Caption = "Antal"
    Range("A13").Select
    ActiveSheet.PivotTables("Pivottabel24").CompactLayoutRowHeader = "Campus"
    Range("B13").Select
    ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems("Antal"). _
        Caption = "Antal af studerende"
    Range("A22").Select
    ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel24").PivotCache. _
        CreatePivotTable TableDestination:="Statistics!R22C1", TableName:= _
        "Pivottabel25", DefaultVersion:=6
    Sheets("Statistics").Select
    Cells(22, 1).Select
    ActiveSheet.PivotTables("Pivottabel25").AddDataField ActiveSheet.PivotTables( _
        "Pivottabel25").PivotFields("ENROLL_LOCATION_NAME"), _
        "Antal af ENROLL_LOCATION_NAME", xlCount
    With ActiveSheet.PivotTables("Pivottabel25").PivotFields("ENROLL_LOCATION_NAME" _
        )
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Pivottabel25").PivotFields( _
        "Antal af ENROLL_LOCATION_NAME")
        .Calculation = xlPercentOfTotal
        .NumberFormat = "0.00%"
    End With
    Range("A22").Select
    ActiveSheet.PivotTables("Pivottabel25").CompactLayoutRowHeader = "Campus"
    Range("B22").Select
    ActiveSheet.PivotTables("Pivottabel25").DataPivotField.PivotItems( _
        "Antal af ENROLL_LOCATION_NAME").Caption = "Procentvis af studerende"
    Range("I1").Select
    ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel25").PivotCache. _
        CreatePivotTable TableDestination:="Statistics!R1C9", TableName:= _
        "Pivottabel26", DefaultVersion:=6
    Sheets("Statistics").Select
    Cells(1, 9).Select
    ActiveSheet.PivotTables("Pivottabel26").AddDataField ActiveSheet.PivotTables( _
        "Pivottabel26").PivotFields("STUDYBOARD_ID"), "Antal af STUDYBOARD_ID", xlCount
    With ActiveSheet.PivotTables("Pivottabel26").PivotFields("STUDYBOARD_ID")
        .Orientation = xlRowField
        .Position = 1
    End With
    Range("I1").Select
    ActiveSheet.PivotTables("Pivottabel26").CompactLayoutRowHeader = "Studienævn"
    Range("J1").Select
    ActiveSheet.PivotTables("Pivottabel26").DataPivotField.PivotItems( _
        "Antal af STUDYBOARD_ID").Caption = "Antal af studerende"
    Range("L15").Select
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread.
Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

Note that sometimes posts from new users require Moderator approval before you can see them on the public forums. When this happens, you should see a message to that effect when you try to post it. Please be patient and do not attempt to post the question again.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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