VBA Pivot Table code working when stepped through, but not when assigned to a Macro Button











up vote
0
down vote

favorite












My code that formats certain rows on my pivot table works when I step through the code, step by step. However, when I assigned the macro to a button, with other macros, it didn't complete the formatting in the finished product. As a result, when I copied the pivot table data across to a new sheet, the correct formatting wasn't present.



Basically I am trying to format certain rows in the pivot table, and then copy the values and format of the pivot table to a new sheet and format the rest there (as formatting the pivot table is slowing down my macro considerably).



Here is the start of my code:



Sub formatpt()
'formatpt Macro

Dim ws As Worksheet
Set ws = Worksheets("Pivot")
Dim pt As PivotTable
Set pt = Worksheets("Pivot").PivotTables("Dashboard")

pt.ShowTableStyleRowHeaders = False
pt.ShowDrillIndicators = False

pt.PivotSelect "Region[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(128, 128, 128)
Selection.RowHeight = 40
Selection.Font.Bold = True
Selection.Font.Color = vbWhite

pt.PivotSelect "Desk[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(166, 166, 166)
Selection.RowHeight = 35
Selection.Font.Bold = True

pt.PivotSelect "Style[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(217, 217, 217)
Selection.RowHeight = 20
Selection.Font.Bold = True

Set ws2 = Worksheets("Final")

ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


The row heights don't copy across either, but this is less important. But if you do know how to copy across row height as well as the formatting and values, then please let me know.



Any help is greatly appreciated.



Thanks










share|improve this question


















  • 1




    Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
    – Comintern
    Nov 22 at 16:43










  • Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
    – H Smith
    Nov 22 at 16:48












  • Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
    – H Smith
    Nov 22 at 17:42















up vote
0
down vote

favorite












My code that formats certain rows on my pivot table works when I step through the code, step by step. However, when I assigned the macro to a button, with other macros, it didn't complete the formatting in the finished product. As a result, when I copied the pivot table data across to a new sheet, the correct formatting wasn't present.



Basically I am trying to format certain rows in the pivot table, and then copy the values and format of the pivot table to a new sheet and format the rest there (as formatting the pivot table is slowing down my macro considerably).



Here is the start of my code:



Sub formatpt()
'formatpt Macro

Dim ws As Worksheet
Set ws = Worksheets("Pivot")
Dim pt As PivotTable
Set pt = Worksheets("Pivot").PivotTables("Dashboard")

pt.ShowTableStyleRowHeaders = False
pt.ShowDrillIndicators = False

pt.PivotSelect "Region[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(128, 128, 128)
Selection.RowHeight = 40
Selection.Font.Bold = True
Selection.Font.Color = vbWhite

pt.PivotSelect "Desk[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(166, 166, 166)
Selection.RowHeight = 35
Selection.Font.Bold = True

pt.PivotSelect "Style[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(217, 217, 217)
Selection.RowHeight = 20
Selection.Font.Bold = True

Set ws2 = Worksheets("Final")

ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


The row heights don't copy across either, but this is less important. But if you do know how to copy across row height as well as the formatting and values, then please let me know.



Any help is greatly appreciated.



Thanks










share|improve this question


















  • 1




    Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
    – Comintern
    Nov 22 at 16:43










  • Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
    – H Smith
    Nov 22 at 16:48












  • Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
    – H Smith
    Nov 22 at 17:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My code that formats certain rows on my pivot table works when I step through the code, step by step. However, when I assigned the macro to a button, with other macros, it didn't complete the formatting in the finished product. As a result, when I copied the pivot table data across to a new sheet, the correct formatting wasn't present.



Basically I am trying to format certain rows in the pivot table, and then copy the values and format of the pivot table to a new sheet and format the rest there (as formatting the pivot table is slowing down my macro considerably).



Here is the start of my code:



Sub formatpt()
'formatpt Macro

Dim ws As Worksheet
Set ws = Worksheets("Pivot")
Dim pt As PivotTable
Set pt = Worksheets("Pivot").PivotTables("Dashboard")

pt.ShowTableStyleRowHeaders = False
pt.ShowDrillIndicators = False

pt.PivotSelect "Region[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(128, 128, 128)
Selection.RowHeight = 40
Selection.Font.Bold = True
Selection.Font.Color = vbWhite

pt.PivotSelect "Desk[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(166, 166, 166)
Selection.RowHeight = 35
Selection.Font.Bold = True

pt.PivotSelect "Style[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(217, 217, 217)
Selection.RowHeight = 20
Selection.Font.Bold = True

Set ws2 = Worksheets("Final")

ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


The row heights don't copy across either, but this is less important. But if you do know how to copy across row height as well as the formatting and values, then please let me know.



Any help is greatly appreciated.



Thanks










share|improve this question













My code that formats certain rows on my pivot table works when I step through the code, step by step. However, when I assigned the macro to a button, with other macros, it didn't complete the formatting in the finished product. As a result, when I copied the pivot table data across to a new sheet, the correct formatting wasn't present.



Basically I am trying to format certain rows in the pivot table, and then copy the values and format of the pivot table to a new sheet and format the rest there (as formatting the pivot table is slowing down my macro considerably).



Here is the start of my code:



Sub formatpt()
'formatpt Macro

Dim ws As Worksheet
Set ws = Worksheets("Pivot")
Dim pt As PivotTable
Set pt = Worksheets("Pivot").PivotTables("Dashboard")

pt.ShowTableStyleRowHeaders = False
pt.ShowDrillIndicators = False

pt.PivotSelect "Region[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(128, 128, 128)
Selection.RowHeight = 40
Selection.Font.Bold = True
Selection.Font.Color = vbWhite

pt.PivotSelect "Desk[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(166, 166, 166)
Selection.RowHeight = 35
Selection.Font.Bold = True

pt.PivotSelect "Style[All]", xlLabelOnly + _
xlFirstRow, True
Selection.Interior.Color = RGB(217, 217, 217)
Selection.RowHeight = 20
Selection.Font.Bold = True

Set ws2 = Worksheets("Final")

ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ws.Range("A1").CurrentRegion.Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


The row heights don't copy across either, but this is less important. But if you do know how to copy across row height as well as the formatting and values, then please let me know.



Any help is greatly appreciated.



Thanks







excel vba excel-vba formatting pivot-table






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 16:37









H Smith

1




1








  • 1




    Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
    – Comintern
    Nov 22 at 16:43










  • Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
    – H Smith
    Nov 22 at 16:48












  • Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
    – H Smith
    Nov 22 at 17:42














  • 1




    Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
    – Comintern
    Nov 22 at 16:43










  • Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
    – H Smith
    Nov 22 at 16:48












  • Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
    – H Smith
    Nov 22 at 17:42








1




1




Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
– Comintern
Nov 22 at 16:43




Is anything selected when you hit the button? I'd give How to avoid using Select in Excel VBA a read.
– Comintern
Nov 22 at 16:43












Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
– H Smith
Nov 22 at 16:48






Apart from the ".PivotSelect" syntax, no. I saw that article posted before and took it into consideration as a result. But I don't know how to avoid PivotSelect. Interestingly I just tested the macro in a new button, with just the "formatpt" macro assigned, and it worked? @Comintern
– H Smith
Nov 22 at 16:48














Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
– H Smith
Nov 22 at 17:42




Managed to solve it. Unfortunately had to just activate the sheet before the code. Not ideal but it works.
– H Smith
Nov 22 at 17:42

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435175%2fvba-pivot-table-code-working-when-stepped-through-but-not-when-assigned-to-a-ma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435175%2fvba-pivot-table-code-working-when-stepped-through-but-not-when-assigned-to-a-ma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista