[英]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 2 Data in after macro excution表 2 宏执行后的数据
The Data i am getting in my Macro excution:我在宏执行中获得的数据:
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:以下将以下列方式覆盖范围:
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.