简体   繁体   English

如果条件满足,Excel VBA 将数据复制到表中

[英]Excel VBA copy data into table if condition is met

I've created the following code, which in theory should copy certain cells from the active worksheet into a worksheet name "checklist - audit".我创建了以下代码,理论上应该将活动工作表中的某些单元格复制到工作表名称“清单 - 审计”中。 "Checklist-audit" contains a table and I only want it to copy and past if the value "KPI_Month" is already present in column A. “清单审计”包含一个表,如果 A 列中已经存在值“KPI_Month”,我只希望它复制和过去。

Please see code below:请看下面的代码:


SheetName = ActiveSheet.Select

KPI_Month = ActiveSheet.Range("N2").Value
KPI_QC_Score = ActiveSheet.Range("I10").Value
KPI_Score_Difference = ActiveSheet.Range("P1").Value
KPI_QC = ActiveSheet.Range("N11").Value
KPI_QC_Role = ActiveSheet.Range("N12").Value
KPI_Date_Stamp = Now()

Sheets("Checklist - Audit").Activate

lrow = Range("A1100").End(xlUp).Row + 1

Cells(lrow, 1).Activate

If Cells(lrow, 1) = KPI_Month Then

Cells(lrow, 5) = KPI_QC_Score
Cells(lrow, 6) = KPI_Score_Difference
Cells(lrow, 9) = KPI_QC
Cells(lrow, 10) = KPI_QC_Role
Cells(lrow, 11) = KPI_Date_Stamp

Else

MsgBox "Reviewer must submit first"


End If

My problem is that instead of copying and pasting it is automatically bringing up the message box.我的问题是它不是复制和粘贴而是自动显示消息框。 Also, if it is to copy and paste, it should do so on the same row as where the month has been found.此外,如果要复制和粘贴,则应在找到月份的同一行上进行。

Could someone please explain where the code is going wrong and direct me to a solution.有人可以解释代码出错的地方并指导我找到解决方案。

Many thanks in advance.提前谢谢了。

I really hope i got your intentions fully and I will assume that you have two sheets involved here.我真的希望我完全理解你的意图,我会假设你在这里有两张纸。

Sub test()

    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim foundRange As Range
    Dim lastRow As Long
    
    Set wsSource = ThisWorkbook.Sheets(1) 'or name of the sheet from where info is copied
    Set wsDest = ThisWorkbook.Sheets("Checklist - Audit")
    
    KPI_Month = wsSource.Range("N2").Value
    KPI_QC_Score = wsSource.Range("I10").Value
    KPI_Score_Difference = wsSource.Range("P1").Value
    KPI_QC = wsSource.Range("N11").Value
    KPI_QC_Role = wsSource.Range("N12").Value
    KPI_Date_Stamp = Now()
    
    lastRow = wsDest.Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    Set foundRange = wsDest.Range("A1:A1100").Find(What:=KPI_Month, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

        If Not foundRange Is Nothing Then
            wsDest.Cells(lastRow, 5) = KPI_QC_Score
            wsDest.Cells(lastRow, 6) = KPI_Score_Difference
            wsDest.Cells(lastRow, 9) = KPI_QC
            wsDest.Cells(lastRow, 10) = KPI_QC_Role
            wsDest.Cells(lastRow, 11) = KPI_Date_Stamp
        Else
            MsgBox "Reviewer must submit first"
        End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM