Original: Excel's Range object (C #)
The Range object is the most frequently used object in an Excel application, and you need to represent it as a Range object before you manipulate any area within Excel, and then use the methods and properties of the Range object. A Range object represents a cell, a row, a column, a selected cell that contains one or more cell blocks (which can be contiguous cells, or a non-contiguous cell), or even a group of cells on multiple worksheets. References a range in code. Address Property: This property returns a string of range coordinates, expressed in one of several formats, including: "$A $1"(cell in position A1)," "$1"(on the first row of the worksheet) and" $A $1: $C $5(the range includes all the cells in the rectangle between A1 and C5). "$" represents absolute coordinates (not relative coordinates). Excel.Worksheet ws= (Excel.Worksheet) thisworkbook.worksheets[1]; Excel.Range rng;rng=thisapplication.activecell;rng= Ws.get_range ("A1", Type.Missing);//use the Range property of an object to specify a rangeRNG = Ws.get_range ("a1:b12", Type.Missing);//use the Range property of an object to specify a rangeRNG = (excel.range) ws. cells[1,1];//Use the Cells property of a worksheet to specify individual row and column valuesRNG = Thisapplication.range ("Somerangename", Type.Missing);//referencing a named rangerng= ws. Range ("A1","C5")//the Cells, Rows, or Columns properties of a direct reference range function the sameRNG = ws. Range ("A1","C5"). Cellsrng= ws. Range ("A1","C5"). Rowsrng= ws. Range ("A1","C5"). Columnsrng= (Excel.Range) ws. rows[1, Type.Missing];//referencing a specific row or range of specific columns or rows and columnsRNG = (excel.range) ws. rows["1:3", Type.missing];rng= (Excel.Range) ws. columns[3, Type.Missing];//The Selection property of the Application object returns the range corresponding to the selected cellRNG =(Excel.Range) thisapplication.selection;//Create a range that contains two other merge ranges (specify two ranges within quotation marks, separated by commas)RNG = Thisapplication.get_range ("A1:d4, F2:g5", Type.Missing);//same effect as after three rowsrng1 = Thisapplication.get_range ("A1","D4"); Rng2= Thisapplication.get_range ("F2","G5"); RNG=thisapplication.union (rng1, Rng2, Type.Missing, Type.Missing, ...);//Create a range that refers to the overlapping portions of the other two ranges (specify two ranges within quotation marks, and do not use separators)RNG = Thisapplication.get_range ("a1:d16 B2:f14", Type.Missing);//same effect as after three rowsrng1 = Thisapplication.get_range ("A1","D16"); Rng2= Thisapplication.get_range ("B2","F14"); RNG=Thisapplication.intersect (rng1, rng2,type.missing, Type.Missing, Type.Missing, Type.Missing,......);//use the Offset property of the range to get an offset relative to the initial rangeRNG = (excel.range) ws. cells[1,1]; for(inti =1; I <=5; i++) {Rng.get_offset (I,0). Value2 =i.tostring ();}//Use the CurrentRegion property of the range to get a range representing the current region, which is bounded by the nearest empty row and column, which is the concatenated cellThisapplication.get_range ("C3", Type.Missing). CurrentRegion.Font.Bold =true;//use the Areas property of the range to get a collection of scopes, where each range corresponds to a region of the range contentrng= Thisapplication.get_range ("Test", Type.Missing); for(inti =1; I <= rng. Areas.count; i++) {MessageBox.Show (rng. Areas[i].get_address (Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing));}//Use the End property, and a XlDirection enumeration value (Xlup, xlToRight, xlToLeft, Xldown) to get a range that represents the end cell of the range (that is , the cell farthest from the selection in four directions)Excel.Range rngleft, Rngright, Rngup, Rngdown;rng=(excel.range) thisapplication.selection;rngright=rng.get_end (Excel.XlDirection.xlToRight); Rngleft=rng.get_end (Excel.XlDirection.xlToLeft); Rngup=rng.get_end (Excel.XlDirection.xlUp); Rngdown=rng.get_end (Excel.XlDirection.xlDown);//use the EntireRow or EntireColumn property to refer to a row or column that contains a specific rangeRNG = Thisapplication.get_range ("Test", Type.Missing); rng. areas[2]. EntireRow.Font.Bold =true; Manipulate the scope in the code. AutoFill: AutoFill lookup: Find sort: Sortcells gets a Range object that represents all the cells on the worksheet (not just the cells that are currently in use). cells[row, column]columns gets a Range object that represents all the columns on the worksheet rows gets a Range object that represents all the rows on the worksheet
Excel's Range object (C #)