如果某个范围内单元格的前 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
以及识别单元格前 3 个字符的代码;-
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
您想要从行和列的单元格中获取数据的位置(我使用 20、30 作为示例)
关于excel - 将 IF else 与 LEFT 组合以隐藏列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1636598/