首页 > 其他 > 详细

8 Range对象(一)

时间:2014-02-27 18:28:13      阅读:673      评论:0      收藏:0      [点我收藏+]

8.1 引用Range

引用Range的主要方法:

Application.ActiveCell

Application.Range

Application.Selection

Worksheet.Cells

Worksheet.Columns

Worksheet.Range

Worksheet.Rows

Worksheet.UsedRange

CurrentRegion, NamedRange

代码清单8.1:使用Application对象引用Range

bubuko.com,布布扣
Sub ReferringToRangesI() 
    Dim rg As Range 
     
    ActiveCell is a range representing the 
    active cell. there can be one and 
    only one active cell. 
    Debug.Print Application.ActiveCell.Address 
     
    selection refers to a range representing 
    all of the selected cells. there can be 
    one or more cells in the range. 
    Debug.Print Application.Selection.Address 
     
    application.Range works on the active 
    worksheet 
    ThisWorkbook.Worksheets(1).Activate 
    Set rg = Application.Range("D5") 
    Debug.Print "worksheets 1 is active" 
    Debug.Print rg.Address 
    Debug.Print rg.Parent.Name 
     
    ThisWorkbook.Worksheets(2).Activate 
    Set rg = Application.Range("D5") 
    Debug.Print "worksheets 2 is active" 
    Debug.Print rg.Address 
    Debug.Print rg.Parent.Name
    
    Set rg = Nothing
End Sub
bubuko.com,布布扣

 Range中地址的表示法:

Application.Range("D5") 
Application.Range("A1:C5") 
Application.Range("A:A") 
Application.Range("3:3") 
Application.Range("A1:D5","D6:F10") 

 

8.1.1 WorkSheet对象的Cells属性和Range属性

代码清单8.2:使用Cells属性指定单个的单元格

bubuko.com,布布扣
Sub UsingCells() 
    Dim rg As Range 
    Dim nRow As Integer 
    Dim nColumn As Integer 
    Dim ws As Worksheet 
     
    Set ws = ThisWorkbook.Sheets(1) 
     
    For nRow = 1 To 10 
        For nColumn = 1 To 10 
            Set rg = ws.Cells(nRow, nColumn) 
            rg.Value = rg.Address 
        Next 
    Next 
     
    Set rg = Nothing 
    Set ws = Nothing 
End Sub
bubuko.com,布布扣

 

代码清单8.3:使用Range属性指向单元格组

bubuko.com,布布扣
Sub UsingRange() 
    Dim ws As Worksheet 
    Dim rg As Range 
     
    Set ws = ThisWorkbook.Worksheets(1)
specifying a range using Cells this range is equivalent to A1:J10 Set rg = ws.Range(ws.Cells(1, 1), ws.Cells(10, 10)) sets the value of each cell in the range to 1 rg.Value = 1 Set rg = ws.Range("D4", "E5") rg.Font.Bold = True ws.Range("A1:B2").HorizontalAlignment = xlLeft Set rg = Nothing Set ws = Nothing End Sub
bubuko.com,布布扣

考虑清单8.3中的语句:

Set rg = ws.Range(ws.Cells(1, 1), ws.Cells(10, 10))

此语句依靠四个整数确定Range引用的范围,这4个整数是两个对角单元格所在的行和列位置。所以特别适合动态确定范围。 

8.1.2 指向命名范围可能是棘手的

有两种范围的名称,工作薄范围和工作表范围。工作薄名称范围必须是唯一的,而工作表范围只需要在它们创建的工作表中是唯一的。

代码清单8.4:使用Names对象列出所有的命名范围

bubuko.com,布布扣
Test the ListWorkbookNmaes procedure outputs to cell A2 on the 2nd worksheet in the workbook 
Sub TestListNames() 
    ListWorkbookNames ThisWorkbook, ThisWorkbook.Worksheets(2).Range("A2") 
End Sub 

Sub ListWorkbookNames(wb As Workbook, rgListStart As Range) 
    Dim nm As Name 
    For Each nm In wb.Names 
        print out the name of the range 
        rgListStart.Value = nm.Name 
         
        print out what the range refers to 
        the ‘ is required so that excel doesn‘t consider it as a formula 
        rgListStart.Offset(0, 1).Value = "" & nm.RefersTo 
        rgListStart.Offset(0, 2).Value = "" & nm.Value 
        rgListStart.Offset(0, 3).Value = nm.RefersToRange 
         
        set rgListStart to refer to the cell the next row down. 
        Set rgListStart = rgListStart.Offset(1, 0) 
    Next 
End Sub 
bubuko.com,布布扣

 

8.1.2.1 安全第一:在使用命名范围之前确认他们有效

代码清单8.5:使用过程RangeNameExists确认名称有效

bubuko.com,布布扣
checks for the existence of a named range on a worksheet 
Function RangeNameExists(ws As Worksheet, sName As String) As Boolean 
    Dim s As String 
    On Error GoTo ErrHandler 
    
    s = ws.Range(sName).Address 
    RangeNameExists = True 
    Exit Function
ErrHandler: 
    RangeNameExists = False
