From a9aebac26d253b6ca80123c4425bf06ec75308a5 Mon Sep 17 00:00:00 2001 From: Brian Skinn Date: Thu, 6 Feb 2020 14:24:43 -0500 Subject: [PATCH] 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. --- src/UFExporter.frm | 25 +++++++++++++++++++++++-- src/UFExporter.frx | Bin 4632 -> 4632 bytes 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/UFExporter.frm b/src/UFExporter.frm index 2b8565b..ee32b1f 100644 --- a/src/UFExporter.frm +++ b/src/UFExporter.frm @@ -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 diff --git a/src/UFExporter.frx b/src/UFExporter.frx index 3441a8c8a8d2f980659cf32c670b98a4c9baf460..b392f93db96c86d0f61449487b7f607a80f2b11f 100644 GIT binary patch delta 394 zcmX|6ze@sP7=G`&)9z01{85pVUg8-TrI9qw~3n^xt3Rt-KTpdqL6LMjr2$Dx#VAS};{ zpk+mXTMn-OepD|8;fmUd#p6BsSjKvv8 zqT`qPUrmv>_sudjKs#b}K6n-r#cR*9%V0Iv@kNf{(9>kDbA-9Hy^B0^!M?W`baqUQ z-Pp2L-Lff0l^Cju8$Z3Pc%n4O@ga_s6fw)FtLM(uQ13(^ delta 401 zcmbQCGDBrU3k%zaMt$4;w>EdNJYi=1G&!F`RlK}d!IgnYfbxMs