[英]copy/paste range of cells x times based on condition
I want to fill each empty cells of a board with a precise range of data.我想用精确范围的数据填充电路板的每个空单元格。
I 've got two worksheets;我有两张工作表;
- worksheets("Board")
- worksheets("Board")
- worksheets("FinalBoard")
- worksheets("FinalBoard")
In worksheet worksheets("Board")
I've got the following board ;在工作worksheets("Board")
工作worksheets("Board")
我有以下板;
Category类别 | Fruits-1水果-1 | Fruits-2水果-2 | Fruits-3水果-3 |
---|---|---|---|
A一种 | Banana香蕉 | Cherries樱桃 | Orange橙色的 |
D D | Apple苹果 | Mango芒果 | Strawberries草莓 |
B乙 | Pineapple菠萝 | Watermelon西瓜 | Grenade手榴弹 |
I want to pick each columns data only if the header starts with "Fruits" and paste them in one colum in worksheet worksheets("FinalBoard")
.仅当标题以“Fruits”开头并将它们粘贴到工作worksheets("FinalBoard")
一个列中时,我才想选择每列数据。 I was able to do so with columns named Fruits, with the following code;我可以使用名为 Fruits 的列执行此操作,代码如下;
Sub P_Fruits()
Dim wsInput As Worksheet
Dim wsOutput As Worksheet
Dim lRowInput As Long
Dim lRowOutput As Long
Dim lCol As Long
Dim i As Long
Dim n As Long
Dim s As String
Dim col As String
'~~> Sheets settings
Set wsInput = Sheets("Board")
Set wsOutput = Sheets("FinalBoard")
With wsInput
'~~> Find last column in Row 2
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> loop through columns
For i = 1 To lCol
'~~> research criterias
If .Cells(1, i).Value2 Like "Fruit-*" Then
'~~> Get columns name
col = Split(.Cells(, i).Address, "$")(1)
'~~> Get the last row in that column
lRowInput = .range(col & .Rows.Count).End(xlUp).row
'~~> Find the next row to write to
If lRowOutput = 0 Then
lRowOutput = 2
Else
lRowOutput = wsOutput.range("B" & wsOutput.Rows.Count).End(xlUp).row + 1
End If
'~~> Copy-paste in the 2nd worksheet every data if the headers is found
.range(col & "2:" & col & lRowInput).Copy _
wsOutput.range("B" & lRowOutput)
End If
Next i
end with
end sub
however I'd like to do so for the column "category" and put the category's type in front of each fruits in column A and thus repeat the copied range category multiple time , as much as there were headers beginning with "Fruits" in worksheets("Board")
.但是我想为“类别”列这样做,并将类别的类型放在 A 列中每个水果的前面,从而多次重复复制的范围类别,就像worksheets("Board")
以“水果”开头的标题一样多worksheets("Board")
。 I tried to add an extra code to the previous one but it didnt work.我试图在前一个代码中添加一个额外的代码,但没有用。 Here is what I'd like as a result;这是我想要的结果;
Category-pasted分类粘贴 | Fruits-pasted果酱 |
---|---|
A一种 | Banana香蕉 |
D D | Apple苹果 |
B乙 | Pineapple菠萝 |
A一种 | Cherries樱桃 |
D D | Melon瓜 |
B乙 | Watermelon西瓜 |
A一种 | Orange橙色的 |
D D | Strawberries草莓 |
B乙 | Grenade手榴弹 |
Here is what I had with the code I added instead...这是我添加的代码的内容......
Category-pasted分类粘贴 | Fruits-pasted果酱 |
---|---|
Banana香蕉 | |
Apple苹果 | |
Pineapple菠萝 | |
Cherries樱桃 | |
Melon瓜 | |
Watermelon西瓜 | |
Orange橙色的 | |
Strawberries草莓 | |
Grenade手榴弹 | |
A一种 | |
D D | |
B乙 |
My finale code;我的结局代码;
Sub Fruits_add()
Dim wsInput As Worksheet
Dim wsOutput As Worksheet
Dim lRowInput As Long
Dim lRowOutput As Long
Dim lCol As Long
Dim i As Long
Dim n As Long
Dim s As String
Dim col As String
'~~> Sheets settings
Set wsInput = Sheets("Board")
Set wsOutput = Sheets("FinalBoard")
With wsInput
'~~> Find last column in Row 2
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> loop through columns
For i = 1 To lCol
'~~> research criterias
If .Cells(1, i).Value2 Like "Fruit-*" Then
'~~> Get column name
col = Split(.Cells(, i).Address, "$")(1)
'~~> Get the last row in that column
lRowInput = .range(col & .Rows.Count).End(xlUp).row
'~~> Find the next row to write to
If lRowOutput = 0 Then
lRowOutput = 2
Else
lRowOutput = wsOutput.range("B" & wsOutput.Rows.Count).End(xlUp).row + 1
End If
'~~> Copy-paste
.range(col & "2:" & col & lRowInput).Copy _
wsOutput.range("B" & lRowOutput)
End If
Next i
'Code to repeat category type added
With wsInput
'~~> Find last column in Row 2
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> loop through columns
For i = 1 To lCol
'~~> research criterias
If .Cells(1, i).Value2 Like "Category*" Then
'~~> Get column name
col = Split(.Cells(, i).Address, "$")(1)
'~~> Get the last row in that column
lRowInput = .range(col & .Rows.Count).End(xlUp).row
'~~> Find the next row to write to
If lRowOutput = 0 Then
lRowOutput = 2
Else
lRowOutput = wsOutput.range("A" & wsOutput.Rows.Count).End(xlUp).row + 1
End If
'~~> copy-paste each category type in column A
.range(col & "2:" & col & lRowInput).Copy _
wsOutput.range("A" & lRowOutput)
End If
Next i
End With
End With
I feel like I'm close to the solution.我觉得我已经接近解决方案了。 I'd appreciate your help guys, thank you!我很感激你们的帮助,谢谢!
This code will produce the required results but uses a different approach.此代码将产生所需的结果,但使用不同的方法。
The first thing it does is read the source data into an array, it then goes through that array and extracts the fruits/categories from every column with a header starting with 'Fruit.它做的第一件事是将源数据读入一个数组,然后遍历该数组并从每个列中提取水果/类别,标题以“Fruit”开头。
Option Explicit
Sub Fruits_add()
Dim wsInput As Worksheet
Dim wsOutput As Worksheet
Dim arrDataIn As Variant
Dim arrDataOut As Variant
Dim idxCol As Long
Dim idxRow As Long
Dim cnt As Long
'~~> Sheets settings
Set wsInput = Sheets("Board")
Set wsOutput = Sheets("FinalBoard")
' assumes data on 'Board' starts in A1
With wsInput
arrDataIn = .Range("A1").CurrentRegion.Value
End With
ReDim arrDataOut(1 To 2, 1 To UBound(arrDataIn, 1) * UBound(arrDataIn, 2))
For idxCol = LBound(arrDataIn, 2) To UBound(arrDataIn, 2)
If arrDataIn(1, idxCol) Like "Fruits*" Then
For idxRow = LBound(arrDataIn, 1) + 1 To UBound(arrDataIn, 1)
cnt = cnt + 1
arrDataOut(1, cnt) = arrDataIn(idxRow, 1)
arrDataOut(2, cnt) = arrDataIn(idxRow, idxCol)
Next idxRow
End If
Next idxCol
If cnt > 0 Then
ReDim Preserve arrDataOut(1 To 2, 1 To cnt)
End If
With wsOutput
.Range("A1:B1").Value = Array("Category-pasted", "Fruit-pasted")
.Range("A2").Resize(cnt, 2) = Application.Transpose(arrDataOut)
End With
End Sub
As I explained in my comments you don't need the second loop if you already found the correct row - get the category column early and reuse it later正如我在评论中所解释的,如果您已经找到正确的行,则不需要第二个循环 - 尽早获取类别列并稍后重用
You can add this variable declaration at the top first您可以先在顶部添加此变量声明
Dim col As String
Then continue with your code for first loop (deleting second loop然后继续执行第一个循环的代码(删除第二个循环
With wsInput
'~~> Find last column in Row 2
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> loop through columns
For i = 1 To lCol
Add this to retrieve categories first添加此项以首先检索类别
If .Cells(1, i).Value2 Like "Category*" Then
'~~> Get column name
colCat = Split(.Cells(, i).Address, "$")(1)
End If
'~~> research criterias
If .Cells(1, i).Value2 Like "Fruit-*" Then
'~~> Get column name
col = Split(.Cells(, i).Address, "$")(1)
'~~> Get the last row in that column
lRowInput = .range(col & .Rows.Count).End(xlUp).row
'~~> Find the next row to write to
If lRowOutput = 0 Then
lRowOutput = 2
Else
lRowOutput = wsOutput.range("B" & wsOutput.Rows.Count).End(xlUp).row + 1
End If
'~~> Copy-paste
.range(col & "2:" & col & lRowInput).Copy _
wsOutput.range("B" & lRowOutput)
Then add this to paste the categories然后添加它以粘贴类别
'~~> copy-paste each category type in column A
.range(colCat & "2:" & colCat & lRowInput).Copy _
wsOutput.range("A" & lRowOutput)
End If
Next i
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.