【发布时间】: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
此代码仅创建一个新文件复制数据并删除现有文件。 我需要一种方法来检查文件是否存在,如果存在则检查当前行是否需要更新或/和添加。 任何帮助都感激不尽。 谢谢。
【问题讨论】: