{"id":691,"date":"2013-07-20T23:44:42","date_gmt":"2013-07-20T23:44:42","guid":{"rendered":"http:\/\/regina-whipp.com\/blog\/?p=691"},"modified":"2013-11-08T18:43:03","modified_gmt":"2013-11-08T23:43:03","slug":"excel-manipulation-from-access","status":"publish","type":"post","link":"https:\/\/regina-whipp.com\/blog\/?p=691","title":{"rendered":"Excel Manipulation from Access&#8230;"},"content":{"rendered":"<p><span style=\"color: #000000;\"><span style=\"font-size: medium;\">Sometimes, I have to send data out to Excel.\u00a0 Every once in a while I have to add a little something to that data&#8230; row count, specific Worksheet, a formula, etc.\u00a0 For those times when you find you need to do the same, here\u2019s what I\u2019ve used&#8230;<\/span><\/span><span style=\"color: #000000; font-size: medium;\">\u00a0<\/span><\/p>\n<p><span style=\"color: #000000; font-size: medium;\">I always use Late Binding, highlighted in green.\u00a0 For more on Late Binding see, <\/span><a href=\"http:\/\/www.granite.ab.ca\/access\/latebinding.htm\"><span style=\"color: #800080; font-size: medium;\">http:\/\/www.granite.ab.ca\/access\/latebinding.htm<\/span><\/a><span style=\"font-size: medium;\"><span style=\"color: #000000;\">\u00a0 by Tony an Access MVP<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><b>Dim ApXL As Object<br \/>\n<\/b><\/span><span style=\"color: #000000;\">Dim xlWBk As Object<br \/>\n<\/span><span style=\"color: #000000;\">Dim xlWSh As Object<\/span><\/p>\n<p><span style=\"color: #000000;\">Const xlUp As Long = -4162<br \/>\n<\/span><span style=\"color: #000000;\">Const xlDown As Long = -4121<\/span><\/p>\n<p><span style=\"color: #008000;\"><b>Set ApXL = CreateObject(&#8220;Excel.Application&#8221;)<\/b><\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Add Row Numbers&#8230;<br \/>\n<\/span><\/span><\/b><span style=\"color: #000000;\">To accomplish this task you must be sure to count a column that has data. \u00a0In my case it was Column J.<\/span><\/p>\n<p><span style=\"color: #000000;\">With xlWSh<br \/>\n<\/span><span style=\"color: #000000;\">xlWSh.Range(&#8220;A2:A&#8221; &amp; .Cells(.rows.Count, &#8220;J&#8221;).End(xlUp).Row).Formula = <b>&#8220;=IF(OR(ROW()=1,ISTEXT(INDIRECT(ADDRESS(ROW()-1,COLUMN())))),1,INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1)&#8221;<br \/>\n<\/b><\/span><span style=\"color: #000000;\">End With<\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Add Filters to your Columns&#8230;<\/span><\/span><\/b><\/p>\n<p>\u00a0<span style=\"color: #000000;\">xlWSh.Cells.rows(1).<b>AutoFilter<\/b><\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Auto Size the Columns to Fit the Data&#8230;<br \/>\n<\/span><\/span><\/b><span style=\"color: #000000;\">This is <\/span><span style=\"color: #ff0000;\"><b>not<\/b> <\/span><span style=\"color: #000000;\">advised on a Field that has been defined as a Memo data type in Access!<\/span><\/p>\n<p><span style=\"color: #000000;\">xlWSh.Cells.rows(1).EntireColumn.<b>AutoFit<\/b><\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Remove Prompt to Save Report&#8230;<\/span><\/span><\/b><\/p>\n<p><b><span style=\"color: #000000;\">ApXL.DisplayAlerts = False<br \/>\n<\/span><\/b><span style=\"color: #000000;\">xlWBk.SaveAs &#8220;C:\\MyReports\\YourReport.xlsx&#8221;, 51<br \/>\n<\/span><b><span style=\"color: #000000;\">ApXL.DisplayAlerts = True<\/span><\/b><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Name Report based on a Form Field Name&#8230;<\/span><\/span><\/b><\/p>\n<p>\u00a0<span style=\"color: #000000;\">xlWBk.SaveAs C:\\MyReports\\&#8221; &amp; <b>Me.txtReportName <\/b>&amp; &#8220;.xlsx&#8221;, 51<\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Add Total to a Column&#8230;<br \/>\n<\/span><\/span><\/b><span style=\"color: #000000;\">In the example provided it was Column H that I wanted to have a *Grand Total* on.\u00a0 (Tip: .End(xlUp) to get Last Row in a Column)<\/span><\/p>\n<p><span style=\"color: #000000;\">With xlWSh<br \/>\n<\/span><span style=\"color: #000000;\">xlWSh.Cells(.Rows.Count, 8).End(xlUp).Offset(1, 0).Formula = <b>&#8220;=SUM(H5:H&#8221; &amp; .Range(&#8220;H5&#8221;).End(xlDown).Row &amp; &#8220;)&#8221;<br \/>\n<\/b><\/span><span style=\"color: #000000;\">xlWSh.Cells(.Rows.Count, 8).End(xlUp).Select<br \/>\n<\/span><span style=\"color: #000000;\">End With<\/span><\/p>\n<p><span style=\"font-size: medium;\"><span style=\"color: #000000;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <b>&#8230;and Make Bold<br \/>\n<\/b><\/span><\/span><span style=\"color: #000000;\"><b><span style=\"font-size: medium;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/b>After I added a* Grand Total* to the Column I wanted to make it Bold.<\/span><\/p>\n<p>\u00a0<span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #000000;\"><span style=\"font-size: medium;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span>ApXL.Selection.Font.Bold = True<\/span><\/p>\n<p><b><span style=\"font-size: medium;\"><span style=\"color: #000000;\">Insert a Worksheet from another Workbook (and rename it)&#8230;<br \/>\n<\/span><\/span><\/b><span style=\"color: #000000;\">In this example I use frmExport with fields as specified below.<\/span><\/p>\n<p><span style=\"color: #000000;\">Dim xlapp As Object<br \/>\n<\/span><span style=\"color: #000000;\">Dim <b>xlWbkNew<\/b> As Object<br \/>\n<\/span><span style=\"color: #000000;\">Dim <b>xlWbkOld<\/b> As Object<br \/>\n<\/span><span style=\"color: #000000;\">Dim <\/span><span style=\"color: #0000ff;\"><b>strSheetName<\/b> <\/span><span style=\"color: #000000;\">As String<\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-size: medium;\">\u00a0\u00a0\u00a0 <\/span>Set <b>xlWbkNew<\/b> = xlapp.Workbooks.Open(Forms![frmExport]![txtExportPath] &amp; &#8220;\\&#8221; &amp; Forms![frmExport]![txtCombinedFileName])<br \/>\n<\/span><span style=\"color: #000000;\">\u00a0\u00a0\u00a0\u00a0 Set <b>xlWbkOld<\/b> = xlapp.Workbooks.Open(Forms![frmExport]![txtExportPath] &amp; &#8220;\\&#8221; &amp; Forms![frmExport]![txtNewFileName] &amp; &#8220;-&#8221; &amp; Forms![frmExport]![txtXXX] &amp; Forms![frmExport]![txtNewFileExtension])<\/span><\/p>\n<p>\u00a0<span style=\"color: #000000;\">\u00a0\u00a0\u00a0 <strong><span style=\"color: #0000ff;\">strSheetName<\/span> <\/strong>= Forms![frmExport]![txtWorksheetName]<\/span><\/p>\n<p>\u00a0<span style=\"color: #000000;\">\u00a0\u00a0\u00a0<b>xlWbkOld<\/b>.Worksheets(<\/span><span style=\"color: #0000ff;\"><b>strSheetName<\/b><\/span><span style=\"color: #000000;\">).Copy After:=<b>xlWbkNew<\/b>.Worksheets(xlWbkNew.Worksheets.Count)<br \/>\n<\/span><span style=\"color: #000000;\">\u00a0\u00a0 \u2018Rename the inserted Worksheet<br \/>\n<\/span><span style=\"color: #000000;\">\u00a0\u00a0\u00a0 <b>xlWbkNew<\/b>.Worksheets(<\/span><span style=\"color: #0000ff;\"><b>strSheetName<\/b><\/span><span style=\"color: #000000;\">).Name = Forms![frmExport]![txtNewFileName] &amp; &#8220;-&#8221; &amp; Forms![frmExport]![txtXXX]<\/span><\/p>\n<p>\u00a0<span style=\"color: #000000;\">\u00a0\u00a0 <strong>xlWbkOld<\/strong>.Close SaveChanges:=True<br \/>\n<\/span><span style=\"color: #000000;\">\u00a0\u00a0\u00a0 <strong>xlWbkNew<\/strong>.Close SaveChanges:=True<\/span><\/p>\n<h5><b>Excel Constants&#8230;<br \/>\n<\/b><\/h5>\n<p><strong>Declared from within the Procedure&#8230;<br \/>\n<\/strong>Const xlUp As Long = -4162<br \/>\nConst xlDown As Long = -4121<br \/>\nConst xlCenter As Long = -4108<br \/>\nConst xlBottom As Long = -4107<\/p>\n<h5>OR<\/h5>\n<p><strong>Declared in General section, at top of Module&#8230;<br \/>\n<\/strong>Private Const xlUP As Long = -4162<br \/>\nPrivate Const xlDown As Long = -4121<br \/>\nPrivate Const xlCenter As Long = -4108<br \/>\nPrivate Const xlBottom As Long = -4107<\/p>\n<p>For a complete list see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa221100(v=office.11).aspx\" target=\"_blank\">Microsoft Excel Constants<\/a>, while for Excel 2003, they have not changed.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_691\" class=\"pvc_stats all  \" data-element-id=\"691\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, I have to send data out to Excel. Every once in a while I have to add a little something to that data&#8230; row count, specific Worksheet, a formula, etc. For those times when you find you need to do the same, here\u2019s what I\u2019ve used&#8230; <\/p>\n<p>I always use Late Binding, highlighted in [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_691\" class=\"pvc_stats all  \" data-element-id=\"691\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[34,21],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-access-tips","tag-excel","tag-vba","odd"],"a3_pvc":{"activated":true,"total_views":2757,"today_views":0},"_links":{"self":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=691"}],"version-history":[{"count":12,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":860,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions\/860"}],"wp:attachment":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}