如果单元格为空,则让Excel宏跳过行

时间:2018-01-16 16:52:24

标签: excel vba excel-vba

我接近这个宏的结尾我正在开发处理文本报告,但是我不确定当我提交的报告是空白时如何忽略

发生的是文本文件被拉入,虽然文本文件中有一些数据,但它与我需要提取的内容无关。我已经确定,如果报告因我的目的而为空,Sheet1.Range("A5")将为空,但我不确定如何告诉宏跳过我已离开评论的步骤"将值复制到主数据表"。

实际上,我想完成整个过程直到该评论,然后我需要宏来查看Sheet1.Range("A5")。如果它是空白的,那么我希望它向前跳到Sheet6.Activate行并继续执行。

我只是在复制之前放了一个If语句吗?

Sub Import()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

' Variables for paths and filename

Dim currentPath As String
Dim newPath As String
Dim currentFile As String

currentPath = "\\Unprocessed\"
newPath = "\\Processed\"

' Get the first file

    currentFile = Dir(currentPath & "*.txt")
    Do While currentFile <> ""

' Clear previous data

    Sheet1.Activate
    ActiveSheet.UsedRange.Clear
    Range("A1").Select

' Process text file

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & currentPath & currentFile, _
        Destination:=Range("$A$1"))
        .Name = "Data"
        .FieldNames = True
        .TextFileTabDelimiter = True
        .TextFileColumnDataTypes = Array(3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .Refresh BackgroundQuery:=False
    End With

    ActiveSheet.QueryTables(1).Delete

' Copy values to main data table

    Sheet3.Range("A2:Q2").Copy
    Sheet6.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Sheet6.Activate

' Move file into Processed folder

    Name currentPath & currentFile As newPath & currentFile

' Get the next file

    currentFile = Dir
    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

IF声明应该这样做:

If Sheet1.Range("A5") <> "" then 'if A5 is not blank then do the following
    Sheet3.Range("A2:Q2").Copy
    Sheet6.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Sheet6.Activate
End if