What does the Excel range.Rows property really do?

前端 未结 9 1839
故里飘歌
故里飘歌 2021-01-30 16:21

OK, I am finishing up an add-on project for a legacy Excel-VBA application, and I have once again run up against the conundrum of the mysterious range.Rows (?) and

相关标签:
9条回答
  • 2021-01-30 16:40

    I've found this works:

    Rows(CStr(iVar1) & ":" & CStr(iVar2)).Select
    
    0 讨论(0)
  • 2021-01-30 16:42

    Range.Rows and Range.Columns return essentially the same Range except for the fact that the new Range has a flag which indicates that it represents Rows or Columns. This is necessary for some Excel properties such as Range.Count and Range.Hidden and for some methods such as Range.AutoFit():

    • Range.Rows.Count returns the number of rows in Range.
    • Range.Columns.Count returns the number of columns in Range.
    • Range.Rows.AutoFit() autofits the rows in Range.
    • Range.Columns.AutoFit() autofits the columns in Range.

    You might find that Range.EntireRow and Range.EntireColumn are useful, although they still are not exactly what you are looking for. They return all possible columns for EntireRow and all possible rows for EntireColumn for the represented range.

    I know this because SpreadsheetGear for .NET comes with .NET APIs which are very similar to Excel's APIs. The SpreadsheetGear API comes with several strongly typed overloads to the IRange indexer including the one you probably wish Excel had:

    • IRange this[int row1, int column1, int row2, int column2];

    Disclaimer: I own SpreadsheetGear LLC

    0 讨论(0)
  • 2021-01-30 16:50

    It's perhaps a bit of a kludge, but the following code does what you seem to want to do:

    Set rng = wks.Range(wks.Rows(iStartRow), wks.Rows(iEndRow)).Rows
    
    0 讨论(0)
提交回复
热议问题