Change_worksheet,Validation下拉列表

时间:2014-12-03 16:58:29

标签: excel vba events

我在设置事件时遇到了很多麻烦(确切地说是Worksheet_change),尽管我在这个论坛上发现了一些类似的问题:

我需要的是:

  • 每当选择/定位范围(" D2")时,我希望它解锁单元格并在未选择/定位后再次锁定

  • 另外,如果我们说"是"被选中我想要的一切"没有"选择以某种方式隐藏。如果没有隐藏它会更好,如果它只是使细胞= NoFill&文字白色?

到目前为止我的代码(sheet1):

下拉

Sub Dropdown()

Range("B2") = "structure"
Range("C2") = "Given:"
Range("B2:C2").Interior.Color = RGB(240, 128, 128)

' Dropdown-list
With Range("D2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=Data!A2:A3"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
Range("D2").Interior.Color = RGB(255, 250, 240)
End With

End Sub

我尝试取消保护细胞

Private Sub Worksheet_Change(ByVal Target As Range)

Dim InputRange As Range
Set InputRange = Range("D2")

Dim MyPassword As String
    MyPassword = "hej"

If Target = Range("D2") Then
        Unprotect Password:=MyPassword
            Target.Locked = False
        Protect Password:=MyPassword
End If

如果目标"是"

If Target = Range("D2") Then
    Select Case Range("D2").Value
        Case Is = "Yes"
            Range("B4") = "EQ%"
            Range("B5") = "D%"
            Range("B4:B5").Interior.Color = RGB(240, 128, 128)
            Range("C4:C5").Interior.Color = RGB(255, 250, 240)

            Cells.EntireColumn.AutoFit

如果目标"否"

        Case Is = "No"
            Range("E4") = "'+ Virksomhedskapital"
            Range("E5") = "'+ Overkurs ved emission"
            Range("E6") = "'+ Reserve for opskrivning"
            Range("E7") = "'+ Andre reserver"
            Range("E8") = "'+ Overført overskud eller underskud"
            Range("E10") = "'=Equity"
            Range("E4:E8").Interior.Color = RGB(240, 128, 128)
            Range("E10").Interior.Color = RGB(240, 128, 128)
            Range("F4:F8").Interior.Color = RGB(255, 250, 240)
            Range("F10").Interior.Color = RGB(135, 206, 250)

            Range("H4") = "'+ Prioritetsgæld"
            Range("H5") = "'+ Bankgæld"
            Range("H6") = "'+ Øvrig rentebærende gæld"
            Range("H7") = "'+ Overskydende likviditet"
            Range("H8") = "'+ Værdipapirer"
            Range("H9") = "'+ Øvrige ikke driftsaktiver"
            Range("H10") = "'= Nettorentebærende gæld"
            Range("H4:H10").Interior.Color = RGB(240, 128, 128)
            Range("I4:I9").Interior.Color = RGB(255, 250, 240)
            Range("I10").Interior.Color = RGB(135, 206, 250)

            Range("B6") = "EQ%"
            Range("B7") = "D%"
            Range("B6:B7").Interior.Color = RGB(240, 128, 128)
            Range("C6:C7").Interior.Color = RGB(135, 206, 250)

            Cells.EntireColumn.AutoFit
    End Select
End If

End Sub

模块1:

Sub ProtectAll()

Dim sh As Worksheet
Dim MyPassword As String

MyPassword = "test"

For Each sh In ActiveWorkbook.Worksheets
    sh.Protect Password:=MyPassword
Next sh

End Sub

Sub UnprotectAll()

Dim sh As Worksheet
Dim MyPassword As String

MyPassword = "test"

For Each sh In ActiveWorkbook.Worksheets
    sh.Unprotect Password:=MyPassword
Next sh

End Sub

1 个答案:

答案 0 :(得分:0)

我没有查看你的代码逻辑,也没有尝试编写代码逻辑,只是检查你是否已经把它:

Private Sub Worksheet_Change(ByVal Target As Range)

在这种情况下,

代码进入相应的Sheet模块(Sheet1?),而不是普通的代码模块。然后,您不需要在此代码中引用“测试”工作表。目前尚不清楚(对我而言)您是否无法解决该事件,或者您无法计算出您希望它执行的操作。

按照OPs回复编辑

好的,回到一垒,尝试这个简单的代码。将其放入单元格A1中具有数据验证列表的工作表中的 Worksheet_Change 事件中的工作表模块。从下面显示的数据验证对话框中设置单元格A1。从列表中选择“ONE”将为单元格C1:C4,'TWO'着色单元格E1:E4和'THREE'将'uncolor'两个范围。如果你有这个工作,你应该能够根据你的情况修改它。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target = Range("A1") Then
        Select Case Range("A1").Value
            Case Is = "ONE"
                Range("C1:C4").Interior.ColorIndex = 10
            Case Is = "TWO"
                Range("E1:E4").Interior.ColorIndex = 15
            Case Is = "THREE"
                Range("C1:E4").Interior.ColorIndex = xlNone
        End Select
    End If

End Sub

enter image description here

enter image description here