Skip to content

Commit

Permalink
Handle idiosyncrasy of .UsedRange on empty sheet
Browse files Browse the repository at this point in the history
Instead of returning something relatively intuitive, like Nothing,
when .UsedRange is called on an empty Worksheet, Excel 2019 instead
returns Range("$A$1").

Thus, since the code tries to reduce the ExportRange to the bounds
of .UsedRange when entire rows and/or columns are selected, if
Selection encompasses $A$1 then it will erroneously set ExportRange
to Range("$A$1"), instead of Nothing.

This commit fixes this behavior by running an explicit check for
.UsedRange == Range("$A$1") and IsEmpty(.UsedRange). If the
sheet is empty, then ExportRange will be set to the appropriate
Nothing.
bskinn committed Feb 6, 2020
1 parent 08e466b commit a9aebac
Showing 2 changed files with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/UFExporter.frm
Original file line number Diff line number Diff line change
@@ -365,12 +365,33 @@ Private Sub setExportRange()
' be set to Nothing, which twigs the error-state check in
' setExportEnabled and setExportRangeText

Dim isSheetEmpty As Boolean

' Detect if the sheet is *actually* completely empty
' .UsedRange returns Range($A$1) rather than Nothing, if
' sheet is completely empty
With Selection.Parent
If .UsedRange.Address = "$A$1" And IsEmpty(.UsedRange) Then
isSheetEmpty = True
Else
isSheetEmpty = False
End If
End With

If Selection.Areas.Count <> 1 Then
Set ExportRange = Nothing
Else
' Handle if entire rows or columns (or both) are selected
If Selection.Address = Selection.EntireRow.Address Or _
Selection.Address = Selection.EntireColumn.Address Then
Set ExportRange = Intersect(Selection, Selection.Parent.UsedRange)
Selection.Address = Selection.EntireColumn.Address Then
If isSheetEmpty Then
' There's definitely no content to be intersected with
' the selection! This copes with the above "$A$1" return
' from .UsedRange.
Set ExportRange = Nothing
Else
Set ExportRange = Intersect(Selection, Selection.Parent.UsedRange)
End If
Else
Set ExportRange = Selection
End If
Binary file modified src/UFExporter.frx
Binary file not shown.

0 comments on commit a9aebac

Please sign in to comment.