Excel resetting “UsedRange”

前端 未结 12 2065
醉酒成梦
醉酒成梦 2021-01-05 15:42

Don\'t know what I\'m missing, but the examples I see posted do not appear to work.

I import data from a web query. I set the query to clear unused cells when it re

相关标签:
12条回答
  • 2021-01-05 15:49

    I only needed to use Worksheets("Sheet1").UsedRange.Calculate after deleting rows to reset the range.

    0 讨论(0)
  • 2021-01-05 15:52

    This may or may not suit your data needs, but if your data is all in one contiguous block, you can use CurrentRegion instead of UsedRange, like this:

    With Cells(1, 1).CurrentRegion
     MsgBox "I have " & .Rows.Count & " rows and " & .Columns.Count & " columns of data."
    End With
    

    Of course, if the region you care about does not start at cell A1, or if your sheet contains multiple contiguous regions that you care about, this option will not work. Depending on how predictable your data is, you can usually find at least one cell in each block of data, and once you have that, CurrentRegion will give you the range of the entire block.

    0 讨论(0)
  • 2021-01-05 15:54
    1. select cell 1,1 in any sheets you want to reset the UsedRange property
    2. Calculate all worksheets in all open workbooks, regardless of whether they changed since last calculation (To Calculate Fully Ctrl+Alt+F9)
    3. Save the workbook

    Works for me on all versions of excel

    0 讨论(0)
  • 2021-01-05 15:54

    This worked for me:

    Worksheets("Sheet1").UsedRange.Clear
    Worksheets("Sheet1").UsedRange = ""
    

    It appears that inserting a value into the UsedRange resets it. After this action I can go

    MyCurrentRow = Worksheets("Sheet1").Range("A:A").SpecialCells(xlCellTypeLastCell).Row
    

    MyCurrentRow comes now back as 1, and I can just count from there. When I did not assign a value into UsedRange, that LastCell value did not reset. No Save required.

    0 讨论(0)
  • 2021-01-05 15:57

    I've used Jeeped solution and worked for me when i add .Activate, so:

    With Worksheets("Sheet1")
            Debug.Print .UsedRange.Address(0, 0)
            .UsedRange.Clear
            .UsedRange   
            .Activate
            Debug.Print .UsedRange.Address(0, 0)
      End With
    

    I'm using Excel2013

    0 讨论(0)
  • 2021-01-05 15:57

    This works for me in Excel 2010:

    Worksheets("Sheet1").UsedRange.Clear  
    Worksheets("Sheet1").UsedRange.Calculate
    
    0 讨论(0)
提交回复
热议问题