简体   繁体   English

VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏

[英]VBA Macro to copy/paste cell value from sheet 1 to 2 based on date

I have anchored a date and a value in Sheet 1 and want to look up that date in Sheet 2's column A, and paste the sheet 1 value in sheet 2's column E.我在工作表 1 中锚定了一个日期和一个值,想在工作表 2 的 A 列中查找该日期,并将工作表 1 的值粘贴到工作表 2 的 E 列中。

In sheet 1, I have today's name as REP_DATE, and the date I want to look for is yesterday's date, which is REP_DATE - 1 and I have had this formula in sheet1 E5, and the value is in F5.在工作表 1 中,我今天的名字是 REP_DATE,我要查找的日期是昨天的日期,即 REP_DATE - 1,我在工作表 1 E5 中有这个公式,值在 F5 中。

here is the code I have from online and I have made some adjustments:这是我从网上得到的代码,我做了一些调整:

Sub CopyData()
Dim myDate As Date
Dim myValue As Variant
myDate = ThisWorkbook.Sheets("Sheet1").Range("E6").Value
myValue = ThisWorkbook.Sheets("Sheet1").Range("F6").Value


ThisWorkbook.Sheets("Sheet2").Range("A:A").Find(myDate).Offset(0, 4).Value = myValue

End Sub

However, I'm keep getting an error "Run-time error '91': Object variable or With block variable not set", debug is referring to this line of code ThisWorkbook.Sheets("Sheet2").Range("A:A").Find(myDate).Offset(0, 4).Value = myValue但是,我不断收到错误“运行时错误‘91’:Object 变量或未设置块变量”,调试指的是这行代码ThisWorkbook.Sheets("Sheet2").Range("A:A").Find(myDate).Offset(0, 4).Value = myValue

It really confuses me and I'm wondering how to solve this issue.这真的让我很困惑,我想知道如何解决这个问题。

Thanks.谢谢。

You described that you have your date/value in row 5 (e5, f5) while in your code you're looking for date by row 6 (e6, f6).您描述了您在第 5 行(e5、f5)中有日期/值,而在您的代码中您正在第 6 行(e6、f6)中查找日期。

Error shows because the date is not found.错误显示是因为找不到日期。

There will be the same error in case your date is not exist in Sheet2.如果您的日期在 Sheet2 中不存在,则会出现相同的错误。

You can update cell adresses in your code.您可以更新代码中的单元格地址。 Additinally, you can add some error handling to let user know why code is not working corectlly in case date is not found;此外,您可以添加一些错误处理,让用户知道在找不到日期的情况下代码无法正常工作的原因;

Option Explicit

Sub CopyData()
Dim myDate As Date
Dim myValue As Variant
myDate = ThisWorkbook.Sheets("Sheet1").Range("E5").Value
myValue = ThisWorkbook.Sheets("Sheet1").Range("F5").Value

On Error GoTo nodate
ThisWorkbook.Sheets("Sheet2").Range("A:A").Find(myDate).Offset(0, 4).Value = myValue
On Error GoTo 0

Exit Sub

nodate:
    MsgBox ("Date is not found")
End Sub

Thanks to @Salamander Krajza, I will share my most updated code here, need to change the date on sheet2 from formula to value so that find function can work its magic:感谢@Salamander Krajza,我将在这里分享我最新的代码,需要将 sheet2 上的日期从公式更改为值,以便查找 function 可以发挥其魔力:

Sub CopyData()
Dim myDate As Date
Dim myValue As Variant
myDate = ThisWorkbook.Sheets("Sheet1").Range("E5").Value
myValue = ThisWorkbook.Sheets("Sheet1").Range("F5").Value


On Error GoTo nodate
ThisWorkbook.Sheets("Sheet2").Range("A:A").Find(what:=myDate, LookIn:=xlValues, lookat:=xlWhole).Offset(0, 4) = myValue
On Error GoTo 0

Exit Sub

nodate:
    MsgBox ("Date is not found")
End Sub

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

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