【问题标题】:Appending data to another workbook/file将数据附加到另一个工作簿/文件
【发布时间】:2018-04-07 07:26:05
【问题描述】:

我有这个代码:

    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

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

【问题讨论】:

    标签: excel vba append transfer


    【解决方案1】:

    要检查变量flname 中的文件名是否存在,请使用Dir() 函数。

    If Dir(flname) = "" Then
      Msgbox("File does not exist.")
      Exit Sub
    End If
    

    【讨论】:

    • 并检查当前行是否需要更新或添加?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多