UsedRange.Count counting wrong

后端 未结 3 1950
陌清茗
陌清茗 2020-12-18 05:43

Summary: I\'m taking a row of data from one sheet and pasting it into another, however the sheet would be a daily use kind of thing where new data is just e

相关标签:
3条回答
  • 2020-12-18 06:21

    This two line do the magic

      usedCol = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
      usedRow = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
    

    For more info visit Microsoft's site

    http://msdn.microsoft.com/en-us/library/office/ff196157.aspx

    0 讨论(0)
  • 2020-12-18 06:22

    Change: usedRows = Sheets("EFT").UsedRange.Count

    To: usedRows = Sheets("EFT").Range("A" & Sheets("EFT").Rows.Count).End(xlUp).Row

    Where "A" can be changed to whichever row you wish to count the total number of columns.

    There is a danger in using UsedRange because it factors in such things and formatted cells with no data and other things that can give you unexpected results, like if you are expecting your data to start in Range("A1"), but it really starts in another range!

    I will say, however, that If you really wish to use UsedRange, your code above is still wrong to get the rows. Use this instead UsedRange.Rows.Count or to get the last absolute cell of the UsedRange, use UsedRange.SpecialCells(xlCellTypeLastCell).Row

    0 讨论(0)
  • 2020-12-18 06:25

    Thanks for the discussion...

    .UsedRange.Rows.Count and .UsedRange.Columns.Count work fine provided there is something in cell A1. Otherwise need to use the SpecialCells solution.

    Hope this is helpful.

    0 讨论(0)
提交回复
热议问题