hi Kostas
I can't see from your code how this all ends up in the "Consol'" workbook (maybe it's in the called macro of "application.run..."?).
Anyway, I've had a go at modifying the below code to run a little smoother by removing selections & declaring variables etc but haven't tested it at all - so hopfully it works...
I have prefixed my comments with "###".
Option Explicit
Sub LTSB()
Dim FileToOpen As String
Dim NewlyOpenedFile As Workbook
Dim ShtToModify As Worksheet
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
'### add the full file path to the front of this string (eg "C:\Documents and Settings\HP_Owner\Desktop\"
FileToOpen = "Finished Lines\Final Versions\workbook1.xls"
Workbooks.Open Filename:=FileToOpen
Set NewlyOpenedFile = ActiveWorkbook
Set ShtToModify = ActiveSheet
' Set Date
With ShtToModify
.Range("M5").FormulaR1C1 = "='[consoleworkbook.xls]Sheet1'!R6C6"
.Range("M6").FormulaR1C1 = "='[consoleworkbook.xls]Sheet1'!R7C6"
.Range("M7").Select
End With
Application.Run _
"workbook1.xls!Update_Dwell_Time_Data"
Sheets("sheet1").Name = "League Board"
'### I've made an assumption here about which is the right sheet _
(w/o knowing what the "update_Dwell_Time_Data" macro is doing & I think my assumption is wrong!
With ShtToModify.Range(Cells(1, 1), LastCell(ShtToModify))
.Copy
NewlyOpenedFile.Activate
'### what file should this below sheet be added into?
Worksheets.Add().Name = "League Board"
With Range("A1")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End With
'Sheets("League Board").Copy After:=Workbooks("workbook1.xls").Sheets(1)
'Cells.Select
'Range("F1").Activate
'Selection.Copy
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
'Range("N3").Select
'ActiveSheet.Shapes("Picture 3").Select
'Selection.Delete
'### if this is closed this way it appears that nothing gets saved?????????????
Windows("workbook1.xls").Close
Sheets("Sheet1").Select
'free memory
Set NewlyOpenedFile = Nothing
Set ShtToModify = Nothing
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub ClearData()
'Now the macro tha runs on the opened workbook (and does the calculations)
'is pretty big. I'll put here first the 2 main calculation modules and if that's not enough information I'll put the main body (grab data from sql) as well.
'create and delete worksheet
Dim SettingsSheet As String
' Dim sh As Worksheet, flg As Boolean
Dim f As Long, i As Long
Dim delsheets As String
Dim setupsheets As Worksheet
SettingsSheet = "Settings"
' Stop messages popping up requiring you to click on yes/no/delete etc.
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
' Delete all sheets starting with "Data_" if they exist
'### can this be changed from...
'For Each sh In Worksheets
'If sh.Name Like "Data_*" Then flg = True: Exit For
'Next
'If flg = True Then
'For f = 1 To 50
'delsheets = "Data_" & Worksheets(SettingsSheet).Cells(16 + f - 1, 10)
'Worksheets(delsheets).Delete
'Next f
'Else
'End If
'### to be...
For f = 1 To 50
delsheets = "Data_" & Worksheets(SettingsSheet).Cells(15 + f, 10).Value
On Error Resume Next 'in case the sheet doesn't exist
Worksheets(delsheets).Delete
On Error GoTo 0
Next f
' Create the worksheets
For i = 1 To 50
Worksheets.Add(After:=Worksheets("Data")).Name = "Data_" & Worksheets(SettingsSheet).Cells(15 + i, 10)
' Add column headers to the worksheets
Set setupsheets = ActiveWorkbook.Worksheets("Data_" & Worksheets(SettingsSheet).Cells(15 + i, 10))
'### why paste special when it seems that you are pasting everything & then delete all but the headers?
' Sheets("Data").Range("A1:H1000").Copy
' Sheets(setupsheets).Select
' Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
' Range("A2:H1000").Clear
' Columns("A:H").EntireColumn.AutoFit
'### this may be quicker...
With setupsheets.Range("a1:h1")
.Value = Sheets("Data").Range("A1:H1").Value
''###if formatting is needed you can uncomment the following 2 lines of code
' Sheets("Data").Range("A1:H1000").Copy
' .Resize(1000, .Columns.Count).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
.EntireColumn.AutoFit
End With
Next i
' free memory etc
Set setupsheets = Nothing
With Application
.DisplayAlerts = False
.ScreenUpdating = True
End With
End Sub
Sub ConstructLeagueBoard()
'and this one does claculations (array formulas)
Dim SettingsSheet As String
Dim datasheet As String
Dim i As Long
Dim LastRow As Long
Worksheets("League Board").Select
SettingsSheet = "Settings"
For i = 1 To 50
datasheet = "Data_" & Worksheets(SettingsSheet).Cells(15 + i, 10).Value
'### referencing 65 thousand rows in an array is likely to slow your computer down...
'###can you resize the reference accurately?
'###eg instead of...
'LastRow = 65500
'### can it be...
LastRow = LastCell(Worksheets("league Board")).Row
With Worksheets("league Board")
.Range("D" & (i + 10)).FormulaArray = "=AVERAGE(QUARTILE(IF(('" & datasheet & "'!K2:K" & LastRow & ">0),'" & datasheet & "'!D2:D" & LastRow & "),2),QUARTILE(IF(('" & datasheet & "'!K2:K" & LastRow & ">0),'" & datasheet & "'!D2:D" & LastRow & "),3))"
.Range("K" & (i + 10)).FormulaArray = "=COUNT(IF('" & datasheet & "'!K2:K" & LastRow & ">0,'" & datasheet & "'!K2:K" & LastRow & "))"
End With
Next i
End Sub
Function LastCell(ws As Worksheet) As Range
Dim LastRow As Long
Dim LastCol As Long
' Error-handling is here in case there is not any data in the worksheet
On Error Resume Next
With ws
' Find the last real row
LastRow = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
' Find the last real column
LastCol = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
' Finally, initialize a Range object variable for the last populated row.
Set LastCell = ws.Cells(LastRow, LastCol)
End Function
Even if this code doesn't work in its entirety it should give you plenty of ideas on how to optimise your working code - Goodluck!
hth
Rob
Bookmarks