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

时间:2018-03-22 15:24:17

标签: excel vba excel-vba

我尝试从一个excel工作簿中复制特定数据,并将其放在我创建的另一个工作簿中。我已经尝试了以下代码的许多变体,但没有一个有效,我一直得到一个"运行时错误' 1004':应用程序定义的或对象定义的错误'& #34;在那条线上。我一直遇到麻烦的是" wb.Sheets(" Sheet1")。范​​围(" A6")。值= ThisWorkbook.Sheets(" Sheet1&# 34;)。范​​围(c.Offset(0,8))。值" - 我已经包含了完整的上下文代码。

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

1 个答案:

答案 0 :(得分:1)

尝试将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