I created an Excel spreadsheet that allows users to input data. Then it takes formulas from a seperate tab, and pastes them in to the data set tab. It then sorts all of them in a specific order which is needed for the process it is used for.
I have tried to increase the efficiency by turning off automatic calcs and only calculating when I need to, then pasting values after that. I have also turned off screen updating, status bar, and enable events. I think these are some of the low hanging fruit.
It works pretty well for smaller datasets (say 25K rows and smaller) but it can really get bogged down with larger ones. I have one in particular that is 48K rows and it often will just end up freezing my Excel.
Any spots jumping out that could be altered to increase efficiency?
--
Worksheets("Update Indicator").Visible = True
Sheets("Update Indicator").Range("B1") = 1
Dim LossSort As Workbook
Dim WC As Worksheet
Dim WC_Form As Worksheet
Dim lastRow As Long
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
Set LossSort = ThisWorkbook
Set WC = LossSort.Sheets("WC Losses")
Set WC_Form = LossSort.Sheets("WC Formulas")
If WC.AutoFilterMode Then
WC.AutoFilterMode = False
End If
lastRow = WC.Range("V" & Rows.Count).End(xlUp).Row
'Code to help speed up macro
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayStatusBar = False
.EnableEvents = False
End With
Calculate
'Test if the value in cell U2 is blank/empty
If IsEmpty(WC.Range("W2").Value) = True Then
MsgBox "No WC Losses Available"
Exit Sub
Else
End If
'Set Original Order
WC.Range("BH2") = 1
WC.Range("BH2:BH" & lastRow).DataSeries , xlDataSeriesLinear
'Copy formulas from WC Formulas tab to WC Losses
WC_Form.Range("A2:U2").Copy Destination:=WC.Range("A2:U" & lastRow)
WC_Form.Range("AX2:BG2").Copy Destination:=WC.Range("AX2:BG" & lastRow)
'Calculate
WC.Calculate
'Apply formatting across the dataset
WC_Form.Range("L2:AM2").Copy
WC.Range("L2:O" & lastRow).PasteSpecial Paste:=xlPasteFormats
'Sort by Acc Desc and Claim# then by Closed No Pay, Loss Date, State
With Sheets("WC Losses").Cells(1, 1).CurrentRegion.Cells
.Sort Key1:=Range("Z1"), Order1:=xlAscending, _
Key2:=Range("V1"), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
With Sheets("WC Losses").Cells(1, 1).CurrentRegion.Cells
.Sort Key1:=Range("K1"), Order1:=xlAscending, _
Key2:=Range("W1"), Order2:=xlAscending, _
Key3:=Range("X1"), Order3:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
'Paste values over formulas
WC.Calculate
Dim rng1 As Range
Set rng1 = WC.Range("A2:T" & lastRow)
WC.Range("A2").Resize(rng1.Rows.Count, rng1.Columns.Count).Cells.Value = rng1.Cells.Value
Dim rng2 As Range
Set rng2 = WC.Range("AX2:BG" & lastRow)
WC.Range("AX2").Resize(rng2.Rows.Count, rng2.Columns.Count).Cells.Value = rng2.Cells.Value
Sheets("Update Indicator").Range("C5").Copy Destination:=Sheets("Update Indicator").Range("B5")
Sheets("Update Indicator").Calculate
Sheets("Update Indicator").Range("B5").Copy
Sheets("Update Indicator").Range("B5").PasteSpecial Paste:=xlPasteValues
Worksheets("Update Indicator").Visible = False
finish:
ActiveWorkbook.RefreshAll
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayStatusBar = True
.EnableEvents = True
End With
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
I would start by formatting your data as a Table and allowing it to manage your formulas. That way you avoid all the VBA you're using to copy and paste your formulas.
You could also try using Power Query to handle the formulas and sorting and avoid most of the VBA you're using.
Also, if you're going to turn off calc, screenupdating, events etc., you've got to have error handling to make sure that is your code fails, they all get reset back to their defaults. Otherwise, you can end with Excel in a REALLY bad state.
Related
I have a workbook with many pivot tables that are based on a range that is deleted and refreshed using VBA. Currently, to update the data source, as the last part of that sub routine, I recreate PivotCaches for each PivotTable and refresh.
I want just 6 of the tables to be linked together with one common PivotCache so they can share slicers etc and I cannot get it working.
I have looked all over for a solution to this but I keep coming up short and I have been stuck on it for a fair while now. I am reasonably new to VBA but I can usually work it out with trial and error but this one just has me stumped.
I obviously have done something wrong and any help to identify what, would be greatly appreciated.
Thanks :)
My current code is below (Note: I removed the non-related stuff from the code for ease of reading):
Sub RunReport()
On Error GoTo ErrorHandler
'############## Define Variables ##############
Dim WS_O As Worksheet 'Output sheet - report sheet
Dim WS_P As Worksheet 'Pivot table Sheet
Dim OuputRow As Integer 'First row for output of data
Dim LastRow_O As Integer 'Last used row of output sheet
Dim PivotCacheName As PivotCache
Dim PivotRange As String 'Range of data for Pivot Data Source
Dim PivotName1 As String 'Pivot Table Name Strings
Dim PivotName2 As String
Dim PivotName3 As String
Dim PivotName4 As String
Dim PivotName5 As String
Dim PivotName6 As String
'############## Modify Application Settings ##############
'Store current configuration
OriginalCalcMode = Application.Calculation
'Set configuration for fastest processing
Application.Calculation = xlManual
Application.ScreenUpdating = False
Application.EnableEvents = False
'############## Set Variable Values ##############
'Worksheets
Set WS_O = Sheets("Report") 'Output sheet - report sheet
Set WS_P = Sheets("Pivot Tables - Live Data") 'Pivot tables sheet
'Pivot Tables
PivotName1 = "PivotTable1"
PivotName2 = "PivotTable2"
PivotName3 = "PivotTable3"
PivotName4 = "PivotTable4"
PivotName5 = "PivotTable5"
PivotName6 = "PivotTable6"
'General
OutputRow = 7
'Used Ranges
LastRow_O = WS_O.Range("A" & Rows.Count).End(xlUp).Row
'############## Refresh Pivot Tables ##############
'Define Data Range
PivotRange = WS_O.Name & "!" & "A" & OutputRow - 1 & ":AM" & LastRow_O
'Error Handling
'Make sure every column in data set has a heading and is not blank
If WorksheetFunction.CountBlank(WS_O.Range("A" & OutputRow - 1 & ":AM" & LastRow_O).Rows(1)) > 0 Then
MsgBox "One or more columns in ''Report'' sheet has a blank heading;" & vbNewLine _
& "This has prevented the pivot tables from refreshing correctly." & vbNewLine & vbNewLine _
& "Please verify cells A" & OutputRow - 1 & ":AM" & OutputRow - 1 & " in ''Report'' sheet are not blank and try again.", vbCritical, "ERROR - Column Heading Missing"
GoTo EndSub
End If
'Change Pivot Data Sources to a single cache
Set PivotCacheName = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange, Version:=xlPivotTableVersion15)
WS_P.PivotTables(PivotName1).ChangePivotCache (PivotCacheName)
WS_P.PivotTables(PivotName2).ChangePivotCache (PivotCacheName)
WS_P.PivotTables(PivotName3).ChangePivotCache (PivotCacheName)
WS_P.PivotTables(PivotName4).ChangePivotCache (PivotCacheName)
WS_P.PivotTables(PivotName5).ChangePivotCache (PivotCacheName)
WS_P.PivotTables(PivotName6).ChangePivotCache (PivotCacheName)
'Refresh Pivot Tables
'Turn on auto calc while pivot's update
Application.Calculation = xlCalculationAutomatic
WS_P.PivotTables(PivotName1).RefreshTable
WS_P.PivotTables(PivotName2).RefreshTable
WS_P.PivotTables(PivotName3).RefreshTable
WS_P.PivotTables(PivotName4).RefreshTable
WS_P.PivotTables(PivotName5).RefreshTable
WS_P.PivotTables(PivotName6).RefreshTable
'Completion Confirmation
MsgBox "Report data has been compiled and pivot tables have been successfully refreshed.", vbInformation, "SUCCESS! - Report Compilation Complete"
'############## End Sub and Reset Application Configuration ##############
'Standard End Sub Functionality (where no undocumented error occurred)
EndSub:
Application.ScreenUpdating = False
Application.Calculation = OriginalCalcMode 'Reset calc option to what it was previously
Application.EnableEvents = True
Exit Sub
'Error Handling (where an undocumented error occurred - that is, an error without an explainatory message box)
ErrorHandler:
Application.ScreenUpdating = False
' Application.Calculation = OriginalCalcMode
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
MsgBox "An error caused this subroutine to stop working correctly." & vbNewLine _
& "Contact Administrator for assistance.", vbCritical, "ERROR - Contact Administrator"
End Sub
I was previously using this and still am for some tables I don't want using the same PivotCache:
'Create new caches for each table
WS_P.PivotTables(PivotName1).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
WS_P.PivotTables(PivotName2).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
WS_P.PivotTables(PivotName3).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
WS_P.PivotTables(PivotName4).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
WS_P.PivotTables(PivotName5).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
WS_P.PivotTables(PivotName6).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
I'm currently getting the Runtime 438 Error (Object doesn't support this property or method) here >>>
WS_P.PivotTables(PivotName1).ChangePivotCache (PivotCacheName)
EDIT: I have found a solution and added an answer below.
So, after a good nights sleep and several more hours of research, I have what I believe to be a solution.
It's probably not the best way, but it works and hasn't yet caused me any issues...
'Create New Pivot Cache from Data Range
WS_P.PivotTables(PivotName1).ChangePivotCache _
ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
'Link Other Pivot Tables to same Pivot Cache
WS_P.PivotTables(PivotName2).CacheIndex = WS_P.PivotTables(PivotName1).CacheIndex
WS_P.PivotTables(PivotName3).CacheIndex = WS_P.PivotTables(PivotName1).CacheIndex
WS_P.PivotTables(PivotName4).CacheIndex = WS_P.PivotTables(PivotName1).CacheIndex
WS_P.PivotTables(PivotName5).CacheIndex = WS_P.PivotTables(PivotName1).CacheIndex
WS_P.PivotTables(PivotName6).CacheIndex = WS_P.PivotTables(PivotName1).CacheIndex
I'm still not sure why the other way wasn't working so would still appreciate some feedback, if anyone has any as this method does prohibit me attaching slicers to all pivot tables (I have to leave the first one out).
I have ran into an automation issue that I cannot seem to figure out.
Currently, I have a worksheet,("Project") that contains data in columns "A"(Project Name) & "B"(Project File Location).
Column "B" contains the string location of each MS Project file.
My VBA macro loops through column "B" and opens each MS Project file and copies a task with the .SelectTaskField method and then copies it back into column "E" of the worksheet.
The first 2 projects loop through without any issues, however, on the 3rd project, I receive the Run-time error '1004': An unexpected error occurred with the method.
I co-worker and I have poured through the code and the MS Project Files to see if there are any differences in the data and we cannot find any differences.
Below is a copy of the code that I have been using.
Just wanted to see if anyone else has had similar issues. I have found that MS Project does not like to be manipulated like Excel or Word.
Any help would be greatly appreciated.
Sub Test()
Dim ws As Worksheet
Set ws = Worksheets("Projects")
Dim lrow As Long
lrow = Range("B" & Rows.Count).End(xlUp).Row
'Turns off updates and alerts
Application.ScreenUpdating = True
Application.DisplayAlerts = True
'Select Daily Field Reports and clear worksheet
ws.Range("E2:E" & lrow).ClearContents
'Opens MS Project
Set objproject = CreateObject("MSProject.Project")
'This keeps MS Project invisible. If you want to see it, change to "True"
objproject.Application.Visible = True
Dim oproject As Range
'This cycles through the range and gathers the data for each project
For Each oproject In Range("B2:B" & lrow)
Set objproject = CreateObject("MSProject.Project")
oproject.Select
objproject.Application.FileOpen Selection
objproject.Application.Visible = True
objproject.Application.SelectTaskField Row:=1, Column:="Percent Complete", RowRelative:=False 'The column name must match. This is the only issue that I have uncovered.
objproject.Application.EditCopy
ws.Select
Dim lastrow As Long
lastrow = ws.Cells(Rows.Count, "E").End(xlUp).Row + 1
Dim Rng As Range
Set Rng = ws.Range("E" & lastrow)
'Rng.PasteSpecial xlPasteFormats
Rng.PasteSpecial xlPasteValues
objproject.Application.Quit
Next oproject
'Turns updates and alerts back on
Application.ScreenUpdating = True
Application.DisplayAlerts = True
'Closes MS Project
objproject.Application.Quit
End Sub
Using the SelectTaskField method presumes the file was saved in a task view and that the column you want is in the table of the view. Better to get the values you need directly from the Task object.
It appears you are looking for the % Complete value from the first task. In that case use this:
objproject.ActiveProject.Tasks(1).PercentComplete
Here's how it could work in your code. I took the liberty of simplifying it a bit:
Sub Test()
Dim ws As Worksheet
Set ws = Worksheets("Projects")
Dim lrow As Long
lrow = Range("B" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = True
Application.DisplayAlerts = True
ws.Range("E2:E" & lrow).ClearContents
Dim objproject As MSProject.Application
Set objproject = CreateObject("MSProject.Application")
objproject.Application.Visible = True
Dim oproject As Range
For Each oproject In Range("B2:B" & lrow)
objproject.FileOpen Name:=oproject.Value, ReadOnly:=True
oproject.Offset(, 3) = objproject.ActiveProject.Tasks(1).PercentComplete
objproject.FileCloseEx
Next oproject
Application.ScreenUpdating = True
Application.DisplayAlerts = True
objproject.Quit
End Sub
Note that it is more straight-forward to get a reference to the application object rather than a child of that object: CreateObject("MSProject.Application") is preferable to CreateObject("MSProject.Project").
I have this working code which copies data in specific columns to a new file.
Sub GetFileCopyData()
Dim Fname As String
Dim SrcWbk As Workbook
Dim DestWbk As Workbook
Set DestWbk = ThisWorkbook
Application.Calculation = xlManual
Application.ScreenUpdating = False
Sheets("Data").UsedRange.ClearContents
Fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv*), *.csv*", Title:="Select a File")
If Fname = "False" Then Exit Sub
Set SrcWbk = Workbooks.Open(Fname)
SrcWbk.Sheets(1).Range("A:A").Copy DestWbk.Sheets("Data").Range("A:A")
SrcWbk.Sheets(1).Range("E:E").Copy DestWbk.Sheets("Data").Range("B:B")
SrcWbk.Sheets(1).Range("M:M").Copy DestWbk.Sheets("Data").Range("C:C")
SrcWbk.Sheets(1).Range("AD:AD").Copy DestWbk.Sheets("Data").Range("D:D")
SrcWbk.Sheets(1).Range("AF:AF").Copy DestWbk.Sheets("Data").Range("E:E")
SrcWbk.Sheets(1).Range("DA:DA").Copy DestWbk.Sheets("Data").Range("F:F")
SrcWbk.Sheets(1).Range("AEG:AEG").Copy DestWbk.Sheets("Data").Range("G:G")
SrcWbk.Sheets(1).Range("AEM:AEM").Copy DestWbk.Sheets("Data").Range("H:H")
SrcWbk.Close False
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
This runs extremely slowly. I've already tried turning screen updating off etc. I read that the following is quicker than copying, which is slow.
Range("A1:Z100").value = Range("A101:Z200").value
Can anyone please tell me how to implement this? I tried using this code but it ended up being blank:
SrcWbk.Sheets(1).Range("A:A").Value = DestWbk.Sheets("Data").Range("A:A").Value
If all you're copying is values, rather than copying the entire column, which is very resource intensive (effectively you're copying 1048576 cells) you could try implementing a lastrow statement and only copy the used range of the column. This could drastically reduce your runtime depending on how many values you have. Something amongst the lines of:
Sub copy()
Dim lastr As Long
lastr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Sheet2.Range("A1:A" & lastr).Value = Sheet1.Range("A1:A" & lastr).Value
End Sub
To adapt your code you should replace the following line:
SrcWbk.Sheets(1).Range("A:A").Copy DestWbk.Sheets("Data").Range("A:A")
With this:
lastr = SrcWbk.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
DestWbk.Sheets("Data").Range("A" & lastr).value = SrcWbk.Sheets(1).Range("A" & lastr).value
Please note if your column lengths vary, you should redo the lastr calculation for every column. If all your columns are the same length (All ending on the same row), then using the first calculation for every column will do.
It looks like your requirement is just extracting specific columns from CSV file, then Get & Transform should be the best fit rather than VBA solution.
Yet another option is to use Microsoft Text Driver via ADO in VBA.
I am editing an existing template in VBA. This template organises data from a "Raw Data" sheet into a "Day", "Evening" and "Night" sheet. I want to create a sheet called "Weekend", where data from "Raw Data" will be copied to if the date is not a weekday (i know how to do that bit).
However, the amount of data that runs through this template is massive, so to avoid creating a weekend sheet where the user does not need/want one I want to put in a section of code where if a tick box is ticked (True), a sheet called "Weekend" will be created (within the workbook, but inbetween existing sheets - namely inbetween a sheet called "Night" and "Graph - All Data") and when it is unticked (False) this sheet will not exist.
I thought about having this sheet to exist all the time, and to have it hidden when the tick box is unticked, however this means that the data would be still piled into it and in the interest of efficiency I would rather not have it like that.
Here is the Code I am trying to alter
Sub ToggleWindDirection()
Dim i As Long
Application.ScreenUpdating = False
If sheetArr(1) Is Nothing And LastNDRow = Empty Then
DefineLists
End If
Sheets("Raw Data").Unprotect Password:="2260"
For Each sht In sheetArr
sht.Unprotect Password:="2260"
Next
Set chtAllData = ActiveWorkbook.Charts("Graph - All Data")
With Sheets("Raw Data")
If .Range("O15").Value = True Then
'Wind direction is being used
.Range("C17:G17").Font.ColorIndex = xlAutomatic
.Range("D17").Font.ColorIndex = 9
.Range("G17").Font.ColorIndex = 9
.Range("D17").Locked = False
.Range("G17").Locked = False
.Range("F" & FirstNDRow & ":F10000").Interior.Pattern = xlNone
.Range("F" & FirstNDRow & ":F10000").Interior.PatternTintAndShade = 0
.Range("F" & FirstNDRow & ":F10000").Font.ColorIndex = xlAutomatic
Else
'Not using wind direction
.Range("C17:G17").Font.ColorIndex = 16
.Range("D17").Locked = True
.Range("G17").Locked = True
.Range("F" & FirstNDRow & ":F10000").Interior.Pattern = xlSolid
.Range("F" & FirstNDRow & ":F10000").Interior.TintAndShade = -4.99893185216834E-02
.Range("F" & FirstNDRow & ":F10000").Font.ColorIndex = 16
End If
'Addition by lewisthegruffalo 2016
Dim ws As Worksheet
If .Range("O21").Value = True Then
'create the weekend sheet
Set ws = Worksheets.Add(After:=Worksheets("Night"))
ws.Name = "Weekend"
Else
'No Weekend needed
Worksheets("Weekend").Delete
End If
End With
Sheets("Raw Data").Activate
Application.ScreenUpdating = True
End Sub
Any help would be greatly apprichiated.
Kind Regards,
lewisthegruffalo
If you want to create a new sheet using sheets.add then you can utilise the arguments in it to tell it where to put it, the msdn page has a good example at the bottom.
What you have so far? Where do you wanna call it? What checkbox shall be ticked? Please provide some code.
A general answer is this to add a new worksheet:
Dim ws As Worksheet
If Something Then
Set ws = Worksheets.Add(Before:=Worksheets("Graph - All Data"))
ws.Name = "Weekend"
'do anything you want with the ws object
End if
In the interest of clarity, I've wiped the original question and am re-posting.
Scenario:
Source workbook has multiple pages, front page of book has a query/extract function to create a new book with some pre-entered data using a template from one of the sheets in the source book.
Requirements:
Phase 1: Extract function needs to set all rows beyond row 6 as hidden where the data in column A = HC.
First (and so far working) draft of that code as follows:
Sub Extract()
Dim wbkOriginal As Workbook
Set wbkOriginal = ActiveWorkbook
'sets site name and site ID into the estate page to be extracted
Worksheets(Sheet11.CmbSheet.Value).Range("B3").Value = Worksheets("front page").Range("E6")
Worksheets(Sheet11.CmbSheet.Value).Range("D3").Value = Worksheets("front page").Range("N6")
Worksheets(Sheet11.CmbSheet.Value).Range("F3").Value = Worksheets("front page").Range("K6")
'hiding all rows that being with HC apart from row 6 which is the starting row
'code to be added to the individual estate sheets to unhide each row after status column filled
'on a row by row basis - as the hiding is for HC rows only, the section headers will remain visible
'may have to code around that on the sheet itself
BeginRow = 7
EndRow = 300
ChkCol = 1
For RowCnt = BeginRow To EndRow
If Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).Value Like "HC" Then
Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
' copies sheet name from combo box into new document, saves it with site name,
' site id and current date into user profile desktop folder for ease of access
' with new HEAT, worth investigating if sheet can be saved directly to a call ID folder?
With ActiveWorkbook.Sheets(Sheet11.CmbSheet.Value)
.Copy
ActiveWorkbook.SaveAs _
"C:\temp\" _
& .Cells(3, 2).Text _
& " " _
& Format(Now(), "DD-MM-YY") _
& ".xlsm", _
xlOpenXMLWorkbookMacroEnabled, , , , False
End With
'code to close the original workbook to prevent accidental changes etc
Application.DisplayAlerts = False
wbkOriginal.Close
Application.DisplayAlerts = True
End Sub
Phase 2: Each row that starts with HC, has a drop down in column E. That drop down has 3 options, 'Complete' 'Incomplete' and 'Not Required'
Task: When the user selects and clicks on an entry, the sheet needs to do the following
Unhide the next row
Enter the current windows username into column I
Enter the current time into column J
Prototype code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ChangedCell As Object
Dim lRow As Long
For Each ChangedCell In Target
If ChangedCell.Column = 5 And ChangedCell <> "" Then
lRow = ChangedCell.Row + 1
lRow.Hidden = False
Cells(lRow, 8) = Environ("USERNAME")
Cells(lRow, 9) = "HH:MM"
End If
Next
End Sub
Problem:
Compile error: Invalid Qualifier, referring to the lRow.Hidden = False line,
Tried to declare it as an object instead, thinking that would allow me to spec it that way instead, but no joy to be had.
As ever, any guidance from the community would be greatly appreciated.
Many thanks.
Rob.
Sub Extract()
Dim wbkOriginal As Workbook
Set wbkOriginal = ActiveWorkbook
'sets site name and site ID into the estate page to be extracted
Worksheets(Sheet11.CmbSheet.Value).Range("B3").Value = Worksheets("front page").Range("E6")
Worksheets(Sheet11.CmbSheet.Value).Range("D3").Value = Worksheets("front page").Range("N6")
Worksheets(Sheet11.CmbSheet.Value).Range("F3").Value = Worksheets("front page").Range("K6")
'hiding all rows that being with HC apart from row 6 which is the starting row
'code to be added to the individual estate sheets to unhide each row after status column filled
'on a row by row basis - as the hiding is for HC rows only, the section headers will remain visible
'may have to code around that on the sheet itself
BeginRow = 7
EndRow = 300
ChkCol = 1
For RowCnt = BeginRow To EndRow
If Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).Value <> "" Then
Worksheets(Sheet11.CmbSheet.Value).Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
' copies sheet name from combo box into new document, saves it with site name,
' site id and current date into user profile desktop folder for ease of access
' with new HEAT, worth investigating if sheet can be saved directly to a call ID folder?
With ActiveWorkbook.Sheets(Sheet11.CmbSheet.Value)
.Copy
ActiveWorkbook.SaveAs _
"C:\temp\" _
& .Cells(3, 2).Text _
& " " _
& Format(Now(), "DD-MM-YY") _
& ".xlsm", _
xlOpenXMLWorkbookMacroEnabled, , , , False
End With
'code to close the original workbook to prevent accidental changes etc
Application.DisplayAlerts = False
wbkOriginal.Close
Application.DisplayAlerts = True
End Sub