简体   繁体   English

使用VBA将数据从一个Excel工作簿复制到另一个

[英]Copy Data from One Excel Workbook to Another Using VBA

I'm trying to copy specific data from one excel workbook and place it in another workbook that I've created. 我正在尝试从一个Excel工作簿中复制特定数据,然后将其放在我创建的另一工作簿中。 I've tried many variations of the following code but none have worked, I keep getting a "Run-time error '1004': Application-defined or object-defined error'" on that line. 我尝试了以下代码的许多变体,但没有奏效,我在该行上不断收到“运行时错误'1004':应用程序定义的错误或对象定义的错误'”。 The line I keep having trouble on is "wb.Sheets("Sheet1").Range("A6").Value = ThisWorkbook.Sheets("Sheet1").Range(c.Offset(0, 8)).Value" - I've included my full code for context. 我一直遇到麻烦的那一行是“ wb.Sheets(” Sheet1“)。Range(” A6“)。Value = ThisWorkbook.Sheets(” Sheet1“)。Range(c.Offset(0,8))。Value” -我已经包含了完整的上下文代码。

Private Sub Go_Click()
With Worksheets(1).Range("A:A")
'Search from user input for the entry with that last name
Set c = .Find(LNE.Text, , xlValues, xlWhole)
  If Not c Is Nothing Then
       Dim wb As Workbook
       Set wb = Workbooks.Add("\Documents\Custom Office Templates\KFS_Template.xltm")

       StartDate = c.Offset(0, 3)
       EndDate = c.Offset(0, 4)
       If DateDiff("d", SDE.Text, StartDate) > -1 Then
            If DateDiff("d", EndDate, EDE.Text) > -1 Then
                    Set q = Range("A1")
                    wb.Sheets("Sheet1").Range("A6").Value = ThisWorkbook.Sheets("Sheet1").Range(c.Offset(0, 8)).Value
            End If
       End If
   End If
End With
End Sub

Try setting C as range, I think the problem is because you're calling a range object using a range object already. 尝试将C设置为范围,我认为问题是因为您已经在使用范围对象来调用范围对象。

Private Sub Go_Click()
Dim c As Range
With Worksheets(1).Range("A:A")
'Search from user input for the entry with that last name
Set c = .Find(LNE.Text, , xlValues, xlWhole)
  If Not c Is Nothing Then
       Dim wb As Workbook
       Set wb = Workbooks.Add("\Documents\Custom Office Templates\KFS_Template.xltm")

       StartDate = c.Offset(0, 3)
       EndDate = c.Offset(0, 4)
       If DateDiff("d", SDE.Text, StartDate) > -1 Then
            If DateDiff("d", EndDate, EDE.Text) > -1 Then
                    Set q = Range("A1")
                    wb.Sheets("Sheet1").Range("A6").Value = c.Offset(0, 8).Value
            End If
       End If
   End If
End With

End Sub

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

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