welshgasman
Well-known Member
- Joined
- May 25, 2013
- Messages
- 1,402
- Office Version
- 2019
- 2007
- Platform
- Windows
Hi all,
I have a set of sheets, that I am trying to make an address book of.
In sheet A, the UsedRange is showing as K1000 (using Ctrl + End), yet I only have 10 rows of data and up to column J.
THis means the mailmerge produces 125 pages of labels when only 2 are needed.
I searched here and found code below, but this did not reset it.
How can I reset the used range with VBA please.
Whilst sheet A is giving me problems now, there might well be others, so I would like to reset each sheet before I process it.
TIA
I have a set of sheets, that I am trying to make an address book of.
In sheet A, the UsedRange is showing as K1000 (using Ctrl + End), yet I only have 10 rows of data and up to column J.
THis means the mailmerge produces 125 pages of labels when only 2 are needed.
I searched here and found code below, but this did not reset it.
How can I reset the used range with VBA please.
Whilst sheet A is giving me problems now, there might well be others, so I would like to reset each sheet before I process it.
TIA
VBA Code:
Sub DeleteUnused()
Dim myLastRow As Long
Dim myLastCol As Long
Dim dummyRng As Range
Dim AnyMerged As Variant
'http://www.contextures.on.ca/xlfaqApp.html#Unused
'Helps to reset the usedrange by deleting rows and columns AFTER your true used range
'Check for merged cells
AnyMerged = ActiveSheet.UsedRange.MergeCells
If AnyMerged = True Or IsNull(AnyMerged) Then
MsgBox "There are merged cells on this sheet." & vbCrLf & _
"The macro will not work with merged cells.", vbOKOnly + vbCritical, "Macro will be Stopped"
Exit Sub
End If
With ActiveSheet
myLastRow = 0
myLastCol = 0
Set dummyRng = .UsedRange
On Error Resume Next
myLastRow = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByRows).Row
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
On Error GoTo 0
If myLastRow * myLastCol = 0 Then
.Columns.Delete
Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete
End If
End With
End Sub