我已经为此疯狂了一整天,四处搜寻,可能是因为想得太聪明了所以完全卡住了。
我试图运行一个简单的if then语句
如果单元格包含“%”,我想做一件事,如果没有则做另一件事。出于我不理解的原因,我无法使其正常工作。我显然从其他地方借鉴了一些想法,但仍然无法使其正常工作。
复杂因素-我不想在整个列上运行它,只想在表格中运行,因此它嵌入在较大的子程序中,使用许多相对ActiveCells。我永远不知道我将在A列中的哪个位置遇到“%变化”,因此范围必须始终是可变的。当它遇到带有“%”的单元格时,我希望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
非常感谢您的帮助!