gpt4 book ai didi

WorkSheet Change to hide rows based on two different cells and different cases(工作表更改为隐藏基于两个不同单元格和不同事例的行)

转载 作者:bug小助手 更新时间:2023-10-22 15:46:46 27 4
gpt4 key购买 nike



First time posting on stack overflow so apologies if I dont get this right.

第一次发布堆栈溢出,所以很抱歉,如果我没有做到这一点。


I am a fairly novice VBA user which mostly consists of googling different VBA pieces and frankensteining them together.

我是一个相当新手的VBA用户,主要包括在谷歌上搜索不同的VBA片段并将它们放在一起。


I have frankensteined a VBA for Worksheet Change to hide blank rows by looking at which option is selected from a data validation list. I can get it working for one data validation option however, the user has the option of two data validation lists. If they select on one (lets say $B$2) there may need to be 5 rows hidden and the other data validation is blank, if they select on the other (say $C$3) there may need to be 7 rows hidden and again the other data validation is blank.

我创建了一个VBA for Worksheet Change,通过查看从数据验证列表中选择的选项来隐藏空白行。我可以让它为一个数据验证选项工作,但是,用户可以选择两个数据验证列表。如果他们在其中一个(比如说$B$2)上选择,则可能需要隐藏5行,而另一个数据验证为空,如果他们在另一个(比方说$C$3)上进行选择,那么可能需要隐藏7行,而其他数据验证再次为空。


The single code works, but I am having trouble with the If Else Then and Case for the multiple options as described above. Any help on the below would be much appreciated. Essentially I want it to look at two data validation ranges, if one is blank, look at the other and when the user changes the data validation entry it hides the blanks rows up to row 22. And vice versa on the other data validation list.

单个代码可以工作,但如上所述,我在多个选项的If Else Then和Case方面遇到了问题。如能在以下方面提供任何帮助,我们将不胜感激。从本质上讲,我希望它查看两个数据验证范围,如果一个是空的,则查看另一个,当用户更改数据验证条目时,它会隐藏直到第22行的空白行。在其他数据验证列表中反之亦然。


Private Sub Worksheet_Change(ByVal Target As Range)

'Define Dims
Dim counter As Long
Dim iRange As Range
Dim AreaToHide As Range

Rows("13:22").Hidden = False

'Calculate book
Worksheets("Home").Calculate

'Case select view by pack
If Target.Address(True, True) = "$D$13" Then
Select Case Target
Case "Product 1", "Product 2", "Product 3"

'Find last row to hide for view by pack
With ActiveSheet.Range("E13:E22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With

'hide blank rows for view by pack
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True

'Case select view by country
ElseIf Target.Address(True, True) = "$C$13" Then
Select Case Target
Case "Country 1", "Country 2", "Country 3"

'find last row to hide for view by country
With ActiveSheet.Range("G13:G22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With


'hide blank rows for view by country
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True

Case Else
'Do nothing
End Select

End If
End Function


End Sub

Thanks in advance.

提前谢谢。


更多回答

That code can't compile as you are missing End Select.

该代码无法编译,因为您缺少End Select。

Is that your question? You don't actually say if you have been getting error messages or another problem.

这是你的问题吗?你实际上并没有说你是否收到了错误消息或其他问题。

@SRJ Correct, the code gives me an Else without IF error on line "ElseIf Target.Address(True, True) = "$C$13" Then"

@SRJ正确,代码在“ElseIf Target.Address(True,True)=”$C$13“Then”行给我一个Else without IF错误

@SRJ apologies, do I need two End Selects? One after each select case statement? Thanks

@SRJ很抱歉,我需要两个End Selects吗?每个选择案例陈述后一个?谢谢

优秀答案推荐

The following worked if anyone ever wants to know, I was missing a Case Else,
End Select in the first case section and didnt need the End Function:

如果有人想知道的话,下面的方法很有效,我在第一个案例部分缺少了一个案例Else,End Select,不需要End Function:


Private Sub Worksheet_Change(ByVal Target As Range)

'Define Dims
Dim counter As Long
Dim iRange As Range
Dim AreaToHide As Range

Rows("13:22").Hidden = False

'Calculate book
Worksheets("Home").Calculate

'Case select view by pack
If Target.Address(True, True) = "$D$13" Then
Select Case Target
Case "Product 1", "Product 2", "Product 3"

'Find last row to hide for view by pack
With ActiveSheet.Range("E13:E22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With

'hide blank rows for view by pack
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True

Case Else
End Select


'Case select view by country
ElseIf Target.Address(True, True) = "$C$13" Then
Select Case Target
Case "Country 1", "Country 2", "Country 3"

'find last row to hide for view by country
With ActiveSheet.Range("G13:G22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With


'hide blank rows for view by country
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True

Case Else
'Do nothing
End Select

End If


End Sub

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com