VBA相关参考

问题描述 投票:0回答:1

VBA的新手,只是学习如何使用相对引用。我有一个excel工作表,它将包含大约27个表,并且我需要为该工作表创建5个版本。我使用的工具的输出格式不是所需的格式。下面是输出的屏幕截图:

enter image description here

这是我要实现的目标的一个示例:

enter image description here

工作表中的每个表将具有不同的行数。我设法获得了将问题加粗的代码,将列居中(从“总计”到“无”)并突出显示了不同的部分……但是我无法获得使行标签和百分比加粗的代码,或标有“列名”的行。

这是我的第一部分代码:

 Format Macro
'
' Keyboard Shortcut: Ctrl+Shift+F
'
    ActiveCell.Select
    Selection.Font.Bold = True
    ActiveCell.Offset(2, 1).Range("A1:B1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    ActiveCell.Offset(-1, 3).Range("A1:E1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
    ActiveCell.Offset(2, 0).Range("A1:E1").Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
    ActiveCell.Offset(-2, 8).Range("A1:B1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
    ActiveCell.Offset(2, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
    ActiveCell.Offset(0, -11).Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToLeft)).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With

End Sub

这里是我尝试编写粗体代码的代码:

' Bold Macro
'
' Keyboard Shortcut: Ctrl+b
'
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = False
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = False
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
    ActiveCell.Offset(3, 0).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Font.Bold = True
End Sub

我提供了一个示例文件的链接以供参考。

任何见解将不胜感激。谢谢!

Example

excel vba excel-vba
1个回答
0
投票

不是您的问题所在的“相对引用”,而是引用。使用此代码。

Dim Ws As WorkSheet
Dim Rng As Range

Set Ws = Worksheets("Sheet1")
Set Rng = Ws.Cells(1, "A").Resize(2, 10)

代码首先声明两个对象,即给出名称。通过这种简单的设置,您可以避免所有“选择”或“激活”语句,并且消除了对ActiveSheetSelection对象的所有引用。这大约是您代码的一半。

已经定义了工作表并为其指定了名称,您可以引用其上的任何单元格,例如Ws.Cells(13, "C").Font.Bold = True。请注意,单元格(13,“ C”)也可以(更好)寻址为单元格(13,3)。您可以更改该工作表的名称,例如Ws.Name = "My New Name",但仍在代码中将其称为Ws。您可以如上所述指定范围,方法是指定一个偏移量(如Resize方法所做的那样),也可以仅指定第一个和最后一个单元格。

Set Rng = Ws.Range("A1:C37")
or 
Set Rng = Ws.Range(Ws.Cells(1, 1), Ws.Cells(37, "A"))
which would usually be written like this:-
With Ws
    Set Rng = .Range(.Cells(1, 1), .Cells(37, 1))
End With

您还可以使用工作表对象来标识工作表中的表。

Dim Tbl As ListObject
Set Tbl = Ws.ListObjects(1)
or
Set Tbl = Ws.ListObjects("Table1")

该新对象使您可以访问各种单元格。

Set Rng = Tbl.DataBodyRange    ' all the data below the header row
Set Rng = Tbl.HeaderRowRange   ' the row with captions
Set Rng = Tbl.Range            ' all of the table

在每个范围内,单元格都可以通过其行和列或索引号来访问。 Tbl.Range.Cells(1)Tbl.Range.Cells(1, 1)Tbl.HeaderRowRange.Cells(1)相同。这些单元格中的每一个都有一个Font对象,该对象具有Bold属性,您无需选择任何内容即可进行设置。而且您可能仍然可以通过其工作表坐标来引用它们。

足够。请从这里拿走。

© www.soinside.com 2019 - 2024. All rights reserved.