excel - 在vba中,如果语句语句的顺序正确

  显示原文与译文双语对照的内容
116 0

当 $A$5的值等于"公司 2"时,我需要这个宏来隐藏某些列。 下面的查询隐藏了列,但不执行打印循环。 正确设置这里设置的方法是什么?


Sub PrintAll()

 Dim BrokerCell As Range
 Dim TotalCell As Range
 Dim Rng As Range
 Dim Wks As Worksheet

 Set Wks = Worksheets("PRINT PAGE")

 If Range("$A$5").Value ="Company 1" Then
 Set Rng = ThisWorkbook.Names("Company1").RefersToRange
 ElseIf Range("$A$5").Value ="Company 2" Then
 Set Rng = ThisWorkbook.Names("Company2").RefersToRange
 Else: Set Rng = ThisWorkbook.Names("Company3").RefersToRange
 End If

If Range("$A$5").Value ="Company 2" Then
 Columns("M:O").Select
 Selection.EntireColumn.Hidden = True
 Else: Columns("M:O").Select
 Selection.EntireColumn.Hidden = False

 For Each BrokerCell In Rng
 If BrokerCell <>"" And Range("$S$5").Value <>"" Then
 Wks.Range("$B$5").Value = BrokerCell.Text
 Wks.PrintOut
 End If
 Next BrokerCell

 End If

End Sub

时间: 原作者:

77 0

你的For Each 循环包含在 If Range("$A$5").Value ="Company 2" Then 语句。仅当 If 语句计算为 false 时才执行。

如果需要在所有实例中执行 For Each 循环,则在 End If 执行后将它的移动。 如果需要仅在 If 语句计算为 True 时执行,则在 Else 语句之前将它的移动。

原作者:
144 5

我相信这就是oyu想要完成的,见下面。


Sub PrintAll()

Dim BrokerCell As Range
Dim TotalCell As Range
Dim Rng As Range
Dim Wks As Worksheet
Dim sCellValue As String

Set Wks = Worksheets("PRINT PAGE")
sCellValue = Replace(Range("$A$5").Value,"","")
 If sCellValue ="Company1" Then
 Set Rng = ThisWorkbook.Names(sCellValue ).RefersToRange
 ElseIf sCellValue ="Company2" Then
 Set Rng = ThisWorkbook.Names(sCellValue ).RefersToRange
 Else
 Set Rng = ThisWorkbook.Names("Company3").RefersToRange
 End If

 Columns("M:O").Select
 If sCellValue ="Company2" Then
 Selection.EntireColumn.Hidden = True
 Else
 Selection.EntireColumn.Hidden = False
 End If
 For Each BrokerCell In Rng
 If BrokerCell <>"" And Range("$S$5").Value <>"" Then
 Wks.Range("$B$5").Value = BrokerCell.Text
 Wks.PrintOut
 End If
 Next BrokerCell

End Sub

原作者:
...