我已经为此发疯了一天,到处搜索,可能是我想变得太可爱,所以我完全陷入困境。
我正在尝试运行一个简单的 if then
如果一个单元格包含“%”,我希望它做一件事,如果不是另一件事。由于我不明白的原因,我无法让它发挥作用。我显然从其他地方借鉴了一些想法,但仍然无法使其发挥作用。
复杂的因素 - 我不想在整个列上运行它,只是在一个表上运行,因此它使用大量或相对的 ActiveCells 嵌入到更大的子中。我永远不知道 A 列中的哪个位置会遇到“% Change”,因此范围始终必须是可变的。我希望 VBA/VBE 在遇到带有“%”的单元格时执行不同的操作。所以
这是原始数据的样子
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
但是当我运行以下命令时,它陷入了一个错误的循环,它应该退出到“If then”而不是子程序的“Else”部分。
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
感谢所有帮助,谢谢!