首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(VBA)适用于所有人,但一项具体规定除外

(VBA)适用于所有人,但一项具体规定除外
EN

Stack Overflow用户
提问于 2017-04-26 10:06:57
回答 2查看 277关注 0票数 1

大家好,我用VBA模块在excel上做了一个按钮,代码在活动表上工作,但是我想要的是应用到更多的工作表上,而不仅仅是放在按钮所在的活动工作表上。

代码语言:javascript
复制
Sub Botón1_Haga_clic_en()
    Call Worksheet_Calculate
End Sub


'apply cells colors from single-cell formula dependencies/links
Private Sub Worksheet_Calculate()
    Dim Cel       As Range
    Dim RefCel  As Range

    On Error Resume Next

    For Each Cel In ActiveSheet.UsedRange
        If Cel.HasFormula Then
            Set RefCel = Evaluate(Mid(Cel.Formula, 2))
            Cel.Interior.Color = RefCel.Interior.Color
        End If
    Next Cel

End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-26 11:14:45

试试下面的代码:

代码语言:javascript
复制
Option Explicit

Sub Botón1_Haga_clic_en()

Dim wsName As String
Dim ws As Worksheet

wsName = ActiveSheet.Name

For Each ws In ThisWorkbook.Worksheets
    If Not ws.Name Like wsName Then '<-- is worksheet's name doesn't equal the ActiveSheet's
        ApplyCellColors ws ' <-- call you Sub, with the worksheet object
    End If
Next ws

End Sub

'=======================================================================    
'apply cells colors from single-cell formula dependencies/links

Private Sub ApplyCellColors(ws As Worksheet)

    Dim Cel       As Range
    Dim RefCel  As Range

    On Error Resume Next

    For Each Cel In ws.UsedRange
        If Cel.HasFormula Then
            Set RefCel = Evaluate(Mid(Cel.Formula, 2))
            Cel.Interior.Color = RefCel.Interior.Color
        End If
    Next Cel

End Sub
票数 0
EN

Stack Overflow用户

发布于 2017-04-26 10:17:07

您的问题可以转化为这样的问题:如何遍历所有的工作表,而忽略其中的一个?

这是一个很好的方法:

代码语言:javascript
复制
Option Explicit
Option Private Module

Public Sub TestMe()

    Dim wks As Worksheet

    For Each wks In ThisWorkbook.Worksheets
        If wks.name = "main" Then
            Debug.Print "Do nothing here, this is the active sheet's name"
        Else
            Debug.Print wks.name
        End If

    Next wks

End Sub

非常肯定,您应该能够将它安装在代码中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43631079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档