PDA

View Full Version : Solved: Simple Selection Code Question. The readable version.



LutonBarry
05-31-2013, 03:41 AM
Apologies for the earlier post.

I've noted on here that folk have said that many Select/Selection requests that the rcorder puts into code are unneccesary. Can I ask experienced folk to review the attached code that I recorded to use to clear or delete a number of spreadheets with differing ranges of information leaving just the the top row of the sheets in place.



End Sub
Sub ClearOldData()
'
' ClearOldData Macro
'

'
Application.DisplayAlerts = False
Sheets("Import").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("All").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Open").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Closed").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("New").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Today").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Yesterday").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Raw Open WIP data").Select
ActiveWindow.SelectedSheets.Delete
Range("A2").Select
Application.DisplayAlerts = True
End Sub

mdmackillop
05-31-2013, 04:08 AM
Something like this
Sub CleanUp()
Dim arr, a
Dim LR As Long
arr = Array("Sheet1", "Sheet2", "Sheet3")
For Each a In arr
With Sheets(a)
LR = .Cells.Find("*", after:=.Cells(1, 1), searchdirection:=xlPrevious).Row
.Rows("2:" & LR).Delete
End With
Next
End Sub

snb
05-31-2013, 05:41 AM
or

Sub M_snb()
Set sn = Sheets("sheet1").UsedRange.Offset(1)
sn.ClearContents
Sheets.FillAcrossSheets sn, 2
End Sub

LutonBarry
05-31-2013, 06:35 AM
Thanks both for your help code fixed now and much shorter than before. How you folks know all this is beyond me but I'm very grateful to you .

snb
05-31-2013, 09:14 AM
The main secret lies in the use of F1 in the VBEditor.

SamT
05-31-2013, 10:50 AM
Original:
End Sub
Sub ClearOldData()
'
' ClearOldData Macro
'

'
Application.DisplayAlerts = False
Sheets("Import").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("All").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Open").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Closed").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("New").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Today").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Yesterday").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Raw Open WIP data").Select
ActiveWindow.SelectedSheets.Delete
Range("A2").Select
Application.DisplayAlerts = True
End Sub
With Selections Removed
End Sub
Sub ClearOldData()
'
' ClearOldData Macro
'

'
Application.DisplayAlerts = False
Sheets("Import").Rows("2:2").End(xlDown)).Delete ' Default: Not needed; Shift:=xlUp
Sheets("All").Delete
Sheets("Open").Rows("2:2").End(xlDown)).Delete
Sheets("Closed").Rows("2:2").End(xlDown)).Delete
'.
'.
'.
Sheets("Raw Open WIP data").Delete
Application.DisplayAlerts = True
End Sub