VBA seems to be executing code on it's own, has me stumped!

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
I have one workbook that does some stuff, then opens another workbook, copies some stuff to it, updates some pivot tables, then saves and closes it.
The second workbook has 3 tabs, but only 1 visible (PivData)
When I run the code, everything seems to be working as it should, but then I go out to look at the finished product.
After the code has run, when I open the second worksheet (RYGmonthly), there are now two tabs visible (PivData) and (DOWNLOADS), with the latter selected.
I can't find what in the code is telling the second worksheet to unhide and select the (DOWNLOADS) tab.
This is a complete mystery to me.


Code:
[Sub DayOverDay()
Set RYG = ThisWorkbook
Set DLX = RYG.Sheets("DLX")
Set DOD = Workbooks.Open("\\RYG_Monthly.xlsx")
Set PIV = DOD.Sheets("PivData")
Set DWN = DOD.Sheets("DOWNLOADS")
Set RAW = DOD.Sheets("RawData")
RAW.Columns("A:C").ClearContents
LR8 = PIV.Cells(Rows.Count, 1).End(xlUp).Row
PIV.Range("A2:I" & LR8).ClearContents
LR5 = DLX.Cells(Rows.Count, 1).End(xlUp).Row
DLX.Range("A1:H" & LR5).Copy
RAW.Range("A1").PasteSpecial xlPasteAll
RAW.Columns("D:G").Delete
RAW.Columns("B").Delete
LR6 = RAW.Cells(Rows.Count, 1).End(xlUp).Row
RAW.Range("A1:C" & LR6).Copy
LR7 = DWN.Cells(Rows.Count, 1).End(xlUp).Row
DWN.Range("A" & LR7 + 1).PasteSpecial xlPasteAll
DWN.Columns("A:C").RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
LR7 = DWN.Cells(Rows.Count, 1).End(xlUp).Row
DWN.Range("A2:C" & LR7).Copy
PIV.Range("A2").PasteSpecial xlPasteAll
LR8 = PIV.Cells(Rows.Count, 1).End(xlUp).Row
PIV.Range("D2:D" & LR8).FormulaR1C1 = "=INT(RC[-2])"
PIV.Range("E2:E" & LR8).FormulaR1C1 = "=TEXT(RC[-3],""mmmm"")"
PIV.Range("F2:F" & LR8).FormulaR1C1 = "=TEXT(RC[-4],""dddd"")"
PIV.Range("G2:G" & LR8).FormulaR1C1 = "=WEEKNUM(RC[-5])"
PIV.Range("H2:H" & LR8).FormulaR1C1 = "=RC[-6]-RC[-4]"
PIV.Range("I2:I" & LR8).FormulaR1C1 = "=HOUR(RC[-1])"
PIV.PivotTables("PivotTable3").PivotCache.Refresh
PIV.PivotTables("PivotTable3").PivotFields("Date").ShowDetail = False
PIV.PivotTables("PivotTable3").PivotFields("Week").ShowDetail = False
PIV.PivotTables("PivotTable3").PivotFields("Month").ShowDetail = False
DOD.Save
DOD.Close
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
if you open the VBA editor, and step through the code with F8 key on windows or Ctrl+Shift+I on Mac, you should be able to see which line of code the occurrence happens.

I suspect it may be; DWN.Range("A" & LR7 + 1).PasteSpecial xlPasteAll

hope this helps

Coops
 
Upvote 0
do you have any code stored in the worksheet change events or anything?
 
Upvote 0
just a thought, could you not just add two lines of code to
a. hide the (DOWNLOADS) tab
b. select the (PivData) tab that you do want to see.

I know its not perfect but it would solve the issue surely?
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top