根据单元格的变化保护和格式化指定的单元格

时间:2018-03-29 11:57:25

标签: excel-vba vba excel

我有一个cellrange(U4:U50),允许你选择" yes"和"不"。我希望,当A列中的相关单元格改变值时,对于每一行,格式化并保护右侧的单元格(V4:AL4,V4:AL4等,直到V50:AL50)。

根据我的小知识,我只能将几段代码放在一起:我设法根据下面的代码对第4行进行所需的更改。

保护和UNprotect子目录在ThisWorkbook中,他们就是这样做的。

Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve com.android.tools.build:gradle:4.4.
Caused by: org.gradle.api.resources.ResourceException: Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/4.4/gradle-4.4.pom'.
Caused by: org.gradle.internal.resource.transport.http.HttpRequestException: Could not GET 'https://jcenter.bintray.com/com/android/tools/build/gradle/4.4/gradle-4.4.pom'.
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to proxy-********:8080 [proxy-********/172.20.10.5] failed: connect timed out

下一步是让代码适用于所有依赖于目标范围的行,所以我从这个开始

 Sub Worksheet_Change(ByVal Target As Range)

 Set checkRange = Application.Intersect(Target, Range("U4:U50"))

' If the change wasn't in this range then we're done
    If checkRange Is Nothing Then Exit Sub

 If Range("U4").Value = "Yes" Then
        Range("V4:AL4").Select
        Call ActiveWorkbook.UNprotect_all_sheets

        With Selection
        .Locked = True
        End With
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark2
        .TintAndShade = -9.99786370433668E-02
        .PatternTintAndShade = 1
        End With

        Range("U4").Select


    ElseIf Range("U4").Value <> "Yes" Then
        Call ActiveWorkbook.UNprotect_all_sheets
        Range("V4:AL4").Select

    With Selection
        .Locked = False
        End With
        With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0


       End With


    End If
       Call ActiveWorkbook.Protect_all_sheets

End Sub

但我的问题是我现在知道如何:

  1. 从单元格(r,c + 1)到单元格(r,c + 17)选择单元格范围;
  2. 制作相对于右行的指令。
  3. 对此的任何评论都非常受欢迎!!

    多谢你们所有人,我希望你能从我的解释中了解我需要做些什么。

    我一直在寻找答案,也许我找不到合适的措辞..

1 个答案:

答案 0 :(得分:0)

你可以这样做。一般来说,没有必要选择任何东西,但是我把它留在了,因为不清楚你的其他潜艇是否正在选择。你可以使用Resize,但是我不能费心计算从V到AL的列数。

在反射时,重新配置第一个块可能是安全的,就像我在第二个块中所做的那样(也许在任何情况下都应该在选择之前调用unprotect)。

严格来说,代码应该适应要更改的多个单元格。为此,您可以将Target的实例更改为Target(1)

Sub Worksheet_Change(ByVal Target As Range)

Set checkRange = Application.Intersect(Target, Range("U4:U50"))

' If the change wasn't in this range then we're done
If checkRange Is Nothing Then Exit Sub

If Target.Value = "Yes" Then
    Range(Cells(Target.Row, "V"), Cells(Target.Row, "AL")).Select
    Call ActiveWorkbook.UNprotect_all_sheets
    With Selection
        .Locked = True
        With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark2
            .TintAndShade = -9.99786370433668E-02
            .PatternTintAndShade = 1
        End With
    End With
Else
    Call ActiveWorkbook.UNprotect_all_sheets
    With Range(Cells(Target.Row, "V"), Cells(Target.Row, "AL"))
        .Locked = False
        With .Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End With
End If

Call ActiveWorkbook.Protect_all_sheets

End Sub