VBA新手,并尝试根据工作表1(F4)中的单元格值(值0-10)创建代码以隐藏/取消隐藏工作表2(称为“费用代码”)上的行。我目前正在使用下面的代码,它通过单元格F23工作,但是程序太长了。我需要为细胞F4:F53复制这个细胞,每个F细胞都有不同的10行对应组以隐藏/取消隐藏在表2上。任何人都可以帮我编译吗?这是我用于F4的例子:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = ("$F$4") And Target.Value = "0" Then Sheets(“Charge Codes”).Rows("3:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "1" Then Sheets(“Charge Codes”).Rows("3").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("4:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "2" Then Sheets(“Charge Codes”).Rows("3:4").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("5:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "3" Then Sheets(“Charge Codes”).Rows("3:5").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("6:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "4" Then Sheets(“Charge Codes”).Rows("3:6").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("7:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "5" Then Sheets(“Charge Codes”).Rows("3:7").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("8:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "6" Then Sheets(“Charge Codes”).Rows("3:8").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("9:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "7" Then Sheets(“Charge Codes”).Rows("3:9").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("10:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "8" Then Sheets(“Charge Codes”).Rows("3:10").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("11:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "9" Then Sheets(“Charge Codes”).Rows("3:11").EntireRow.Hidden = False: Sheets(“Charge Codes”).Rows("12:12").Hidden = True
If Target.Address = ("$F$4") And Target.Value = "10" Then Sheets(“Charge Codes”).Rows("3:12").EntireRow.Hidden = False
End Sub
答案 0 :(得分:1)
如果我的基本数学正确,这应该照顾你的隐藏行组。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$4" Then
Worksheets("Charge Codes").Cells(3, 1).Resize(10, 1).EntireRow.Hidden = False
Select Case Target.Value
Case 0, 10
Worksheets("Charge Codes").Cells(3, 1).Resize(10, 1).EntireRow.Hidden = True
Case 1, 2, 3, 4, 5, 6, 7, 8, 9
Worksheets("Charge Codes").Cells(3, 1).Resize(Target.Value, 1).EntireRow.Hidden = True
Case Else
'do nothing
End Select
End If
End Sub
答案 1 :(得分:1)
这使用嵌套的Ifs:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = ("$F$4") Then
If Target.Value <> 0 Then
Sheets("Charge Codes").Rows("3:12").Hidden = True
ElseIf Target.Value = 10 Then
Sheets("Charge Codes").Rows("3:12").Hidden = False
Else
Sheets("Charge Codes").Rows(3 + Target.Value & ":12").Hidden = True
Sheets("Charge Codes").Rows("3:" & 2 + Target.Value).Hidden = False
End If
End Sub
答案 2 :(得分:0)
只需使用逻辑来查找要隐藏的行号
If Target.Address = ("$F$4") Then
Sheets("Charge Codes").Rows(3 & ":" & CDbl(Target.Value) + (CDbl(Target.Value) - 3)).EntireRow.Hidden = False: _
Sheets("Charge Codes").Rows(12 - CDbl(Target.Value) & ":" & 12).EntireRow.Hidden = True
End If