将数据附加到另一个工作簿/文件

时间:2018-04-07 07:26:05

标签: excel vba append transfer

我有这段代码:

    Private Sub TransfertoWrkBtn_Click()

    Dim c As Range
    Dim i As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim fSource As Worksheet
    Dim TargetBook As Workbook
    Dim Target As Worksheet
    Dim fTarget As Worksheet

    Set Source = ActiveWorkbook.Worksheets("Customer")
    Set fSource = ActiveWorkbook.Worksheets("Family")

flname = InputBox("Enter File Name :", "Creating New File...")

If flname <> "" Then
     Set TargetBook = Workbooks.Add

  TargetBook.SaveAs Filename:="C:\MSN\" & flname
  TargetBook.Sheets.Add.Name = "Family"
  TargetBook.Sheets.Add.Name = "Customer"

End If


Set Target = ActiveWorkbook.Worksheets("Customer")

   j = 2     ' Start copying to row 1 in target sheet
   For Each c In Source.Range("v1:v10000")   ' Do 1000 rows
       If c = flname Then
           Source.Rows(c.Row).Copy Target.Rows(j)
       j = j + 1
          End If
    Next c


Set fTarget = ActiveWorkbook.Worksheets("Family")

j = 2     ' Start copying to row 1 in target sheet
   For Each i In fSource.Range("n1:n10000")
       If i = flname Then
           fSource.Rows(i.Row).Copy fTarget.Rows(j)
       j = j + 1
          End If
    Next i

End Sub

此代码仅创建新文件复制数据并删除现有文件。 我需要一种方法来检查文件是否存在,如果是,检查当前行是否需要更新或/和添加。 任何帮助都感激不尽。 感谢。

1 个答案:

答案 0 :(得分:0)

要检查变量res[, c(1:3, order(as.numeric(sub("\\D+", "", names(res)[-(1:3)]))) + 3), with = FALSE] # Company_Name Company_ID Company_Phone Employee_Name1 Employee_ID1 Employee_Name2 Employee_ID2 Employee_Name3 Employee_ID3 #1: KFC 1 237389 John 1001 Mary 1002 Jane 1003 #2: McD 2 237002 Joshua 2001 Anne 2002 NA NA 中的文件名是否存在,请使用flname函数。

Dir()