Combinando IF mais com se esconder colunas
-
06-07-2019 - |
Pergunta
Eu estou tentando escrever algum código para esconder colunas se os 3 primeiros caracteres de células em um intervalo igual ao conteúdo de outro. Eu tenho o código para esconder colunas se células em um intervalo estão em branco como este; -
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
E o código para identifiying os primeiros 3 charcters de uma célula; -
Dim LResult As String
LResult = Left ("Alphabet",3)
Mas como posso combinar os dois, fazendo referência a uma célula específica em vez de "Alphabet"?
Cant chegar a este trabalho? - alguma sugestão
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
Felicidades
Solução
Você tem quase o código de trabalho. Você está comparando cell.Value para uma cadeia vazia - agora se aplica apenas à esquerda a ele
LResult = Left (cell.Value,3)
Editar:
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
onde você quer dados de célula da linha e col (eu usei 20, 30 como o exemplo)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow