ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Excel VBA(3) Ranges

2019-06-23 18:50:57  阅读:533  来源: 互联网

标签:A1 VBA End Dim rng Cells Excel Ranges Range


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

标签:A1,VBA,End,Dim,rng,Cells,Excel,Ranges,Range
来源: https://blog.csdn.net/weixin_34068198/article/details/93394981

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有