[英]VBA Copy and Paste Certain Columns of a sheet to specific columns on another sheet
So, I have two sheets, “Budget Setup” and “Summary”.所以,我有两张表,“预算设置”和“摘要”。 I need to use VBA to copy and paste CERTAIN columns (NOT entire row) of the Budget Setup to specific columns of the Summary sheet, based on one criterion.
我需要使用 VBA 根据一个标准将预算设置的某些列(不是整行)复制并粘贴到汇总表的特定列。
This is how the Budget Setup sheet looks like:这是预算设置表的样子:
And this is how my Summary sheet looks like for now (after running the VBA code I wrote):这就是我的摘要表现在的样子(在运行我编写的 VBA 代码之后):
So, if the value is “Yes” in Column A of Budget Setup sheet, I want to transfer the value in Column B of Budget Setup to Column A of Summary, Column C of Budget Setup to Column B of Summary, Column F of Budget Setup to Column C of Summary, and Column G of Budget Setup to Column H of Summary.所以,如果预算设置表A栏的值为“是”,我想把预算设置B栏的值转移到汇总A栏,预算设置C栏到汇总B栏,预算F栏设置到汇总的 C 列,将预算设置的 G 列设置到汇总的 H 列。
This code will do the trick:这段代码可以解决问题:
Sub PCAMMatching()
a = Worksheets("Budget Setup").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Budget Setup").Cells(i, 1).Value = "Yes" Then
Worksheets("Budget Setup").Cells(i, 2).Copy
Worksheets("Summary").Cells(i, 1).Select
ActiveSheet.Paste
End If
If Worksheets("Budget Setup").Cells(i, 1).Value = "Yes" Then
Worksheets("Budget Setup").Cells(i, 3).Copy
Worksheets("Summary").Cells(i, 2).Select
ActiveSheet.Paste
End If
If Worksheets("Budget Setup").Cells(i, 1).Value = "Yes" Then
Worksheets("Budget Setup").Cells(i, 6).Copy
Worksheets("Summary").Cells(i, 3).Select
ActiveSheet.Paste
End If
If Worksheets("Budget Setup").Cells(i, 1).Value = "Yes" Then
Worksheets("Budget Setup").Cells(i, 7).Copy
Worksheets("Summary").Cells(i, 8).Select
ActiveSheet.Paste
End If
Next
Application.CutCopyMode = False
End Sub
However, as you can see on my Summary sheet, this code is creating 3 blank rows because the top 3 rows on the Budget Setup sheet have a status of “No” in Column A. What I really want is, if the status is “No”, simply skip that row (instead of creating a blank row) and copy the rows with a status of “Yes” one by one to the Summary sheet.但是,正如您在我的摘要表中看到的那样,此代码正在创建 3 个空白行,因为“预算设置”表上的前 3 行在 A 列中的状态为“否”。我真正想要的是,如果状态为“否”,只需跳过该行(而不是创建一个空白行)并将状态为“是”的行一一复制到汇总表中。
So, ideally, I want my Summary sheet to look like this:因此,理想情况下,我希望我的摘要表如下所示:
Any help would be appreciated!任何帮助,将不胜感激!
Here is a basic copy_paste using AutoFilter
and SpecialCells(xlCellTypeVisible)
这是使用
AutoFilter
和SpecialCells(xlCellTypeVisible)
的基本 copy_paste
'Assign and set your variables
Dim ws1 As Worksheet, ws2 As Worksheet, lRow As Long
Set ws1 = ThisWorkbook.Sheets("Budget Setup")
Set ws2 = ThisWorkbook.Sheets("Summary")
lRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
With ws1
.Range("A1").AutoFilter Field:=1, Criteria1:="Yes" 'set your filter
'copy the visible cells in each column from row 2 and resize to the last row
'paste to the the cell you want your copied range to start in your second worksheet
.Range("C2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("B2")
.Range("F2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("C2")
.Range("H2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("H2")
.Range("A1").AutoFilter 'clear the filter
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.