Hi,
I have some code that is supposed to find the last row and column with data and clear the contents. Normally, this works, but there are circumstances when this code (see step 3) does not clear the contents. I've tried two methods, one that uses a named range and another that creates a range based on the furthest row,column. Both do not work sometimes. I watch the commands being built in the debugger and all the variables are getting set correctly. The command is executed without a run time error, but the cells do not clear. I use Excel 2010, any suggestions would be appreciated.
I have some code that is supposed to find the last row and column with data and clear the contents. Normally, this works, but there are circumstances when this code (see step 3) does not clear the contents. I've tried two methods, one that uses a named range and another that creates a range based on the furthest row,column. Both do not work sometimes. I watch the commands being built in the debugger and all the variables are getting set correctly. The command is executed without a run time error, but the cells do not clear. I use Excel 2010, any suggestions would be appreciated.
Code:
Sub ClearSave(targetSheet As String, Optional ColumnToSearch As String = "C")
'This code will clear the data in the sheet and prepare for new saved data.
'Steps:
'1. Determine which sheet is being requested to be cleared
'2. Adjust the named range to clear all populated rows
'3. Remove the values
'Declare Variables Needed
Dim RangeToClear As String
Dim LastRowWithData As Long
Dim LastColumnWithData As Long
Dim TargetWorksheet As Worksheet
Set TargetWorksheet = Sheets(targetSheet)
'==================
' Step 1
'==================
'Determine which tab was requested to be cleared
'Set the respective range name to be used to clear the data
Select Case targetSheet
Case "workingProjectionsDB"
RangeToClear = "tabClear_WP"
Case "PGworkingProjectionsDB"
RangeToClear = "tabClear_PGWP"
Case "What-If-01"
RangeToClear = "tabClear_WI01"
Case "PGWhat-If-01"
RangeToClear = "tabClear_PGWI01"
Case "What-If-02"
RangeToClear = "tabClear_WI02"
Case "PGWhat-If-02"
RangeToClear = "tabClear_PGWI02"
Case "importDataDB"
RangeToClear = "tabClear_Actual"
Case "Original"
RangeToClear = "tabClear_Orig"
Case "PGOriginal"
RangeToClear = "tabClear_PGOrig"
Case "CP-Commit"
RangeToClear = "tabClear_CP"
Case "PGCP-Commit"
RangeToClear = "tabClear_PGCP"
Case "LP-Commit"
RangeToClear = "tabClear_LP"
Case "PGLP-Commit"
RangeToClear = "tabClear_PGLP"
Case "LLP-Commit"
RangeToClear = "tabClear_LLP"
Case "PGLLP-Commit"
RangeToClear = "tabClear_PGLLP"
Case "pgDataDB"
RangeToClear = "tabClear_PGData"
Case "allCCDataDB"
RangeToClear = "tabClear_AllCC"
Case "mergedCCsDB"
RangeToClear = "tabClear_Merged"
Case "userDefaultsDB"
RangeToClear = "tabClear_UserDef"
Case Else 'No Tab, or invalid tab indicated
'Add Error Trap Here
End Select
'==================
' Step 2
'==================
'Adjust the range name to cover the entire data set
'First check to see if there is any data
If Range(RangeToClear).Value <> "" Then 'No data in first row. Skip the clear.
LastRowWithData = TargetWorksheet.Cells.Find("*", searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumnWithData = TargetWorksheet.Cells.Find("*", searchorder:=xlByRows, SearchDirection:=xlPrevious).Column
Else
LastRowWithData = 1
LastColumnWithData = 1
End If
'==================
' Step 3
'==================
'Clear the data
'Research: Sometimes these clear commands don't work.
If LastRowWithData > 1 Then 'there's data. If value = 1 there's no data, only a header
Range(RangeToClear).Resize(LastRowWithData, LastColumnWithData).Clear
Range(TargetWorksheet.Cells(2, 1), TargetWorksheet.Cells(LastRowWithData, LastColumnWithData)).Clear
End If
End Sub