End Function 

Sub ValidateNamedRangeExample() 
    If RangeNameExists(ThisWorkbook.Worksheets(1), "Test") Then 
        MsgBox "The name exists, it refers to: " & ThisWorkbook.Names("Test").RefersTo, vbOKOnly 
    Else 
        MsgBox "the name does not exist", vbOKOnly 
    End If 
    If RangeNameExists(ThisWorkbook.Worksheets(1), "djfs") Then 
        MsgBox "The name exists, it refers to: " & ThisWorkbook.Worksheets(1).Names("djfs").RefersTo, vbOKOnly 
    Else 
        MsgBox "the name does not exist", vbOKOnly      
    End If      
End Sub 
bubuko.com,布布扣

 

8.2 找到我们的方法

8.2.1 Offset用于相对导航

代码清单8.6:使用Offset属性的列表处理方法

bubuko.com,布布扣
Sub ListExample() 
    FilterYear 2000 
End Sub 

Sub Reset() 
    With ThisWorkbook.Worksheets("List Example") 
        .Rows.Hidden = False 
        .Rows.Font.Bold = False 
        .Rows(1).Font.Bold = True          
    End With 
End Sub 

Sub FilterYear(nYear As Integer) 
    Dim rg As Range 
    Dim nMileageOffset As Integer 
     
    1st row is column header so start with 2nd row 
    Set rg = ThisWorkbook.Worksheets("List Example").Range("A2") 
    nMileageOffset = 6 
     
    go until we bump into first empty cell 
    Do Until IsEmpty(rg) 
        If rg.Value < nYear Then 
            rg.EntireRow.Hidden = True 
        Else 
            check milage 
            If rg.Offset(0, nMileageOffset).Value < 40000 Then 
                rg.Offset(0, nMileageOffset).Font.Bold = True 
            Else 
                rg.Offset(0, nMileageOffset).Font.Bold = False 
            End If 
            rg.EntireRow.Hidden = False 
        End If 
        move down to the next row 
        Set rg = rg.Offset(1, 0) 
    Loop 

    Set rg = Nothing 
End Sub
bubuko.com,布布扣

 

8.2.2 最后的但不是最不重要的—找到End

代码清单8.7:使用End属性在一个工作表中导航

bubuko.com,布布扣
Sub ExperimentWithEnd() 
    Dim ws As Worksheet 
    Dim rg As Range 
     
    Set ws = ThisWorkbook.Worksheets(1) 
    Set rg = ws.Cells(1, 1) 
     
    ws.Cells(1, 8).Value = "rg.address = " & rg.Address 
    ws.Cells(2, 8).Value = "rg.End(xlDown).Address = " & rg.End(xlDown).Address 
    ws.Cells(3, 8).Value = "rg.End(xlDown).End(xlDown).Address = " & rg.End(xlDown).End(xlDown).Address 
    ws.Cells(4, 8).Value = "rg.End(xlToRight).Address = " & rg.End(xlToRight).Address 
     
    Set rg = Nothing 
    Set ws = Nothing 
End Sub 
bubuko.com,布布扣

 

代码清单8.8:查找列或者行中最后使用的单元格

bubuko.com,布布扣
returns a range object that represents the last non-empty cell in the same column 
Function GetLastCellInColumn(rg As Range) As Range 
    Dim lMaxRows As Long 
     
    lMaxRows = ThisWorkbook.Worksheets(1).Rows.Count 
     
    make sure the last cell in the column is empty 
    If IsEmpty(rg.Parent.Cells(lMaxRows, rg.Column)) Then 
        Set GetLastCellInColumn = rg.Parent.Cells(lMaxRows, rg.Column).End(xlUp) 
    Else 
        Set GetLastCellInColumn = rg.Parent.Cells(lMaxRows, rg.Column) 
    End If 
End Function 

returns a range object that represents the last non-empty cell in the same row 
Function GetLastCellInRow(rg As Range) As Range 
    Dim lMaxColumns As Long 
     
    lMaxColumns = ThisWorkbook.Worksheets(1).Columns.Count 
     
    make sure the last cell in the row is empty 
    If IsEmpty(rg.Parent.Cells(rg.Row, lMaxColumns)) Then 
        Set GetLastCellInRow = rg.Parent.Cells(rg.Row, lMaxColumns).End(xlToLeft) 
    Else 
        Set GetLastCellInRow = rg.Parent.Cells(rg.Row, lMaxColumns) 
    End If 
End Function
bubuko.com,布布扣

 

代码清单8.9:使用工作表可调用函数,返回列或者行中最后使用的单元格

bubuko.com,布布扣
returns a number that represents the last nonempty cell in the same column callable from a worksheet 
Function GetLastUsedRow(rg As Range) As Long 
    Dim lMaxRows As Long 
     
    lMaxRows = ThisWorkbook.Worksheets(1).Rows.Count 
    make sure the last cell in the column is empty 
    If IsEmpty(rg.Parent.Cells(lMaxRows, rg.Column)) Then 
        GetLastUsedRow = rg.Parent.Cells(lMaxRows, rg.Column).End(xlUp).Row 
    Else 
        GetLastUsedRow = rg.Parent.Cells(lMaxRows, rg.Column).Row 
    End If 
     
