杀死打开的CSV文件

时间:2018-11-01 09:02:16

标签: excel vba

所以我有代码将数据从一个csv复制到另一个打开的Excel xlsx文件,但是我要做的是一旦处理完成就杀死csv文件。该代码有效,但kill部分不起作用,我在这里做错了什么?

Sub MasterData()
Dim getfilestg As String

    Application.Calculation = xlManual
    Application.ScreenUpdating = False
    ActiveSheet.UsedRange.ClearContents
    Application.ScreenUpdating = True
    Range("A1").Select

Application.ScreenUpdating = False


Set reporting = ActiveWorkbook

Set rep = Sheets("Master Data")


 For Each reportSF In Application.Workbooks
        reportSF.Activate

        If Range("A1").Value Like "GoldTier ID" And Range("AT1").Value Like "Owner's TL" Then reportSF.Activate: Exit For
        Next reportSF
If Not reportSF Is Nothing Then

reporting.Activate
rep.Activate
endLine = Range("A1").Offset(Rows.Count - 1, 0).End(xlUp).Row

 reportSF.Activate
endLine = Range("A1").Offset(Rows.Count - 1, 0).End(xlUp).Row
reportSF.Sheets(1).Range("A1:AT" & endLine).Copy
reporting.Activate
rep.Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False

End If

If reportSF.ActiveCell.Value = "Account" Then
getfilestg = Application.GetOpenFilename("Excel Files (*.csv), *.csv")

Kill (getfilestg)
End If

Range("A1").Select


End Sub

1 个答案:

答案 0 :(得分:1)

替换以下代码:

If reportSF.ActiveCell.Value = "Account" Then
getfilestg = Application.GetOpenFilename("Excel Files (*.csv), *.csv")

Kill (getfilestg)
End If

具有:

If reportSF.Sheet(1).Range("A1").Value = "Account" Then '<-- change range here
getfilestg = reportSF.FullName
reportSF.Close
Kill (getfilestg)
End If

这将关闭并删除reportSF