如何应用将活动选定单元格突出显示到所有活动工作表的vba代码?

时间:2016-09-13 16:42:39

标签: excel vba excel-vba macros

我目前正在尝试通过将此代码转换为宏来将此代码应用于活动工作表。但是我遇到了每个功能的问题。

这是原始代码。

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    'Update 20140318
    Static xRow
    Static xColumn
    If xColumn <> "" Then
        With Columns(xColumn).Interior
            .ColorIndex = xlNone
        End With
        With Rows(xRow).Interior
            .ColorIndex = xlNone
        End With
    End If
    pRow = Selection.Row
    pColumn = Selection.Column
    xRow = pRow
    xColumn = pColumn
    With Columns(pColumn).Interior
        .ColorIndex = 22
        .Pattern = xlSolid
    End With
    With Rows(pRow).Interior
        .ColorIndex = 6
        .Pattern = xlSolid
    End With
End Sub

我转换成功的尝试失败了:

Sub Highlighter()
'
' Highlighter Macro
'
' Keyboard Shortcut: Ctrl+Shift+H
'
Dim xSheet As Worksheet

For Each xSheet In This.Workbook.Worksheets
    xSheet.Select
    Static xRow
    Static xColumn

    If xColumn <> "" Then
        With Columns(xColumn).Interior
            .ColorIndex = xlNone
        End With
        With Rows(xRow).Interior
            .ColorIndex = xlNone
        End With
    End If
    pRow = Selection.Row
    pColumn = Selection.Column
    xRow = pRow
    xColumn = pColumn
    With Columns(pColumn).Interior
        .ColorIndex = 22
        .Pattern = xlSolid
    End With
    With Rows(pRow).Interior
        .ColorIndex = 6
        .Pattern = xlSolid



Next xSheet

End Sub

请帮忙!谢谢!

第三次尝试&gt;&gt;它正在工作,但如何在工作簿中进行选择更改以应用于所有工作表?

Sub Highlighter()
'
' Highlighter Macro
'
' Keyboard Shortcut: Ctrl+Shift+H
'


Dim xSheet As Worksheet

For Each xSheet In ActiveWorkbook.Worksheets


xSheet.Select
Static xRow
Static xColumn

If xColumn <> "" Then
    With xSheet.Columns(xColumn).Interior
        .ColorIndex = xlNone
    End With
    With xSheet.Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
pRow = Selection.Row
pColumn = Selection.Column
xRow = pRow
xColumn = pColumn
With xSheet.Columns(pColumn).Interior
    .ColorIndex = 22
    .Pattern = xlSolid
End With
With xSheet.Rows(pRow).Interior
    .ColorIndex = 6
    .Pattern = xlSolid

End With

4 个答案:

答案 0 :(得分:0)

替换

This.Workbook.Worksheets

通过

ThisWorkbook.Worksheets

vba中不存在对象This。但是,有一个名为ThisWorkbook的全局属性(用一个词)。

但是,您的代码还有另一个问题,即使用静态变量。你现在这样做的方式只会保存最后一张纸的选择。您需要找到另一种方法来存储该信息。一种需要最少代码更改的方法是使用dictionaries,它允许您存储多个值并将它们与特定工作表相关联。

答案 1 :(得分:0)

将其粘贴到模块中:

Sub Highlight()

Dim xSheet As Worksheet

For Each xSheet In Worksheets

    xSheet.Select

    With xSheet.Cells.Interior
        .ColorIndex = xlNone
    End With

    With xSheet.Columns(Selection.Column).Interior
        .ColorIndex = 22
        .Pattern = xlSolid
    End With
    With xSheet.Rows(Selection.Row).Interior
        .ColorIndex = 6
        .Pattern = xlSolid
    End With


Next xSheet

End Sub

将其粘贴到每个工作表的代码中:

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Call Highlight
End Sub

如果要通过代码将代码添加到工作表的Worksheet_SelectionChange事件中,请参阅以下问题: Excel vba add code to sheet module programmatically

答案 2 :(得分:0)

您的代码适用于任何单张,如果它位于该工作表的工作表代码区域中。

如果您希望代码能够处理多个工作表,则必须将其放在每个工作表的工作表代码区域中。

循环不会。

答案 3 :(得分:0)

将以下内容放入模块中:

Sub Highlight(ws As Worksheet, xRow As Long, xColumn As Long)
    'Clear previous formatting
    If xColumn > 0 Then
        With ws.Columns(xColumn).Interior
            .ColorIndex = xlNone
        End With
        With ws.Rows(xRow).Interior
            .ColorIndex = xlNone
        End With
    End If

    pRow = Selection.Row
    pColumn = Selection.Column
    xRow = pRow
    xColumn = pColumn
    With ws.Columns(pColumn).Interior
        .ColorIndex = 22
        .Pattern = xlSolid
    End With
    With ws.Rows(pRow).Interior
        .ColorIndex = 6
        .Pattern = xlSolid
    End With
End Sub

并将以下内容放入每个工作表中:

Private xRow As Long
Private xColumn As Long

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Call Highlight(Me, xRow, xColumn)
End Sub

然后,每个工作表将跟踪先前的选择&#34;特定于该工作表,这些值将传递给公共Highlight子例程。

一个警告:保存工作簿后,也会保存高亮显示。重新打开工作簿时,&#34;之前的选择&#34;将不再为宏所知。您可以在Workbook_Open事件中添加一些内容来设置每个工作表的初始xRow和xColumn值。