<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tim laqua dot com &#187; excel</title>
	<atom:link href="http://timlaqua.com/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Sun, 09 May 2010 15:25:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to add a calculated measure (calculated field) to an Excel 2007 PivotTable with a SSAS data source</title>
		<link>http://timlaqua.com/2008/11/how-to-add-a-calculated-measure-calculated-field-to-an-excel-2007-pivottable-with-a-ssas-data-source/</link>
		<comments>http://timlaqua.com/2008/11/how-to-add-a-calculated-measure-calculated-field-to-an-excel-2007-pivottable-with-a-ssas-data-source/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 18:28:12 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[How-to Guides]]></category>
		<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[calculated measure]]></category>
		<category><![CDATA[calculated member]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[excel 2007]]></category>
		<category><![CDATA[named set]]></category>
		<category><![CDATA[pivottable]]></category>
		<category><![CDATA[ssas]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=50</guid>
		<description><![CDATA[As it turns out, you can do it programatically as explained by Allan Folting here: Microsoft Excel: Common Questions Around Excel 2007 OLAP PivotTables: http://blogs.msdn.com/excel/archive/2008/02/05/common-questions-around-excel-2007-OLAP-PivotTables.aspx And the parts that I have to keep looking up (I use my blog as a notebook for things I don't want to forget : Sub AddCalculatedMeasure() Dim pvt As [...]]]></description>
			<content:encoded><![CDATA[<p>As it turns out, you can do it programatically as explained by <strong>Allan Folting </strong>here:<br />
<strong>Microsoft Excel: Common Questions Around Excel 2007 OLAP PivotTables</strong>:<br />
<a href="http://blogs.msdn.com/excel/archive/2008/02/05/common-questions-around-excel-2007-OLAP-PivotTables.aspx">http://blogs.msdn.com/excel/archive/2008/02/05/common-questions-around-excel-2007-OLAP-PivotTables.aspx</a></p>
<p>And the parts that I have to keep looking up (I use my blog as a notebook for things I don't want to forget <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> :</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Sub</span> AddCalculatedMeasure() 
	<span style="color: #000080;">Dim</span> pvt <span style="color: #000080;">As</span> PivotTable
	<span style="color: #000080;">Dim</span> strName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Dim</span> strFormula <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> 
&nbsp;
	<span style="color: #000080;">Set</span> pvt = Sheet1.PivotTables(&quot;PivotTable1&quot;)
	strName = &quot;[Measures].[Internet Sales Amount 25 %]&quot;
	strFormula = &quot;[Measures].[Internet Sales Amount]*1.25&quot;
	pvt.CalculatedMembers.Add Name:=strName, Formula:=strFormula, <span style="color: #000080;">Type</span>:=xlCalculatedMember 
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span> 
&nbsp;
<span style="color: #000080;">Sub</span> AddCalculatedMember() 
	<span style="color: #000080;">Dim</span> pvt <span style="color: #000080;">As</span> PivotTable
	<span style="color: #000080;">Dim</span> strName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Dim</span> strFormula <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> 
&nbsp;
	<span style="color: #000080;">Set</span> pvt = Sheet1.PivotTables(&quot;PivotTable1&quot;)
	strName = &quot;[Product].[Product Categories].[Bikes].[Mountain Bikes].[Mountain-100 Silver, 38 25 %]&quot;
	strFormula = &quot;[Product].[Product Categories].[Bikes].[Mountain Bikes].[Mountain-100 Silver, 38]*1.25&quot;
	pvt.CalculatedMembers.Add Name:=strName, Formula:=strFormula, <span style="color: #000080;">Type</span>:=xlCalculatedMember
	pvt.ViewCalculatedMembers = <span style="color: #000080;">True</span> 
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span> 
&nbsp;
<span style="color: #000080;">Sub</span> AddNamedSet() 
	<span style="color: #000080;">Dim</span> pvt <span style="color: #000080;">As</span> PivotTable
	<span style="color: #000080;">Dim</span> strName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Dim</span> strFormula <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Dim</span> cbf <span style="color: #000080;">As</span> CubeField 
&nbsp;
	<span style="color: #000080;">Set</span> pvt = Sheet1.PivotTables(&quot;PivotTable1&quot;)
	strName = &quot;[My Mountain Bikes]&quot;
	strFormula = &quot;[Product].[Product Categories].[Bikes].[Mountain Bikes].children&quot;
	pvt.CalculatedMembers.Add Name:=strName, Formula:=strFormula, <span style="color: #000080;">Type</span>:=xlCalculatedSet
	<span style="color: #000080;">Set</span> cbf = pvt.CubeFields.AddSet(Name:=&quot;[My Mountain Bikes]&quot;, Caption:=&quot;Mountain Bikes&quot;) 
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></pre></div></div>

<p>He also mentions that you can expose these members to Excel Services 2007 by creating the new objects and then removing the VBA code - very useful article.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2008/11/how-to-add-a-calculated-measure-calculated-field-to-an-excel-2007-pivottable-with-a-ssas-data-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing Server Names in Excel 2007 Embedded Workbook Connections</title>
		<link>http://timlaqua.com/2008/09/changing-server-names-in-excel-2007-embeded-workbook-connections/</link>
		<comments>http://timlaqua.com/2008/09/changing-server-names-in-excel-2007-embeded-workbook-connections/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 19:59:44 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[connections]]></category>
		<category><![CDATA[data sources]]></category>
		<category><![CDATA[embeded connections]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[excel 2007]]></category>
		<category><![CDATA[pivot table]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[ssas]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=48</guid>
		<description><![CDATA[Ok, so you want to migrate servers, eh? Well - all your business users pry have a couple thousand Pivot Reports lying around that point to the old server. We need to point all references to the old server at the new server. There are two places these references exist (afaik) - The My Data [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so you want to migrate servers, eh?  Well - all your business users pry have a couple thousand Pivot Reports lying around that point to the old server.  We need to point all references to the old server at the new server.</p>
<p>There are two places these references exist (afaik) - The My Data Sources folder (Windows XP / Excel 2007) which holds all of the odc objects for the logged in user, and the real pain - Embedded data sources in Excel Workbooks.</p>
<p>For the odc objects - you can just loop through the My Data Sources folder files and do a little find/replace action.</p>
<p>For the Excel files - we need to first find all the files, then loop through them and hunt for embedded Connections.  Once we find a connection, we can just blindly replace occurrences of the old server name with the new server name.  The first thing we need here is a function we can call recursively to go hunt down all the excel files - then we loop through and play with the connections (when found).<br />
<span id="more-48"></span></p>
<p>Let me know how it goes!  Here's the full version:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Set</span> regEx = <span style="color: #000080;">New</span> RegExp
regEx.IgnoreCase = <span style="color: #000080;">True</span>
regEx.Global = <span style="color: #000080;">True</span>
<span style="color: #000080;">Set</span> WshShell = Wscript.CreateObject(&quot;Wscript.Shell&quot;)
strMyDocsPath = WshShell.RegRead(&quot;HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal&quot;)
strDataSourceDir = strMyDocsPath &amp; &quot;\My Data Sources&quot;
&nbsp;
<span style="color: #008000;">'======================== Fix all the Data Sources
</span><span style="color: #000080;">Set</span> objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
<span style="color: #000080;">Set</span> Folder = objFSO.GetFolder(strDataSourceDir)
<span style="color: #000080;">Set</span> Files = Folder.Files
&nbsp;
<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> File <span style="color: #000080;">In</span> Files
	<span style="color: #000080;">If</span> LCase(Right(File.Name, 4)) = &quot;.odc&quot; <span style="color: #000080;">Then</span>
	    <span style="color: #000080;">Set</span> objReadFile = objFSO.OpenTextFile(File.Path, 1)
	    strContents = &quot;&quot;
&nbsp;
		<span style="color: #000080;">Do</span> <span style="color: #000080;">While</span> <span style="color: #000080;">Not</span> objReadFile.AtEndOfStream
			strContents = strContents &amp; objReadFile.ReadLine &amp; vbCrLf
		<span style="color: #000080;">Loop</span>
&nbsp;
	    objReadFile.<span style="color: #000080;">Close</span>
&nbsp;
		regEx.Pattern = &quot;(?=.*Data Source=OldCrappyServerName.*)(&lt;odc:ConnectionString&gt;.+&lt;/odc:ConnectionString&gt;)&quot;
&nbsp;
		<span style="color: #000080;">If</span> regEx.Test(strContents) <span style="color: #000080;">Then</span>
			<span style="color: #000080;">Set</span> colMatches = regEx.Execute(strContents)
&nbsp;
			<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> objMatch <span style="color: #000080;">in</span> colMatches
				strOldConnectionString = objMatch.SubMatches(0)
				regEx.Pattern = &quot;Data Source=OldCrappyServerName&quot;
				strNewConnectionString = regEx.Replace(strOldConnectionString, &quot;Data Source=NewAwesomeServerName&quot;)
				regEx.Pattern = strOldConnectionString
				strContents = regEx.Replace(strContents, strNewConnectionString)
			<span style="color: #000080;">Next</span>
&nbsp;
			<span style="color: #000080;">Set</span> objWriteFile = objFSO.CreateTextFile(File.Path, <span style="color: #000080;">True</span>)
			objWriteFile.Write strContents
			objWriteFile.<span style="color: #000080;">Close</span>
		<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
	<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">Next</span>
&nbsp;
&nbsp;
<span style="color: #008000;">'================================ Fix all the embeded connections (yikes)
</span>
<span style="color: #000080;">Set</span> arrFileList = CreateObject( &quot;System.Collections.ArrayList&quot; )
<span style="color: #000080;">Set</span> objExcel = CreateObject(&quot;Excel.Application&quot;)
objExcel.DisplayAlerts = <span style="color: #000080;">False</span>
&nbsp;
populateExcelFileList(strMyDocsPath)
&nbsp;
<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> strFileName <span style="color: #000080;">In</span> arrFileList
	objExcel.Workbooks.<span style="color: #000080;">Open</span>(strFileName)
	<span style="color: #000080;">Set</span> objWorkbook = objExcel.Workbooks(1)
	<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> objWorkbook <span style="color: #000080;">in</span> objExcel.Workbooks
		<span style="color: #000080;">If</span> objWorkbook.Connections.Count &gt; 0 <span style="color: #000080;">Then</span>
			WScript.Echo strFileName
			<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> objConnection <span style="color: #000080;">in</span> objWorkbook.Connections
				strOldConnection =  objConnection.OLEDBConnection.Connection
				regEx.Pattern = &quot;OldCrappyServerName&quot;
				strNewConnection = regEx.Replace(strOldConnection, &quot;NewAwesomeServerName&quot;)
				objConnection.OLEDBConnection.Connection = strNewConnection
			<span style="color: #000080;">Next</span>
&nbsp;
		<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
		objWorkbook.<span style="color: #000080;">Close</span> <span style="color: #000080;">True</span>
	<span style="color: #000080;">Next</span>
&nbsp;
	objExcel.Quit
<span style="color: #000080;">Next</span>
&nbsp;
<span style="color: #000080;">Function</span> populateExcelFileList(path)
	<span style="color: #000080;">Set</span> pathFolder = objFSO.GetFolder(path)
	<span style="color: #000080;">Set</span> pathFiles = pathFolder.Files
	<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> pathSubFolder <span style="color: #000080;">in</span> pathFolder.SubFolders
		populateExcelFileList(pathSubFolder.Path)
	<span style="color: #000080;">Next</span>
&nbsp;
	<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> pathFile <span style="color: #000080;">in</span> pathFiles
		<span style="color: #000080;">If</span> (LCase(Right(pathFile.Name, 5)) = &quot;.xlsx&quot; _
			<span style="color: #000080;">Or</span> LCase(Right(pathFile.Name, 4)) = &quot;.xls&quot;) _
			<span style="color: #000080;">And</span> Left(pathFile.Name, 1) &lt;&gt; &quot;~&quot; <span style="color: #000080;">Then</span>
			arrFileList.Add pathFile.Path
		<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
	<span style="color: #000080;">Next</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p><strong><em>Update</em></strong><br />
I added in support for .xls files - doing this against .xls files can (and usually will) break backwards compatibility for SSAS Pivot Tables.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2008/09/changing-server-names-in-excel-2007-embeded-workbook-connections/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
