我工作表上的每一行代表一次货币交易。每行的前 3 个单元格显示货币、日期和价格。之后的每 3 个单元格说明每个帐户在做什么。交易可以有任意数量的账户,因此行的长度不同。对于每笔交易(行),我希望每个账户都有自己独特的行,显示货币、日期、价格、买入/卖出、数量、账户。
**Example**
1 2 3 4 5 6 7 8 9 10 11 12 13
-------------------------------------------------------------------------------
EURUSD 1/1/13 1.30 Buy 100 acc1 Buy 1000 Acc2 Buy 100 acc3 Buy .....
EURUSD 2/1/13 1.31 Buy 1000 acc1 Buy 1000 Acc2 Buy 100 acc3 Buy .....
.
.
.
**WOULD BECOME**
EURUSD 1/1/13 1.30 Buy 100 acc1
EURUSD 1/1/13 1.30 Buy 1000 Acc2
EURUSD 1/1/13 1.30 Buy 100 Acc3
. . .
. . .
. . .
EURUSD 2/1/13 1.31 Buy 1000 acc1
EURUSD 2/1/13 1.31 Buy 1000 acc2
EURUSD 2/1/13 1.31 Buy 100 acc3
我已经编写了一些代码(如下),我希望用它来实现这一点,但我认为我在那里有一个无限循环。对于我从第 4 列开始的每一行,如果它是买入或卖出
我尝试了许多更改但同样的问题。我确定它正盯着我看,但我看不到我的错误。任何人都可以建议更正吗?顺便说一句,它不是最漂亮的代码,所以如果你有更好的解决方案,我会全力以赴。谢谢
Sub changeformat()
Dim p As Integer
Dim r As Integer
Dim c As Integer
p = 150
For r = 1 To 140
c = 4
Range("A" & r).Select
Do While ActiveCell.Value = """"
Do Until c = 303
Cells(r, c).Select
If InStr(ActiveCell.Value, "Buy") > 0 Or InStr(ActiveCell.Value,"Buy") > 0 Then
'The first 3 cells will of each new row will be the same as the first 3 cells
'of current active 'original' row
ActiveSheet.Cells(p, 1).Value = ActiveSheet.Cells(r, 1).Value
ActiveSheet.Cells(p, 2).Value = ActiveSheet.Cells(r, 2).Value
ActiveSheet.Cells(p, 3).Value = ActiveSheet.Cells(r, 3).Value
'The active cell and the 2 cells that follow will be pasted to
'columns D to F in row p
ActiveSheet.Cells(p, 4).Value = ActiveSheet.Cells(r, c).Value
ActiveSheet.Cells(p, 5).Value = ActiveSheet.Cells(r, c + 1).Value
ActiveSheet.Cells(p, 6).Value = ActiveSheet.Cells(r, c + 2).Value
p = p + 1
c = c + 3
End If
Loop
Loop
Next r
End Sub
编辑
我做了以下更改,无限循环似乎已经停止。它现在几乎完成了它应该做的事情,但它正在省略数据。例如,我工作表上的第一行只有 9 个单元格(2 个帐户)。第二个有 33 个(10 个帐户)。这 2 行应转换为 12 个帐户的 12 个新行。不幸的是,它只是复制每行中的第一个帐户。它对前 11 行执行此操作,然后对第 12 行和第 13 行执行此操作。对可能发生的情况有什么建议吗?谢谢
p = 150
For r = 1 To 140
If Range("A" & r).Value <> "" Then
'Do While Range("A" & r) <> """"
For c = 4 To 303
Cells(r, c).Select
If ActiveCell.Value <> "" Then
If InStr(ActiveCell.Value, "Buy") > 0 Or InStr(ActiveCell.Value, "Buy") > 0 Then
'The first 3 cells will of each new row will be the same as the first 3 cells
'of current active 'original' row
ActiveSheet.Cells(p, 1).Value = ActiveSheet.Cells(r, 1).Value
ActiveSheet.Cells(p, 2).Value = ActiveSheet.Cells(r, 2).Value
ActiveSheet.Cells(p, 3).Value = ActiveSheet.Cells(r, 3).Value
'The active cell and the 2 cells that follow will be pasted to
'columns D to F in row p
ActiveSheet.Cells(p, 4).Value = ActiveSheet.Cells(r, c).Value
ActiveSheet.Cells(p, 5).Value = ActiveSheet.Cells(r, c + 1).Value
ActiveSheet.Cells(p, 6).Value = ActiveSheet.Cells(r, c + 2).Value
p = p + 1
End If
End If
Next c
'Loop
End If
Next r