有没有一种方法可以在有回车符的地方拆分和复制行?

时间:2019-01-28 16:56:13

标签: vba

我有一个包含很多列和行的电子表格。在这些列中,有一些单元格已使用回车符组合了数据, 最上方是数据的屏幕快照,第一行在代码列中有2个回车,备份名称有1行2行在代码列中有回车 我希望将其更改为下面的表格

enter image description here

下面的代码将单元格拆分并粘贴到最后。然后,我将原始数据+ 1列移到新的工作簿中。将拆分后的值粘贴回正确的列中。然后将其粘贴到新的工作簿中。 重复直到其他列消失

    Sub SplitCarriageReturnsCostCodes()


    Sheets("Booking Details").Select

'This will cells where there is a carriage return

    Dim splitVals As Variant
    Dim totalVals As Long
    Dim CellstoSplit As Range
    Dim LastRow As Long          'Makes the last row check the last cell with data in'


    LastRow = Worksheets("Booking Details").Cells(Rows.Count, 5).End(xlUp).Row

    'Set range to split:
    Set CellstoSplit = Range("AG2", "AG2" & LastRow)

    For Each cell In CellstoSplit
    cell.Activate
    splitVals = Split(ActiveCell.Value, Chr(10))
    totalVals = UBound(splitVals)
    Range(Cells(ActiveCell.Row, ActiveCell.Column + 17), Cells(ActiveCell.Row, ActiveCell.Column + 17 + totalVals)).Value = splitVals
    'Pastes them on the end, from range of cell selected plus a number of cells count
    Next cell

End Sub

Sub CreateTabCostCodes()

    ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)   'Adding sheet after all tabs
    ActiveWorkbook.Sheets(Worksheets.Count).Name = "Temp Sheet 1"     'Gives new sheet a name

    ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)   'Adding sheet after all tabs
    ActiveWorkbook.Sheets(Worksheets.Count).Name = "Booking Details New"     'Gives new sheet a name


End Sub

Sub CopyandPasteCostCodes()
'
        'Dim I As Integer                            'Set I name as a variable
        'I = 1                                       'I = 1
        'Do While I <= 100


        Dim ColCount As Integer                     'Sets column count as an integer
        Dim I As Integer                            'Set I as interget
        intCol = ThisWorkbook.Sheets("Booking Details").UsedRange.Columns.Count
                                                    'counts column in my range I just created
        ColCount = intCol - 49                      'column count minus the pre existing columns
        Do While I <= ColCount                      'do while I is equal to or less than column count

        Dim LastRow As Long                     'Makes the last row check the last cell with data in'
        LastRow = Sheet1.Cells(Rows.Count, 5).End(xlUp).Row ' last row in column E = 5, count columns as per a Vlookup'

        Sheets("Booking Details").Select
        Range("A2", "AX2" & LastRow).Select
        Selection.Copy
        Sheets("Temp Sheet 1").Select
        Range("A1").Select
        ActiveSheet.Paste
        Range("AX1", "AX1" & LastRow).Select
        Application.CutCopyMode = False
        Selection.Cut
        Range("AG1").Select
        ActiveSheet.Paste
        Range("A1", "AW1" & LastRow).Select
        Selection.Cut
        Sheets("Booking Details New").Select
        Range("A100000").Select              'Find empty cell at bottom
        Selection.End(xlUp).Select          'Goes from bottom till it find data, xlup = go up'
        ActiveCell.Offset(1, 0).Select      'In this 1,0 then 1 is rows to offset, pastes below the copied data'.
                                            '0 is columns.
        ActiveSheet.Paste

    'Deletes column AW and moves the data along so I can pick it up again

        Sheets("Booking Details").Select
        'Columns("AW:AW").Select               'Before I fixed notes column
        Columns("AX:AX").Select
        Selection.Delete Shift:=xlToLeft

        I = I + 1                   'Add 1 to I up to my number above
    Loop

    Sheets("Booking Details New").Select
    Range("Z2").Select
    Selection.Copy
    Columns("AG:Ag").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

End Sub

Sub Copy_HeadersCostCodes()
'
' Copies headers from booking details to new tab

    Sheets("Booking Details").Select
    Rows("1:1").Select
    Range("AP1").Activate
    Selection.Copy
    Sheets("Booking Details New").Select
    Rows("1:1").Select
    ActiveSheet.Paste

    'Fixes a header that formats badly

    Sheets("Booking Details New").Select
    Range("Z1").Select
    Selection.Copy
    Range("AG1").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

End Sub


Public Sub DeleteBlankRowsCostCodes()

'This deletes any row where there is a blank in column AA in the Bookings new tab'

    Dim lLRow As Long
    With Worksheets("Booking Details New") 'Select temp sheet tab'
     lLRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Looks from top to end of data in first column, hence rows.count, 1'
    .Range("AG:AG").AutoFilter Field:=1, Criteria1:=""
    .Range("AG2:AG" & lLRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
     .AutoFilterMode = False
     End With

End Sub

它可以工作,但手感沉重。对于VBA来说仍然是新手,随时随地学习

0 个答案:

没有答案