'initialize dates
start_dates
Sheets("menu").Range("E14:F14").Interior.Color = 65535
Application.DisplayAlerts = False
Sheets("cap").Select
finalrow = Cells(Rows.Count, "R").End(xlUp).Row
Range("a1:r" & finalrow).Clear
Path = "\\a\b\c\" & p_show_Year & p_show_Month & p_show_Day & "\"
fname = "cap_" & p_show_Year & p_show_Month & p_show_Day & ".csv"
Set cap_file = Workbooks.Open(Path & fname)
finalrow = Cells(Rows.Count, "R").End(xlUp).Row
Range("a1:r" & finalrow).Copy
ThisWorkbook.Sheets("cap").Activate
Range("a1").PasteSpecial xlPasteValues
Range("a1").Select
'close
cap_file.Close False
Sheets("menu").Select
ActiveWorkbook.Save
Code:'initialize dates start_dates Sheets("menu").Range("E14:F14").Interior.Color = 65535 Application.DisplayAlerts = False Sheets("cap").Select finalrow = Cells(Rows.Count, "R").End(xlUp).Row Range("a1:r" & finalrow).Clear Path = "\\a\b\c\" & p_show_Year & p_show_Month & p_show_Day & "\" fname = "cap_" & p_show_Year & p_show_Month & p_show_Day & ".csv" Set cap_file = Workbooks.Open(Path & fname) finalrow = Cells(Rows.Count, "R").End(xlUp).Row Range("a1:r" & finalrow).Copy ThisWorkbook.Sheets("cap").Activate Range("a1").PasteSpecial xlPasteValues Range("a1").Select 'close cap_file.Close False Sheets("menu").Select ActiveWorkbook.Save
Public p_show_Day As Variant
Public p_show_Day_short As Variant
Public p_show_Month As Variant
Public p_show_month_short As Variant
Public p_show_month_long As Variant
Public p_show_Year As Variant
Public Sub start_dates()
d1 = Sheets("menu").Range("T_1").Value
p_show_Day_short = format(d1, "D")
p_show_Day = format(d1, "DD")
p_show_month_short = format(d1, "M")
p_show_Month = format(d1, "MM")
p_show_Month_mid = format(d1, "MMM")
p_show_month_long = format(d1, "MMMM")
p_show_year_short = format(d1, "YY")
p_show_Year = format(d1, "YYYY")
Public Sub start_dates()
[I][COLOR=#006400]'declared variables already used in your code ...[/COLOR][/I]
Dim Path As String, fName As String, cap_file As Workbook
Dim finalrow As Long
[COLOR=#006400]'variables added by me[/COLOR]
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Sheets("cap")
Dim ws2 As Worksheet
[COLOR=#006400] wb[/COLOR].Sheets("menu").Range("E14:F14").Interior.Color = 65535
Application.DisplayAlerts = False
[COLOR=#006400]ws[/COLOR].Select 'no need to select if you use variables - [COLOR=#ff0000]DELETE this line[/COLOR]
finalrow = ws.Cells(Rows.Count, "R").End(xlUp).Row
[COLOR=#006400] ws[/COLOR].Range("a1:r" & finalrow).Clear
Path = "\\a\b\c\" & p_show_Year & p_show_Month & p_show_Day & "\"
fName = "cap_" & p_show_Year & p_show_Month & p_show_Day & ".csv"
Set cap_file = Workbooks.Open(Path & fName)
Set [COLOR=#006400]ws2[/COLOR] = cap_file.Sheets(1)
[I][COLOR=#006400]'which sheet in which workbook do these ranges refer to?? - I have guessed 1st sheet in cap_file[/COLOR][/I]
finalrow = ws2.Cells(Rows.Count, "R").End(xlUp).Row
ws.Range("a1:r" & finalrow).Copy
[COLOR=#006400]ws[/COLOR].Activate 'no need to activate if VBA knows what ws means - [COLOR=#ff0000]DELETE this line[/COLOR]
With [COLOR=#006400]ws[/COLOR]
.Range("a1").PasteSpecial xlPasteValues
.Range("a1").Select
End With
'close
cap_file.Close False
[COLOR=#006400] wb[/COLOR].Sheets("menu").Select
[COLOR=#006400] wb[/COLOR].Save
End Sub
Set cap_file = Workbooks.Open(Path & fname)
Activesheet.EnableCalculation = False
Activesheet.EnableCalculation = True
finalrow = Cells(Rows.Count, "R").End(xlUp).Row
This sounds very much like a timing issue to me, I have had this sort of probelm with EXCEL before.
What you could try is forcing a recalculation of the workbook when you open it ( after you have opened it) by putting this code in
Code:Set cap_file = Workbooks.Open(Path & fname) Activesheet.EnableCalculation = False Activesheet.EnableCalculation = True finalrow = Cells(Rows.Count, "R").End(xlUp).Row