Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Personal.xls macro that writes to ThisWorkBook

I've writen a macro that only runs when my user needs to run the marco. Here
is what the marco does:
1) User opens a SAP report which needs to be formated.
2) User runs my macro to format the report
3) Macro also creats a PivotTable on worksheet "Discounts".
4) User might need to update one of the fields in Data worksheet and
if the user wants to have the PivotTable updated, they have to click on
the
PivotTable and then Refresh the data.

Here is what I would like to do:
I would like it so that when the macro runs, it would put some code in the
"ThisWorkbook" that will do the following :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim DisSheet, CurSheet
DisSheet = "Discounts"
CurSheet = ActiveCell.Worksheet.Name
If CurSheet = DisSheet Then GoTo 10
If CurSheet < DisSheet Then GoTo 20
10: Sheets("Discounts").Select
Range("A3").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache. Refresh
20:
End Sub

How do I do this by adding it to my Macro in my Personal.xls file?

Derek
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Personal.xls macro that writes to ThisWorkBook

I've writen a macro that only runs when my user needs to run the marco. Here
is what the marco does:
1) User opens a SAP report which needs to be formated.
2) User runs my macro to format the report
3) Macro also creats a PivotTable on worksheet "Discounts".
4) User might need to update one of the fields in Data worksheet and
if the user wants to have the PivotTable updated, they have to click on
the
PivotTable and then Refresh the data.

Here is what I would like to do:
I would like it so that when the macro runs, it would put some code in the
"ThisWorkbook" that will do the following :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim DisSheet, CurSheet
DisSheet = "Discounts"
CurSheet = ActiveCell.Worksheet.Name
If CurSheet = DisSheet Then GoTo 10
If CurSheet < DisSheet Then GoTo 20
10: Sheets("Discounts").Select
Range("A3").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache. Refresh
20:
End Sub

How do I do this by adding it to my Macro in my Personal.xls file?

I should also add that the SAP report file is going to be new each time so
it not like the same file will be used each time.

Derek

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Personal.xls macro that writes to ThisWorkBook

Chip Pearson shares some VBA techniques for writing code that writes code.
http://www.cpearson.com/excel/vbe.aspx

Derek Bliss wrote:

I've writen a macro that only runs when my user needs to run the marco. Here
is what the marco does:
1) User opens a SAP report which needs to be formated.
2) User runs my macro to format the report
3) Macro also creats a PivotTable on worksheet "Discounts".
4) User might need to update one of the fields in Data worksheet and
if the user wants to have the PivotTable updated, they have to click on
the
PivotTable and then Refresh the data.

Here is what I would like to do:
I would like it so that when the macro runs, it would put some code in the
"ThisWorkbook" that will do the following :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim DisSheet, CurSheet
DisSheet = "Discounts"
CurSheet = ActiveCell.Worksheet.Name
If CurSheet = DisSheet Then GoTo 10
If CurSheet < DisSheet Then GoTo 20
10: Sheets("Discounts").Select
Range("A3").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache. Refresh
20:
End Sub

How do I do this by adding it to my Macro in my Personal.xls file?

Derek


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Personal.xls macro that writes to ThisWorkBook

If anyone is wondering, this is what I added to my code:

' This adds the refresh to the "Discounts" tab.
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("ThisWorkBook")
Set CodeMod = VBComp.CodeModule

With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Private Sub Workbook_SheetActivate(ByVal Sh
As Object)"
LineNum = LineNum + 1
.InsertLines LineNum, "Dim DisSheet, CurSheet"
LineNum = LineNum + 1
.InsertLines LineNum, "DisSheet = " & DQUOTE & "Discounts" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "CurSheet = ActiveCell.Worksheet.Name"
LineNum = LineNum + 1
.InsertLines LineNum, "If CurSheet = DisSheet Then GoTo 10"
LineNum = LineNum + 1
.InsertLines LineNum, "If CurSheet < DisSheet Then GoTo 20"
LineNum = LineNum + 1
.InsertLines LineNum, "10: Sheets(" & DQUOTE & "Discounts" & DQUOTE
& ").Select"
LineNum = LineNum + 1
.InsertLines LineNum, "Range(" & DQUOTE & "A3" & DQUOTE & ").Select"
LineNum = LineNum + 1
.InsertLines LineNum, "ActiveSheet.PivotTables(" & DQUOTE &
"PivotTable1" & DQUOTE & ").PivotCache.Refresh"
LineNum = LineNum + 1
.InsertLines LineNum, "20:"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With

Thanks Dave

"Dave Peterson" wrote:

Chip Pearson shares some VBA techniques for writing code that writes code.
http://www.cpearson.com/excel/vbe.aspx

Derek Bliss wrote:

I've writen a macro that only runs when my user needs to run the marco. Here
is what the marco does:
1) User opens a SAP report which needs to be formated.
2) User runs my macro to format the report
3) Macro also creats a PivotTable on worksheet "Discounts".
4) User might need to update one of the fields in Data worksheet and
if the user wants to have the PivotTable updated, they have to click on
the
PivotTable and then Refresh the data.

Here is what I would like to do:
I would like it so that when the macro runs, it would put some code in the
"ThisWorkbook" that will do the following :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim DisSheet, CurSheet
DisSheet = "Discounts"
CurSheet = ActiveCell.Worksheet.Name
If CurSheet = DisSheet Then GoTo 10
If CurSheet < DisSheet Then GoTo 20
10: Sheets("Discounts").Select
Range("A3").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache. Refresh
20:
End Sub

How do I do this by adding it to my Macro in my Personal.xls file?

Derek


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ThisWorkbook of personal.xls Jack Sons Excel Discussion (Misc queries) 4 August 29th 07 04:28 PM
Creating a macro that writes to all worksheets ___Zoom[_3_] Excel Programming 4 August 4th 07 07:54 AM
Macro that writes Macros schoujar[_19_] Excel Programming 1 June 1st 06 04:59 AM
Personal macro workbook and personal.xls John Kilkenny Excel Discussion (Misc queries) 1 June 14th 05 09:43 PM


All times are GMT +1. The time now is 10:55 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"