VBA:"之后分裂细胞:"最后6行

时间:2016-07-12 15:40:07

标签: vba

到目前为止,我已经在同一列中管理了6个分裂单元格(与冒号一致)。现在我正在尝试将其标准化,以便在我上面有额外的数据时(例如,如果最后一行= 50,那么我的第一行是44)。

我的代码如下:

        Dim fullstring As String, colonposition As Integer, j As Integer
        For i=1 to 6
        fullstring = Cells(j, 1).Value
        colonposition = InStr(fullstring, ":")
        Cells(j, 2).Value = Mid(fullstring, colonposition + 2)
        Cells(j, 1).Value = Left(fullstring, colonposition - 1)
        Next j

我也试过这个(但没有成功)

            Dim fullstring As String, colonposition As Integer, j As Integer, LastRow as Long, FirstRow as Long
            LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
            FirstRow = LastRow - 6
            For j = FirstRow To EndRow
            fullstring = Cells(j, 1).Value
            colonposition = InStr(fullstring, ":")
            Cells(j, 2).Value = Mid(fullstring, colonposition + 2)
            Cells(j, 1).Value = Left(fullstring, colonposition - 1)
            Next

关于如何让VBA选择最后一行和第一行=最后一行 - 6的任何建议?

1 个答案:

答案 0 :(得分:1)

Sub test()

    Dim rng         As Range
    Dim rngCell     As Range
    Dim lCtr        As Long
    Dim vArr

    Set rng = Sheet1.UsedRange.Columns(1)

    '/ UnComment the comments if you have to set Row limits e.g 6 from last.
    '/ Otherwise this code will work on any range.
'
'    If rng.Rows.Count > 7 Then
'        Set rng = rng.Offset(rng.Rows.Count - 7).Resize(7)

        For Each rngCell In rng.Cells
            vArr = Split(rngCell.Value2, ":")

            For lCtr = LBound(vArr) To UBound(vArr)
                rngCell.Offset(0, lCtr) = vArr(lCtr)
            Next
        Next
'    End If

End Sub