【问题标题】:Insert a row n times插入一行 n 次
【发布时间】:2020-12-06 21:46:24
【问题描述】:

我有一个包含 10 列的 Excel 文件。在第 2、3、4 列中,我有一个数字或破折号。
如果这 3 个单元格的总和大于 1,我需要将整行替换为 n 行,其中只有一列的值为 1,但其他单元格保持不变。

Example
1 - -  #-> leave it as is
- 2 -  #-> replace that row with 2 rows : - 1 - ; - 1 -
2 - 1  #-> replace that row with 3 rows : 1 - - ; 1 - - ; - - 1;

我设法自下而上进行迭代,但我无法在内存中存储一​​行、对其进行操作并在下方插入。

Sub Test()      
    Dim rng As Range
    Dim count20, count40, count45, total, i As Integer
    
    Set rng = Range("A3", Range("A3").End(xlDown))
        
    For i = rng.Cells.count To 1 Step -1
        count20 = 0
        count40 = 0
        count45 = 0
        total = 0
            
        count20 = Cells(rng.Item(i).Row, 10).Value
        If count20 > 1 Then
            total = total + count20
        End If
            
        count40 = Cells(rng.Item(i).Row, 11).Value
        If count40 > 1 Then
            total = total + count40
        End If
            
        count45 = Cells(rng.Item(i).Row, 12).Value
        If count45 > 1 Then
            total = total + count45
        End If
            
        If total <> 0 Then
            MsgBox total
        End If
     
    Next i     
End Sub

【问题讨论】:

  • 在第三个示例中,您说需要三行,但您的代码只会找到两行。你能澄清一下吗?我的意思是,在这个例子中 Count20 是 2 并且将被添加到总数中,但 count45 是 1 并且不会被添加。
  • 不要相信我的代码...我应该数 3。

标签: excel vba


【解决方案1】:

编辑 2

我已根据您的最新评论提供了替代代码。它使用列 J-L (10-12) 作为要更改的数字单元格,并使用列 A-I (1-9) 和 M-AD (13-30) 作为要保留文本的单元格。和以前一样,假设从第 3 行开始的工作表 1,您可以将其更改为您需要的任何内容。

Option Explicit
Sub testJtoL()
Dim LastRow As Long, i As Long, j As Long, c As Long, _
insertR As Long, TopRow As Long, BottomRow As Long
Dim b As Range
Dim ws As Worksheet

'*** This code is based your values being in Columns J-L (10-12) in sheet 1 ***

'Set sheet 1 as ws
Set ws = Sheet1

'Sheet1 column J is used here to get your last row
LastRow = ws.Cells(Rows.Count, 10).End(xlUp).Row

'*** This code is based your values starting in Row 3 ***
For c = LastRow To 3 Step -1
    'Determine number of rows to insert based on sum of that row
    insertR = Application.WorksheetFunction.Sum(Range(Cells(c, 10), Cells(c, 12))) - 1
    If insertR = 0 Then GoTo skip
    
    'STEP 1 insert the correct number of rows
    With ws.Range(Cells(c + 1, 1), Cells(c + insertR, 30))
        .Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End With
    
    'STEP 2 fill the values into the correct number of rows
    insertR = insertR + 1
    With ws.Range(Cells(c, 1), Cells(c, 30))
        .Resize(insertR, 30).Value = .Value
    End With
    
    TopRow = c
    If insertR = 0 And c = 3 Then
        BottomRow = c
            Else
            BottomRow = c + insertR - 1
    End If
    
    'STEP 3 replace all numbers with 1 or "-"
    'Replace numbers in column J
    If ws.Range(Cells(c, 10), Cells(c, 10)).Value = "-" Then GoTo SkipA
    i = ws.Range(Cells(c, 10), Cells(c, 10)).Value
    j = 1
    
    For Each b In ws.Range(Cells(TopRow, 10), Cells(BottomRow, 10))
    
    If j <= i Then
        b.Value = 1
        b.Offset(0, 1).Value = "-"
        b.Offset(0, 2).Value = "-"
            Else
            b.Value = "-"
    End If
    j = j + 1
    
    Next b
    
SkipA:
    'Replace numbers in column K
    j = 1
    For Each b In ws.Range(Cells(TopRow, 11), Cells(BottomRow, 11))
    If b.Value = "-" Then GoTo SkipB
    i = b.Value
    
    If j <= i Then
        b.Value = 1
        b.Offset(0, 1).Value = "-"
            Else
            b.Value = "-"
    End If
    j = j + 1
        
SkipB:
    Next b
    
    'Replace numbers in column L
    j = 1
    For Each b In ws.Range(Cells(TopRow, 12), Cells(BottomRow, 12))
    If b.Value = "-" Then GoTo SkipC
    i = b.Value
    
    If j <= i Then
        b.Value = 1
            Else
            b.Value = "-"
    End If
    j = j + 1
    
SkipC:
    Next b
    
skip:
Next c

End Sub

【讨论】:

  • 感谢大家花时间解决这个问题。在我工作的地方,我得到包含 20 列和多行订单的 Excel 报告。每行特别是 3 个单元格,分别代表 TypeA、TypeB 和 TypeC 对象的数量。至少一个单元格是 1,但我可以让所有 3 个单元格的数字达到 999。要导入另一个软件,我只需要 1 个类型,每行只有 1 个单元。例如:如果第一行是 1 ; 1; 999 ,输出将是 1001 行 1001 行的相同文本。 = 1x[ 1 ; - ;-] + 1x [- ; 1; -] + 999x [- ; - ; 1],保持每行左侧和右侧的其他单元格。重复...
  • 代码已修改 - 请告诉我进展如何。
  • kevin999,当我运行你的代码时,我哭了。完美,cmets 对理解也有很大帮助!我将在第 10、11 和 12 列中运行此代码,您能否告诉我如何在每次重复时复制/保留列 [1 到 9] 和 [13 - 30] 的原始行上的相同值。如果我将第一行重复 10 次,则在新行的左侧和右侧保留相同的文本。再次非常感谢您。
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 2016-08-13
  • 2011-10-17
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多