我正在尝试编写一些代码来隐藏列,如果某个范围中单元格的前3个字符等于另一个内容。我有用于隐藏列的代码,如果范围中的单元格是空白的,则是这样;-
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, cell As Range
On Error GoTo ErrHandler
Set r = Me.Range("C8:R8")
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each cell In r
If cell.Value = "" Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next
ErrHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
以及用于识别单元格前三个字符的代码;-
Dim LResult As String
LResult = Left ("Alphabet",3)
但是,如何结合引用特定单元而不是“字母”的两者呢?
不能让它正常工作-有什么建议吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, cell As Range
On Error GoTo ErrHandler
Set r = Me.Range("B7:CG7")
Application.ScreenUpdating = False
Application.EnableEvents = False
Row = 1
col = 1
For Each cell In r
If cell.Value = "" And Left(cell.Value, 3) = cell(Row, col).Value Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next
ErrHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
干杯
最佳答案
您几乎拥有有效的代码。您正在将cell.Value与一个空字符串进行比较-现在只需向其应用Left
LResult = Left (cell.Value,3)
编辑:
row = 20
col = 30
For Each cell In r
If cell.Value = "" and Left (cell.Value,3) = Cell(row, col).Value Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
您要从第和col列的单元格获取数据(我以20、30为例)
关于excel-vba - 将IF else与LEFT合并以隐藏列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1636598/