Excel VBA(3) Ranges

Range

Range property and shortcut references

Application.Range("B2")

Range("B2")

Range("A1:D10")

Range("A1:A10, C1:C10, E1:E10")

Range("A1", "D10")

Range("Name")

Range("A1", Range("Name"))

'Shortcut:

[B2]

[A1:D10]

[A1:A10, C1:C10]

[Name]

Ranges on Inactive Worksheets

Workbooks("1.xlsx").Worksheets("Sheet1").Range("A1")

With Workbooks("1.xlsx").Worksheets("Sheet1")
	.Range.Select
End With

Range property of a range object

Range("B2").Range("A1") '=Range("B2")

Cells

Cells Property

ActiveSheet.Cells
Application.Cells
Cells

Cells.Item(2,2)
Cells.Item(2, "B")

Cells(2,2)
Cells(2, "B")

Cells Used in Range

Range(Cells(1,1), Cells(10, 5))

More on the Cells Property of the Range Object

Range("D10").Cells(2,3) '=
Range("D10")(2,3) '=
[D10].Cells(2,3)

Single-Parameter Range Reference

Range(“D10:E11")(2) '=E11
Range(“D10:E11")(7) '=D13

Offset Property

Range("A1").Offset(0, 0)

Resize Property

Range("A1: B10").Resize(1,2)
Range("A1: B10").Resize(, 2)

SpecialCells Method

Last Cell

'last row & column in the worksheet:
Set rngLast = Range("A1").SpecialCells(xlCellTypeLastCell)
lLastRow = rngLast.Row
lLastCol = rngLast.Column

'A more complex one:

Sub GetRealLastCell()
	Dim lRealLastRow As Long
	Dim lRealLastColumn As Long
	'Get bottom right corner of cells with data
	Range("A1").Select

	On Error Resume Next
	lRealLastRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
	lRealLastColumn = Cells.Fin("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column

	Cells(lRealLastRow, lRealLastColumn).Select
End Sub

'eg: Delete Unused Formats:
Sub DeleteUnusedFormats()
	Dim lLastRow as Long, lLastColumn As Long
	Dim lRealLastRow As Long, lRealLastColumn as Long

	'Delete from used range rows & columns that have no data

	'Delete end of used range including empty formatted cells
	With Range("A1").SpecialCells(xlCellTypeLastCell)
		lLastRow = .Row
		lLastColumn = .Column
	End With

	'Find end of cells with data
	On Error Resume Next
	lRealLastRow = Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
	lRealLastColumn = Cells.Fin("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column

	Cells(lRealLastRow, lRealLastColumn).Select

	'If used range exceeds data, delete unused rows & columns
	If lRealLastRow < lLastRow Then
		Range(Cells(lRealLastRow + 1, 1), Cells(lLastRow, 1)).EntireRow.Delete
	End If

	If lRealLastColumn < lLastColllumn Then
		Range(Cells(1, lRealLastColumn + 1,), Cells(1, lLastColumn)).EntireColumn.Delete
	End If

	ActiveSheet.UsedRange 'Resets LastCell

Deleting Numbers

On Error Resume Next
Cells.SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents

'to keep date:
On Error Resume Next
For Each rng In Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
	If Not IsDate(rng.Value) Then rng.ClearContents
Next rng

CurrentRegion Property

Range("name").CurrentRegion.Select

End Property

Range("A1").End(xlDown)
Range("A1").End(xlToRight)

Referring to Ranges with End

Range("B3", Range("B3").End(xlToRight).End(xlDown)).Select

Summing a Range

With ActiveCell
	Set rng = Range(.Offset(1), .Offset(1).End(xlDown))
	.Formula = "=SUM(" * rng.Address(RowAbsolute:=False, ColumnAbsolute:=False) & ")"
	.Copy Destination:=Range(.Cells(1), .Offset(1).End(xlToRight).Offset(-1))
End With

Columns and Rows Properties

Rows.Count
rng.Rows

Areas

For Each rng in Range("name").Areas
//Note: .Columns.Count gives the first continus area's count, same as Rows.Count

Union and Intersect Methods

Union(rng1, rng2)
Intersect(rng1, rng2)

//eg: Prevent a user from changing data in particular range, for example, B10:F20 + H10:L20

Private Sub Worksheet_SelectionChange(ByVal Target as Range)
	Dim rngForbidden As Range
	Set rngForbidden = Union(Range("B10:F20"), Range("H10:L20")
	If Intersect(Target, rngForbidden) Is Nothing Then Exit Sub

	Range("A1").Select
	MsgBox "You cannot select cells in ..."
End Sub

Empty Cells

IsEmpty(ActiveCell.Value)

Transferring Values between Arrays and Ranges

'eg1:
Dim vSalesData As Variant
Dim vaDiscount As Variant

vSalsesData = Range().Value
Redim vaDiscount(1 To Ubound(vSalesData, 1), 1 To 1)

Range().Value = vaDiscount

'or, change to
RiDim vaDiscount(1 To Ubound(vSalesData,1))
Range().Value = WorkSheetFunction.Transpose(vaDiscount)

Deleting Rows

The fast way to delete certain rows may be techniques with AutoFilter

Sub DeleteRows3()
	Dim lLastRow As Long
	Dim rng As Range
	Dim rngDelete As Range

	'Freeze
	Applicaiton.ScreenUpdating = False
	
	'Insert dummy row for dummy field name? why?
	Rows(1).Insert
	
	'Insert dummy field name
	Range("C1").Value = "Temp"

	With ActiveSheet
		'Reset Last Cell
		.UsedRange
		'Determine last row
		lLastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
		'Set rng to the C column data rows
		Set rng = Range("C1", Cells(lLastRow, "C"))
		'Filter the C column to show only the data to be deleted
		rng.AutoFilter Field:=1, Criterial:="Mangoes"

		'Get referencce to the visible cells, including dummy field name
		Set rngDelete = rng.SpecialCells9xlCellTypeVisible)

		'Turn off AutoFilter
		rng.AutoFilter

		'Delete rows
		rngDelete.EntireRow.Delete

		'Reset the last cell
		.usedRange
	End With
End Sub

转载于:https://www.cnblogs.com/kongs/archive/2012/02/02/2331333.html

猜你喜欢

转载自blog.csdn.net/weixin_34068198/article/details/93394981