[英]Copy and paste data based on criteria
我编写了一个宏代码,用于从一个工作表(Sheet2)复制数据并粘贴到另一工作表(Sheet1)。 这很好。 但是,我希望有一个条件来确定将数据粘贴到Sheet1中的位置。 条件是,如果在Sheet1的A列(已复制数据)的Sheet2的单元格A1中(从下拉列表中)选择的国家/地区,则使用最新数据更新(覆盖)该国家/地区数据,否则为新国家/地区,则应将新数据粘贴到Sheet1上最后一组数据下面。
这是我到目前为止编写的代码:
Private Sub CommandButton1_Click()
ActiveSheet.Range("A1:F44").Copy
With Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
.PasteSpecial xlPasteFormats
ActiveSheet.Range("A1:F44").Copy
With Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets("Sheet2").Activate
End With
End With
End Sub
任何帮助将不胜感激! 非常感谢!
您可以在A列上使用match
功能来检查您是否拥有该国家/地区。 如果匹配返回一个数字,则使用该数字替换国家/地区数据,否则将返回错误-捕获该错误并将数据添加到表格末尾。 我不确定从您编写的数据来看您的数据到底是什么样子,但这就是您通常想要做的。
一个一般的方法是:我正在使用match查找行值,并且使用iferror来确保我没有得到返回的错误。
country = Worksheets("Sheet1").Cells(1, "A").Value 'or where your drop down is
test = Application.WorksheetFunction.IfError(WorksheetFunction.Match(country, "A:A", 0), -1)
If test = -1 Then
'copy to end of sheet
Else
*with sheets(Sheet1).cells(rows.value, test).end(xlup).offset(1,0)*
'copy to the value in test - it's the row number of the country we are copying now
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.