[英]VBA: Copy cell in worksheet1 to worksheet2 based on value of cell
使用VBA,我如何:
仅当单元格A1(在工作表1中)的值=“ YES”时,才将工作表1中的单元格B1复制到新的工作表2。
然后,对范围A1:A1000中的每一行重复此操作(即,基于A2值的B2复制=“ YES”,依此类推)-如果单元格为空,请检查下一行。
Sub Output()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Set Source = ActiveWorkbook.Worksheets("worksheet1")
Set Target = ActiveWorkbook.Worksheets("worksheet2")
j = 1 ' Start copying to row 1 in target sheet
For Each c In Source.Range("R2:R1000") ' Do 1000 rows
If c = "YES" Then
Source.Rows(c.Row).Copy Target.Rows(j) /*getting stuck here on trying to copy a cell from column S in worksheet1 to worksheet2
j = j + 1
End If
Next c
End Sub
您只需要进行快速修改即可从将行复制到单个单元格和位置进行更改。 我曾经使用过您是从c行的AG列复制过来的。
Sub Output()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Set Source = ActiveWorkbook.Worksheets("workshee1")
Set Target = ActiveWorkbook.Worksheets("workshee2")
j = 1 ' Start copying to row 1 in target sheet
For Each c In Source.Range("R2:R1000") ' Do 1000 rows
If c = "YES" Then
Target.Cells(j, "A").Value = Source.Cells(c.Row, "AG").Value
j = j + 1
End If
Next c
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.