End Function 

returns a number that represents the last nonempty cell in the same row callable from a worksheet 
Function GetLastUsedColumn(rg As Range) As Long 
    Dim lMaxColumns As Long 
     
    lMaxColumns = ThisWorkbook.Worksheets(1).Columns.Count 
    If IsEmpty(rg.Parent.Cells(rg.Row, lMaxColumns)) Then 
        GetLastUsedColumn = rg.Parent.Cells(rg.Row, lMaxColumns).End(xlToLeft).Column 
    Else 
        GetLastUsedColumn = rg.Parent.Cells(rg.Row, lMaxColumns).Column 
    End If 
End Function
bubuko.com,布布扣

 

8.3 轻松输入;轻松输出

8.3.1 输出策略

代码清单8.10:提防包含了许多说明性文字范围的过程

bubuko.com,布布扣
this is procedures are generally error prone and unnecessarily difficult to maintain/modify 
Sub RigidFormattingProcedure() 
    Activate Test Report worksheet 
    ThisWorkbook.Worksheets("Test Report").Activate 
    make text in first column bold 
    ActiveSheet.Range("A:A").Font.Bold = True 
    widen first column to display text 
    ActiveSheet.Range("A:A").EntireColumn.AutoFit 
    format date on report 
    ActiveSheet.Range("A2").NumberFormat = "mmm-yy" 
    Make column headings bold 
    ActiveSheet.Range("6:6").Font.Bold = True 
     
    add & format totals 
    ActiveSheet.Range("N7:N15").Formula = "=sum(rc[-12]:rc[-1])" 
    ActiveSheet.Range("N7:N15").Font.Bold = True 
     
    ActiveSheet.Range("B16:N16").Formula = "=sum(r[-9]c:r[-1]c)" 
    ActiveSheet.Range("B16:N16").Font.Bold = True 
     
    format data range 
    ActiveSheet.Range("B7:N16").NumberFormat = "#,##0"      
End Sub 
bubuko.com,布布扣

本清单假定已在工作薄"Test Report"中定义如下的名称:
REPORT_TITLE
REPORT_DATE
COLUMN_HEADING
ROW_HEADING
DATA
COLUMN_TOTAL
ROW_TOTAL

代码清单8.11:一个更加灵活的处理结构化范围的过程

bubuko.com,布布扣
Sub RigidProcedureDeRigidized() 
    Dim ws As Worksheet 
    If Not WorksheetExists(ThisWorkbook, "Test Report") Then 
        MsgBox "can‘t find required worksheet ‘test report‘", vbOKOnly 
        Exit Sub 
    End If 
     
    Set ws = ThisWorkbook.Worksheets("Test Report") 
     
    If RangeNameExists(ws, "report_title") Then 
        ws.Range("report_title").Font.Bold = True 
    End If 
     
    If RangeNameExists(ws, "report_date") Then 
        With ws.Range("report_date") 
            .Font.Bold = True 
            .NumberFormat = "mmm-yy" 
            .EntireColumn.AutoFit 
        End With 
    End If 
     
    If RangeNameExists(ws, "row_heading") Then 
        ws.Range("row_heading").Font.Bold = True 
    End If 
     
    If RangeNameExists(ws, "column_heading") Then 
        ws.Range("column_heading").Font.Bold = True 
    End If 
     
    If RangeNameExists(ws, "DATA") Then 
        ws.Range("DATA").NumberFormat = "#,##0" 
    End If 
     
    If RangeNameExists(ws, "COLUMN_TOTAL") Then 
        With ws.Range("COLUMN_TOTAL") 
            .Formula = "=sum(r[-9]c:r[-1]c)" 
            .Font.Bold = True 
            .NumberFormat = "#,##0" 
        End With 
    End If 
     
    If RangeNameExists(ws, "ROW_TOTAL") Then 
        With ws.Range("ROW_TOTAL") 
            .Formula = "=sum(rc[-12]:rc[-1])" 
            .Font.Bold = True 
            .NumberFormat = "#,##0" 
        End With 
    End If 
         
    Set ws = Nothing 
End Sub 
bubuko.com,布布扣

 

8.3.2 接受工作表输入

代码清单8.12:确认一个有正确数据的范围 

bubuko.com,布布扣
Function ReadCurrencyCell(rg As Range) As Currency 
    Dim cValue As Currency 
    cValue = 0 
     
    On Error GoTo ErrHandler 
     
    If IsEmpty(rg) Then GoTo ExitFunction 
    If Not IsNumeric(rg) Then GoTo ExitFunction 
     
    cValue = rg.Value 
 
ExitFunction: 
    ReadCurrencyCell = cValue 
    Exit Function 
 
ErrHandler: 
    ReadCurrencyCell = 0 
End Function
bubuko.com,布布扣

8 Range对象(一),布布扣,bubuko.com

8 Range对象(一)

原文:http://www.cnblogs.com/cuishengli/p/3569970.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!