答案 0 :(得分:0)
就像快速搅打VBA子程序一样,你可以使用:
Sub frenchToast()
Dim rngData As Range, rngRow As Range, rngFound As Range
Dim frenchCol As Integer, startCol As Integer, writeCol As Integer, totalCols As Integer
'This is the range containing your french toast phrases
Set rngData = Sheet1.Range("A1:D5")
'get the total number of columns from this range. We will use to determine how
' far out we need to make the "French toast" column on sheet2
totalCol = rngData.Columns.Count
'Loop through each row in the range
For Each rngRow In rngData.Rows
'Where's my french toast?
Set rngFound = rngRow.Find("French Toast")
'only continue if you found some french toast
If Not (rngFound Is Nothing) Then
'Determine which column the french toast is in
frenchCol = rngFound.Column - rngRow.Cells(1).Column + 1
'determine which column we need to start writing the sentence
' for this row
startCol = (totalCol + 1) - frenchCol
'Loop through the columns in this sentence and write them out
' to sheet2
For writeCol = startCol To totalCol + 5
Sheet2.Cells(rngRow.Row, writeCol).Value = rngRow.Cells(1, writeCol - startCol + 1).Value
Next
End If
Next rngRow
End Sub
我通常不会完全写下这个问题,因为它不是真正符合StackOverflow的精神,但我喜欢法式吐司,而且如何在新记录中创造价值排列的问题很有意思,尤其是当你考虑到你必须把它写到一个新的范围,可以容纳转移开始和记录的位置。
无论如何,您必须更改Set rngData = Sheet1.Range("A1:D5")
行以适合您的数据,并且您可能希望更改它写入sheet2
的底部附近的位,但除此之外应该确定最好的列,开始将每一行写入新工作表,以便它不会用完列。
我已经对代码中的鼻涕进行了评论,所以它应该有点自我解释。列确定数学有点棘手,但是如果你遵循它就会看到它正在做什么。