Encontre uma string dentro de uma célula usando o VBA
Eu tenho me enlouquecido com isso por um dia, pesquisado alto e baixo, e provavelmente estou tentando ser muito fofo, então estou totalmente preso.
Eu estou tentando executar um simples se então
Se uma célula contiver "%", gostaria que fizesse uma coisa e, se não, outra. Por razões que não entendo, não consigo fazer com que funcione. Eu claramente peguei algumas idéias de outros lugares, mas ainda não consigo fazer funcionar.
Fatores de complicação - não quero executar isso em toda a coluna, apenas uma tabela, por isso ela é incorporada em um sub maior usando lotes ou ActiveCells relativos. Eu nunca sei onde na coluna A eu vou encontrar o "% Change", então o Range sempre tem que ser variável. Eu quero VBA / VBE para fazer algo diferente quando se trata de uma célula com o "%" nele. ASSIM
Veja como são os dados brutos
Initial Value (6/30/06)
Value (12/31/06)
Net Additions (9/30/07)
Withdrawal (12/07)
Value (12/31/07)
Withdrawal (2008)
Value (12/31/08)
Addition (8/26/09)
Value (12/31/09)
Value (12/31/10)
Value (12/30/11)
Value (3/31/12)
% Change 1st Quarter
% Change Since Inception
Mas quando eu corro o seguinte, ele fica preso em um loop ruim, no qual deveria ter puxado para o "If Then", em vez da parte "Else" do sub.
Sub IfTest()
'This should split the information in a table up into cells
Dim Splitter() As String
Dim LenValue As Integer 'Gives the number of characters in date string
Dim LeftValue As Integer 'One less than the LenValue to drop the ")"
Dim rng As Range, cell As Range
Set rng = ActiveCell
Do While ActiveCell.Value <> Empty
If InStr(rng, "%") = True Then
ActiveCell.Offset(0, 0).Select
Splitter = Split(ActiveCell.Value, "% Change")
ActiveCell.Offset(0, 10).Select
ActiveCell.Value = Splitter(1)
ActiveCell.Offset(0, -1).Select
ActiveCell.Value = "% Change"
ActiveCell.Offset(1, -9).Select
Else
ActiveCell.Offset(0, 0).Select
Splitter = Split(ActiveCell.Value, "(")
ActiveCell.Offset(0, 9).Select
ActiveCell.Value = Splitter(0)
ActiveCell.Offset(0, 1).Select
LenValue = Len(Splitter(1))
LeftValue = LenValue - 1
ActiveCell.Value = Left(Splitter(1), LeftValue)
ActiveCell.Offset(1, -10).Select
End If
Loop
End Sub
Toda ajuda é apreciada, obrigado!