简体   繁体   English

需要帮助:将行复制到下面创建的许多行中(Excel VBA)

[英]Need Help: Copying Row Into Many Rows Created below (Excel VBA)

New user here who is also very new to Excel VB. 这里的新用户对Excel VB也是很新的。

At the moment, I have a macro which does what you see here. 目前,我有一个宏,它可以完成您在此处看到的内容。

Essentially, I have 2 columns which can sometimes have cells which contain vertically stacked lines of data in each cell. 本质上,我有2列,有时可以包含一些单元格,其中每个单元格中都包含垂直堆叠的数据行。 Each of those lines is split out and put into newly inserted rows below (one line of data in the cell per row). 这些行中的每一行都被拆分并放入下面新插入的行中(每行单元格中有一行数据)。

The problem I am having now, is that while the new rows now contain data in the two columns which had to be split (34 and 35), the remaining cells are empty. 我现在遇到的问题是,尽管新行现在在必须拆分的两列(34和35)中包含数据,但其余单元格为空。 I am having trouble bringing the remaining 38 columns down into the newly-created rows. 我在将其余38列放到新创建的行中时遇到麻烦。 You can see what I mean in the image I posted. 您可以在发布的图片中看到我的意思。 Two new rows were created and I need to fill them with the content of row 1 (fill in to the shaded area). 创建了两个新行,我需要用第1行的内容填充它们(填充到阴影区域)。

Here is my code right now. 这是我的代码。 The part that is commented out is me trying to fill the empty space. 被注释掉的部分是我试图填充空白处。 The un-commented code does what you see in the image. 未注释的代码执行您在图像中看到的内容。

Sub main()
Dim iRow As Long, nRows As Long, nData As Long
Dim IDVariables As Range
Dim arr As Variant


With Worksheets("UI").Columns("AH") 
    nRows = .Cells(.Rows.Count, 1).End(xlUp).Row 
    For iRow = nRows To 2 Step -1 
        With .Cells(iRow) 
            arr = Split(.Value, vbLf) 
            nData = UBound(arr) + 1 
            If nData > 1 Then 
                    .EntireRow.Offset(1).Resize(nData - 1).Insert 
                    .Resize(nData).Value = Application.Transpose(arr) 
                    .Offset(, 1).Resize(nData).Value = Application.Transpose(Split(.Offset(, 1).Value, vbLf)) 
                    'Set IDVariables = Range("A" & iRow & ":AG" & iRow)
                    'IDVariables.Select
                    'Selection.Copy
                    'Range("A" & (iRow + 1) & ":A" & (iRow + nData -1)).Select
                    'Selection.Paste             
            End If
        End With
    Next iRow
End With

End Sub 结束子

Any help would be very much appreciated. 任何帮助将不胜感激。

Thanks! 谢谢!

Tested and working fine.... 经过测试,工作正常。


Option Explicit

Sub ReCode()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")

Dim LR As Long, i As Long, arr
LR = ws.Range("AH" & ws.Rows.Count).End(xlUp).Row

For i = LR To 2 Step -1
    If InStr(ws.Range("AH" & i), vbLf) Then
        ws.Range("A" & i + 1).EntireRow.Insert xlUp
            ws.Range("A" & i).EntireRow.Copy ws.Range("A" & i + 1)
            arr = Split(ws.Range("AH" & i), vbLf)
            ws.Range("AH" & i) = arr(0)
            ws.Range("AH" & i + 1) = arr(1)
        arr = ""
    End If
Next i

End Sub

I'm late doing this but I figured it out. 我迟到了,但是我想通了。 I'll post my solution for anyone who has a similar problem. 我会将解决方案发布给有类似问题的任何人。

Sub main()
Dim iRow As Long, nRows As Long, nData As Long
Dim arr As Variant
Dim IDVariables, Comments, AllocationCheck As Range

Application.ScreenUpdating = False

With Worksheets("PRM2_Computer").Columns("AH")
    nRows = .Cells(.Rows.Count, 1).End(xlUp).Row        
    For iRow = nRows To 2 Step -1
        With .Cells(iRow)
            arr = Split(.Value, vbLf)
            nData = UBound(arr) + 1
            If nData = 1 Then
                Range("AI" & iRow) = 1
                Range("AK" & iRow) = "Single Industry"
            End If
            If nData > 1 Then
                    .EntireRow.Offset(1).Resize(nData - 1).Insert
                    .Resize(nData).Value = Application.Transpose(arr)
                    .Offset(, 1).Resize(nData).Value = Application.Transpose(Split(.Offset(, 1).Value, vbLf))
                    .Offset(, 2).Resize(nData).Value = Application.Transpose(Split(.Offset(, 2).Value, vbLf))
                    Set Comments = Range("AL" & iRow & ":AM" & iRow)
                    Comments.Copy Range("AL" & (iRow + 1) & ":AL" & (iRow + nData - 1))
                    Set AllocationCheck = Range("AK" & (iRow) & ":AK" & (iRow + nData - 1))
                    AllocationCheck.Value = Application.Sum(Range("AI" & iRow & ":AI" & (iRow + nData - 1)))
                    Set IDVariables = Range("A" & iRow & ":AG" & iRow)
                    IDVariables.Copy Range("A" & (iRow + 1) & ":A" & (iRow + nData - 1))
            End If
        End With
    Next iRow
End With

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM