excel 在结果从不同行(相同列)开始的情况下使用回车符拆分单元格,并且不删除现有公式

oknrviil  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(126)

下面的代码拆分然后转置数据,但目标单元格也是正在计算的单元格。因此,它将覆盖单元格中的公式。
我需要保持在单元格中的公式(S),并在不同的单元格(相同的列,不同的行)的结果开始。(见屏幕截图。)
详情:
我有一个来自系统的报表,其中单个单元格中包含无数个回车符(有些单元格包含2000多个回车符)。
我需要将单元格内容拆分为一个垂直列表以进行分析,但我需要列表从列中较低的单元格开始。
包含需要拆分的回车内容的单元格区域:"h2:aa2"。
开始创建列表的目标单元格:"H8:aa8"
代码将覆盖单元格中的公式。
1.如何将目标单元格语言添加到代码中?
1.如何删除目标单元格中的空行?
即在结果之间有一个额外的回车。见屏幕截图。
我可以稍后使用独特的公式来做这件事,但我很希望不必这样做。
代码:

Sub Splitcelldatawithcarriagereturnformultiplecolumns()
'VBA code to split out cell that has countless data with carriage returns
'Separates on carriage return, then transposes data. Result = vertical list"
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
Set WorkRng = Range("h2:aw2")
For Each Rng In WorkRng
    lLFs = VBA.Len(Rng) - VBA.Len(VBA.Replace(Rng, vbLf, ""))
    If lLFs > 0 Then
        Rng.Offset(1, 0).Resize(lLFs).Insert shift:=xlShiftDown
        Rng.Resize(lLFs + 1).Value = Application.WorksheetFunction.Transpose(VBA.Split(Rng, vbLf))
    End If
Next
End Sub

我失败的地方:在For Each Rng In WorkRng部分拆分和转置数据时,在无数位置都标识了Rng。我尝试将Rng替换为"H8:AA8",但不起作用。
我认为我需要定义目标区域,然后分配它(我也发现了许多不同的方法来删除回车,但它们似乎更麻烦(我不确定这是否是最有效的代码,但它确实有效)。

iklwldmw

iklwldmw1#

**EDIT:**添加了工作表循环和标签名检查,并清除先前的输出

你可以这样做:

Option Explicit

Sub SplitCells()

    Dim c As Range, ws As Worksheet, v, arr, arr2, i As Long, x As Long, el
    Dim cOut As Range, wb As Workbook
    
    Set wb = ActiveWorkbook 'or ThisWorkbook
    
    For Each ws In wb.Worksheets                       '##loop all worksheets
        If LCase(ws.Name) Like "*exceptions*" Then     '##tabname contains "exceptions" ?
            For Each c In ws.Range("h2:aw2").Cells     'loop over range in row2
                
                Set cOut = c.EntireColumn.Cells(8)     'output starts here
                cOut.Resize(1000).ClearContents        '##clear any previous data
                
                v = Trim(c.Value)                      'remove any spaces
                If Len(v) > 0 Then                     'any content?
                    arr = Split(v, vbLf)                       'split on vbLf
                    ReDim arr2(LBound(arr) To UBound(arr))     'for consolidated array
                    x = LBound(arr2)
                    For i = LBound(arr) To UBound(arr)
                        el = Trim(arr(i))
                        If Len(el) > 0 Then  'non-blank?
                            arr2(x) = el     'add to consolidated array
                            x = x + 1        'next position
                        End If
                    Next i
                    'drop the array onto the sheet below the cell being processed
                    If x > LBound(arr) Then 'EDIT: added this check
                        cOut.Resize(x).Value = Application.Transpose(arr2)
                    End If
                End If
            Next c
        End If      'tab name contains "exceptions"
    Next ws
End Sub

你的第二个场景:

Sub SplitCells()

    Dim c As Range, ws As Worksheet, rwData As Range
    Dim wb As Workbook, wsSumm As Worksheet, cOut As Range
    Dim arrJur, arrG1, arrG2, fileName, el
    
    Set wb = ActiveWorkbook                'or ThisWorkbook
    Set wsSumm = wb.Worksheets("Summary")  'summary sheet
    Set cOut = wsSumm.Range("A2")          'output starts here
    
    For Each ws In wb.Worksheets                       '##loop all worksheets
        If LCase(ws.Name) Like "*exceptions*" Then     '##tabname contains "exceptions" ?
            fileName = ws.Range("A1").Value
            Set rwData = ws.Range("A9:C9")
            Do While Application.CountA(rwData) > 0    'while have any data
                arrJur = CellValues(rwData.Cells(1))
                If Not IsEmpty(arrJur) Then            'any Jurisdictions?
                    arrG1 = CellValues(rwData.Cells(2))
                    arrG2 = CellValues(rwData.Cells(3))
                    For Each el In arrJur
                        cOut.Value = fileName               'file name
                        cOut.Offset(0, 1).Value = el        'jurisdiction
                        PutValues cOut.Offset(0, 2), arrG1  'group1
                        PutValues cOut.Offset(0, 27), arrG2 'group2
                        Set cOut = cOut.Offset(1) 'next output row
                    Next el
                
                End If
                Set rwData = rwData.Offset(1) 'next data row
            Loop
        End If      'tab name contains "exceptions"
    Next ws
End Sub

'If `arr` is not Empty, place it into a row starting at `c`
Sub PutValues(c As Range, arr)
    If Not IsEmpty(arr) Then
        c.Resize(1, UBound(arr) + 1).Value = arr
    End If
End Sub

'return an array of vbLf-separated non-blank values in a cell
Function CellValues(c As Range)
    Dim v As String, arr, col As New Collection, el
    v = Trim(c.Value)                      'remove any spaces
    If Len(v) > 0 Then                     'any content?
        arr = Split(v, vbLf)                       'split on vbLf
        For Each el In arr
            el = Trim(el)
            If Len(el) > 0 Then col.Add el 'non-blank?
        Next el
    End If
    CellValues = ColToArray(col)
End Function

'load a Collection to a 1D array
Function ColToArray(col As Collection)
    Dim i, arr
    If col.Count > 0 Then
        ReDim arr(0 To col.Count - 1)
        For i = 1 To col.Count
            arr(i - 1) = col(i)
        Next i
        ColToArray = arr
    Else
        ColToArray = Empty
    End If
End Function

相关问题