重新排序并重复某些文本列

时间:2016-05-03 15:09:29

标签: excel vba excel-vba

在对VBA列进行文本处理后,我的数据如下所示:

<form name="calc">
<input name="rezultat"/>
<input type="button" name="1" value="2" onclick="run2()"/>
<input type="button" name="1" value="3" onclick="run3()"/>
<input type="button" name="1" value="4" onclick="run4()"/>
<input type="button" name="1" value="5" onclick="run5()"/>
<input type="button" name="1" value="6" onclick="run6()"/>
<input type="button" name="1" value="7" onclick="run7()"/>
<input type="button" name="1" value="8" onclick="run8()"/>
<input type="button" name="1" value="9" onclick="run9()"/>
<input type="button" name="1" value="10" onclick="run10()"/>
</form>

运行其余代码后,我的数据如下所示:

1995 (1)
(23:00)

Math 0630 
0830 Break 0930 
1000 English 1200 
1200 Lunch 1300 
1330 Free

为了更容易理解“0630 MATH 0830”在一个单元格中,依此类推。

我现在的问题是我需要这样的数据:

1995 (1) (23:00) 0630 Math 0830 0930 Math Break 1000 1200 Break English 1200 1300 English Lunch 1300 1330 Lunch Free

所以基本上,如果有意义的话,需要移动类的时间。我的代码如下。任何类型的帮助将不胜感激。

1995 (1) (23:00) Math 0630 0830 Break 0930 1000 English 1200 1200 Lunch 1300 1330 Free

2 个答案:

答案 0 :(得分:0)

未经测试,但这应该可以解决问题:

Sub MoveIt()
    Dim LastRow as Long, RowCol as Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row

    For RowCol = 1 to LastRow
        Cells(1, RowCol).Value = Cells(RowCol, 1).Value
        Cells(RowCol, 1).Value = ""
    Next RowCol
End Sub

答案 1 :(得分:0)

如果我理解你的问题,那么你正在寻找这样的事情:

Option Explicit

Sub ReOrderAllDataInAllCells()

Dim rngCell As Range

For Each rngCell In Worksheets(1).Range("C5:G5")
    rngCell.Value2 = ReorderDataInCells(rngCell.Value2)
Next rngCell

End Sub


Public Function ReorderDataInCells(strUnsorted As String) As String

Dim strTemp As String

strTemp = strUnsorted
strTemp = Replace(strTemp, Split(strUnsorted, " ")(0), "")
If IsNumeric(Split(strUnsorted, " ")(UBound(Split(strUnsorted, " ")))) Then
    strTemp = Replace(strTemp, Split(strUnsorted, " ")(UBound(Split(strUnsorted, " "))), "")
End If
strTemp = Trim(strTemp) & " " & Split(strUnsorted, " ")(0) & " "
If IsNumeric(Split(strUnsorted, " ")(UBound(Split(strUnsorted, " ")))) Then
    strTemp = strTemp & Split(strUnsorted, " ")(UBound(Split(strUnsorted, " ")))
End If
ReorderDataInCells = strTemp

End Function

当然,Worksheets(1).Range("C5:G5")必须调整到要转换的单元格的实际位置。