[英]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.