rgottesman
New Member
- Joined
- Jul 23, 2014
- Messages
- 6
I am having trouble with my VBA code it appears that I need to run the code twice in order to have the code process all of the data. The code should first refresh all pivot tables then copy and paste the data from pivot table 1 to a worksheet. After pasting the data, all negative values need to be moved with their absolute value to the next cell. I need to run the code twice, the first time all of the pivot tables are refreshed and the data is copied but the negative numbers are not moved until the second time running the code, also the negative numbers that are moved are then rounded, which they should not be. Thank you in adavance.
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit;">Sub pivotrefresh()'' pivotrefresh Macro'**********************************Store Variables****************************Dim PivotCurrent As WorksheetDim PivotAccrual As WorksheetDim NumDaysPivot As WorksheetDim TotalAccrual As WorksheetDim journal As WorksheetDim pt1 As PivotTableDim pt2 As PivotTableDim pt3 As PivotTableDim pt4 As PivotTableDim ptacct As PivotFieldDim ptitem As PivotItemDim currentmonth As RangeDim JE_Db2Cr As RangeDim cell As RangeDim AccrualAcct As RangeDim inversecell As LongDim Lastrow As Long'**********************************Set Variables****************************'Titled worksheetsSet PivotCurrent = Sheets("PIVOT Current")Set PivotAccrual = Sheets("Pivot Accrual")Set NumDaysPivot = Sheets("#of DAYS PIVOT")Set TotalAccrual = Sheets("TOTAL Accrual")Set journal = Sheets("Journal")Set TotalAccrual = Sheets("Total Accrual")'Titled pivot tables in all of the workbookSet pt1 = PivotCurrent.PivotTables("pivotcurrent")Set pt2 = PivotAccrual.PivotTables("pivotaccrual")Set pt3 = NumDaysPivot.PivotTables("numberdays")Set pt4 = TotalAccrual.PivotTables("accrual")'Titled pivot field in pivot currentSet ptacct = pt1.PivotFields("G/L ACCOUNT NUMBER")'Titled named rangesSet currentmonth = Range("currentmonth")Set JE_Db2Cr = Range("JE_Db2Cr")Set AccrualAcct = Range("AccrualAcct")'**********************************Refresh Pivottables**************************** 'cleared extraneous left over data pt1.PivotCache.MissingItemsLimit = xlMissingItemsNone pt2.PivotCache.MissingItemsLimit = xlMissingItemsNone pt3.PivotCache.MissingItemsLimit = xlMissingItemsNone pt4.PivotCache.MissingItemsLimit = xlMissingItemsNone'refreshed all pivot tables in workbook in order pt1.RefreshTable pt2.RefreshTable pt3.RefreshTable pt4.RefreshTable '**********************************Copy data from pt1 to Journal tab**************************** For Each ptitem In ptacct.PivotItems If Len(ptitem.Name) > 2 And IsNumeric(ptitem.Name) Then ptacct.PivotItems(ptitem.Name).Visible = True Else ptacct.PivotItems(ptitem.Name).Visible = False End If Next ptitem '**********************************Posting Current Month JE**************************** currentmonth.Copy Destination:=journal.Range("a2") For Each cell In JE_Db2Cr If cell < 0 Then inversecell = Abs(cell.Value) cell.ClearContents cell.Offset(0, 1) = inversecell End If Next cell '**********************************Posting Current Month Accrual**************************** </code></pre>


<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit;">Sub pivotrefresh()'' pivotrefresh Macro'**********************************Store Variables****************************Dim PivotCurrent As WorksheetDim PivotAccrual As WorksheetDim NumDaysPivot As WorksheetDim TotalAccrual As WorksheetDim journal As WorksheetDim pt1 As PivotTableDim pt2 As PivotTableDim pt3 As PivotTableDim pt4 As PivotTableDim ptacct As PivotFieldDim ptitem As PivotItemDim currentmonth As RangeDim JE_Db2Cr As RangeDim cell As RangeDim AccrualAcct As RangeDim inversecell As LongDim Lastrow As Long'**********************************Set Variables****************************'Titled worksheetsSet PivotCurrent = Sheets("PIVOT Current")Set PivotAccrual = Sheets("Pivot Accrual")Set NumDaysPivot = Sheets("#of DAYS PIVOT")Set TotalAccrual = Sheets("TOTAL Accrual")Set journal = Sheets("Journal")Set TotalAccrual = Sheets("Total Accrual")'Titled pivot tables in all of the workbookSet pt1 = PivotCurrent.PivotTables("pivotcurrent")Set pt2 = PivotAccrual.PivotTables("pivotaccrual")Set pt3 = NumDaysPivot.PivotTables("numberdays")Set pt4 = TotalAccrual.PivotTables("accrual")'Titled pivot field in pivot currentSet ptacct = pt1.PivotFields("G/L ACCOUNT NUMBER")'Titled named rangesSet currentmonth = Range("currentmonth")Set JE_Db2Cr = Range("JE_Db2Cr")Set AccrualAcct = Range("AccrualAcct")'**********************************Refresh Pivottables**************************** 'cleared extraneous left over data pt1.PivotCache.MissingItemsLimit = xlMissingItemsNone pt2.PivotCache.MissingItemsLimit = xlMissingItemsNone pt3.PivotCache.MissingItemsLimit = xlMissingItemsNone pt4.PivotCache.MissingItemsLimit = xlMissingItemsNone'refreshed all pivot tables in workbook in order pt1.RefreshTable pt2.RefreshTable pt3.RefreshTable pt4.RefreshTable '**********************************Copy data from pt1 to Journal tab**************************** For Each ptitem In ptacct.PivotItems If Len(ptitem.Name) > 2 And IsNumeric(ptitem.Name) Then ptacct.PivotItems(ptitem.Name).Visible = True Else ptacct.PivotItems(ptitem.Name).Visible = False End If Next ptitem '**********************************Posting Current Month JE**************************** currentmonth.Copy Destination:=journal.Range("a2") For Each cell In JE_Db2Cr If cell < 0 Then inversecell = Abs(cell.Value) cell.ClearContents cell.Offset(0, 1) = inversecell End If Next cell '**********************************Posting Current Month Accrual**************************** </code></pre>