简体   繁体   English

使用 VBA 宏为每行添加 5 行

[英]Add 5 rows for each row using VBA Macros

Hi All i have two worksheets from Sheet1 i am copying to Sheet2 which copy data to each row once now i want my data should be look like "To be Version": First column will be repeated 5 time and other column should be only once i am trying but it does not working..大家好,我有两个工作表来自 Sheet1 我正在复制到 Sheet2 将数据复制到每一行现在我希望我的数据应该看起来像“成为版本”:第一列将重复 5 次,其他列应该只有一次我我正在尝试,但它不起作用..

I am using Macros to do this我正在使用宏来执行此操作

    Function getLastRow(targetSheet As Worksheet, colLetter As String) As Integer
        Dim lastRow As Integer
        With targetSheet
            getLastRow = .Cells(.Rows.count, colLetter).End(xlUp).Row
        End With
    End Function
    
    Function getColumn(targetSheet As Worksheet, FindWord As String, Optional iRow As Integer = 1) As Integer
        Dim iCol As Integer
        Dim tmpString As String
        For iCol = 1 To getLastColumn(targetSheet, 2)
            'targetSheet.Activate
            tmpString = VBA.Replace(targetSheet.Cells(iRow, iCol).Value, "", "")
            If VBA.InStr(1, VBA.LCase(tmpString), VBA.Replace(VBA.LCase(FindWord), "", "")) Then
                getColumn = iCol
                Exit Function
            End If
        Next iCol
        
    End Function
    
Sub ProcFile()
Dim wsRaw As Worksheet: Set wsRaw = ThisWorkbook.Sheets("Sheet1")
Dim wsAR As Worksheet: Set wsAR = ThisWorkbook.Sheets("Sheet2")
Dim iRow, x, LRow, sRow, col As Long
Dim Tes1, Test2, Test3 As String

sRow = getLastRow(wsAR, "E") + 1
LRow = getLastRow(wsRaw, "A")

If wsRaw.Range("A2").Value = "" Then MsgBox "Raw Data tab is Empty!!", vbCritical: Exit Sub

For x = 2 To LRow
        
        Tes1 = wsRaw.Cells(x, getColumn(wsRaw, "Tes1")).Value
        Test2 = wsRaw.Cells(x, getColumn(wsRaw, "Test2")).Value
        Test3 = wsRaw.Cells(x, getColumn(wsRaw, "Test3")).Value
        
       
        
            For col = 3 To 45 Step 2
                If wsRaw.Cells(x, col).Value <> "" Then

                    wsAR.Range("A" & sRow).Value = Tes1
                    wsAR.Range("B" & sRow).Value = Test2
                    wsAR.Range("C" & sRow).Value = Test3
        
                    
                    
                    End If
               
                
                
            Next col
           sRow = sRow + 1
        
Next x

MsgBox "Done!!"

End Sub

Sheet 1 Raw Data表 1 原始数据在此处输入图像描述

Sheet 2 Data in after macro excution表 2 宏执行后的数据在此处输入图像描述

The Data i am getting in my Macro excution:我在宏执行中获得的数据:

在此处输入图像描述

Repeat Rows重复行

Quick Help (Not tested)快速帮助(未测试)

Replace the line换行

sRow = sRow + 1

with

sRow = sRow + 5

Instead of the lines:而不是行:

wsAR.Range("A" & sRow).Value = Tes1
wsAR.Range("B" & sRow).Value = Test2
wsAR.Range("C" & sRow).Value = Test3

use the following:使用以下内容:

wsAR.Range("B" & sRow).Value = Test2
wsAR.Range("C" & sRow).Value = Test3
For sRow = sRow To sRow + 4
    wsAR.Range("A" & sRow).Value = Test1
Next sRow
sRow = sRow - 5

Initial Answer初步答案

  • The following will overwrite a range in the following way:以下将以下列方式覆盖范围:

    • The first row (the headers) stays the same.第一行(标题)保持不变。
    • Every value in the first column will be written five times, one below the other.第一列中的每个值将被写入五次,一个在另一个之下。
    • The remaining columns will be written only once leaving four empty cells below.其余列将仅写入一次,并在下面留下四个空单元格。
Option Explicit

Sub repeatRows()
    
    Const wsName As String = "Sheet2"
    Const RowsCount As Long = 4
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim rg As Range: Set rg = wb.Worksheets(wsName).Range("A1").CurrentRegion
    
    Dim Data As Variant: Data = rg.Value
    
    Dim srCount As Long: srCount = UBound(Data, 1)
    Dim cCount As Long: cCount = UBound(Data, 2)
    Dim drCount As Long: drCount = (srCount - 1) * (RowsCount + 1) + 1
    
    Dim Result As Variant: ReDim Result(1 To drCount, 1 To cCount)
    
    Dim c As Long
    
    For c = 1 To cCount
        Result(1, c) = Data(1, c)
    Next c
    
    Dim n As Long: n = 1
    Dim r As Long, i As Long
    
    For r = 2 To srCount
        n = n + 1
        For c = 1 To cCount
            Result(n, c) = Data(r, c)
        Next c
        For i = 1 To RowsCount
            n = n + 1
            Result(n, 1) = Data(r, 1)
        Next i
    Next r
            
    rg.Resize(n).Value = Result
    
End Sub

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

相关问题 在Excel中使用VBA宏为每行创建多个图表 - Create mulitple charts for each row using VBA Macros in Excel 使用Excel中的VBA宏为每行创建新图表 - Create a new chart for each row using VBA Macros in Excel VBA - Excel 通过一列中每一行的宏 - VBA - Excel through macros for each row in a column 每次我运行VBA的“添加/删除行”宏时,其运行速度都会逐渐变慢 - VBA Add/Remove Row macros runs progressively slower each time I run them 对于Excel宏VBA中的每个循环跳转备用行 - for each loop in excel macros vba jumping alternate rows 我将数据附加到表中,列 A - I,大约。 每次200行。 我想将日期添加到每一行的 col J 中,对于每个 append,使用 VBA - I am appending data to a table, cols A - I, approx. 200 rows each time. I want to add the date into col J for each row, for each append, using VBA Excel VBA宏使用复选框隐藏行-如果其他语句 - Excel VBA Macros Using Checkboxes To Hide Rows - If Else Statements 在Excel中使用VBA宏进行行选择和放置边框 - Row selection and putting border using VBA macros in excel 如何使用 VBA/宏根据行内容将某些输入行转换为多个新行? - How do can I turn some rows of input into multiple new rows depending row contents with VBA/macros? Excel VBA添加表单按钮到每一行 - excel vba add form button to each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM