将单元格值复制到不同的工作簿

首先,如果这个问题已经在别的地方得到答复,让我道歉。 我有一个很好看,但找不到任何帮助我的东西。

其次,我相信有一个更简单的方法来做到这一点,但我是新的VBA,我只是试图自我介绍。

好的,所以我在工作簿的最后有一张工作表,用于编译上一张工作表中的信息,并且希望将那些全部在第2行的值复制到另一个有networking驱动器的工作簿中。

我设法得到这个工作在同一张工作表,但不是另一个工作簿(不使用用户表单)。

它返回错误“无效限定符”的行单元格(emptyRow,1.Value – DateRaised.Value

下面是我的代码,

Sub CommandButton1_Click() Dim emptyRow As Long Dim DateRaised As Long Dim CustomerName As String Dim SiteAddress As String Dim CallReason As String Dim CustomerOrderNo As Long Dim InvoiceNo As Long Dim CovernoteNo As Long Dim Findings As String Dim ProductType As String Dim Supplier As String Dim Attempts As Long Dim Condition As String Dim DateClosed As Long Dim CreditGiven As String Dim CreditValue As Long Dim IssueDays As Long Dim Comments As String DateRaised = Cells(2, "A").Value CustomerName = Cells(2, "B").Value SiteAddress = Cells(2, "C").Value CallReason = Cells(2, "D").Value CustomerOrderNo = Cells(2, "F").Value InvoiceNo = Cells(2, "G").Value CovernoteNo = Cells(2, "H").Value Findings = Cells(2, "I").Value ProductType = Cells(2, "J").Value Supplier = Cells(2, "K").Value Attempts = Cells(2, "L").Value Condition = Cells(2, "M").Value DateClosed = Cells(2, "N").Value CreditGiven = Cells(2, "O").Value CreditValue = Cells(2, "P").Value IssueDays = Cells(2, "Q").Value Comments = Cells(2, "R").Value Dim WrkBk As Workbook Dim WrkSht As Worksheet Set WrkBk = Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx") Set WrkSht = WrkBk.Sheets("Covernote Database") WrkSht.Activate emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 Cells(emptyRow, 1).Value = DateRaised.Value Cells(emptyRow, 2).Value = CustomerName.Value Cells(emptyRow, 3).Value = SiteAddress.Value Cells(emptyRow, 4).Value = CallReason.Value Cells(emptyRow, 5).Value = CustomerOrderNo.Value Cells(emptyRow, 6).Value = InvoiceNo.Value Cells(emptyRow, 7).Value = CovernoteNo.Value Cells(emptyRow, 8).Value = Findings.Value Cells(emptyRow, 9).Value = ProductType.Value Cells(emptyRow, 10).Value = Supplier.Value Cells(emptyRow, 11).Value = Attemps.Value Cells(emptyRow, 12).Value = Condition.Value Cells(emptyRow, 13).Value = DateClosed.Value Cells(emptyRow, 14).Value = CreditGiven.Value Cells(emptyRow, 15).Value = CreditValue.Value Cells(emptyRow, 16).Value = IssueDays.Value Cells(emptyRow, 17).Value = Comments.Value WrkBk.Close (SaveChanges = False) End Sub 

如果任何人都能指出我的方向,我会是一个非常快乐的人。

这是因为你试图将types(比如StringLong )variables当作referencetypes(对象)来对待它们的Value属性:

 Cells(emptyRow, 1).Value = DateRaised.Value 

而你不能(除非你使用用户定义types): typesvariables只能被访问:

 Cells(emptyRow, 1).Value = DateRaised 

但你可以简单地编码如下:

 Option Explicit Sub CommandButton1_Click() Dim emptyRow As Long Dim curSht As Worksheet Set curSht = ActiveSheet With Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx").Sheets("Covernote Database") emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1 .Cells(emptyRow, 1).Resize(, 17).value = curSht.Cells(2, 1).Resize(, 17).value '<-- paste values from originally opened sheet range A2:Q2 End With ActiveWorkbook.Close SaveChanges:=False End Sub