Skip to content

Commit a9aebac

Browse files
committed
Handle idiosyncrasy of .UsedRange on empty sheet
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.
1 parent 08e466b commit a9aebac

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/UFExporter.frm

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,33 @@ Private Sub setExportRange()
365365
' be set to Nothing, which twigs the error-state check in
366366
' setExportEnabled and setExportRangeText
367367

368+
Dim isSheetEmpty As Boolean
369+
370+
' Detect if the sheet is *actually* completely empty
371+
' .UsedRange returns Range($A$1) rather than Nothing, if
372+
' sheet is completely empty
373+
With Selection.Parent
374+
If .UsedRange.Address = "$A$1" And IsEmpty(.UsedRange) Then
375+
isSheetEmpty = True
376+
Else
377+
isSheetEmpty = False
378+
End If
379+
End With
380+
368381
If Selection.Areas.Count <> 1 Then
369382
Set ExportRange = Nothing
370383
Else
384+
' Handle if entire rows or columns (or both) are selected
371385
If Selection.Address = Selection.EntireRow.Address Or _
372-
Selection.Address = Selection.EntireColumn.Address Then
373-
Set ExportRange = Intersect(Selection, Selection.Parent.UsedRange)
386+
Selection.Address = Selection.EntireColumn.Address Then
387+
If isSheetEmpty Then
388+
' There's definitely no content to be intersected with
389+
' the selection! This copes with the above "$A$1" return
390+
' from .UsedRange.
391+
Set ExportRange = Nothing
392+
Else
393+
Set ExportRange = Intersect(Selection, Selection.Parent.UsedRange)
394+
End If
374395
Else
375396
Set ExportRange = Selection
376397
End If

src/UFExporter.frx

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)