繁体   English   中英

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

[英]Have an Excel macro skip lines if a cell is blank

我正在为处理文本报告而开发的这个宏快要结束了,但是当我拉入的报告为空白时,我不确定如何忽略。

发生的情况是文本文件被拉入,虽然文本文件中有一些数据,但它与我需要提取的内容无关。 我已经确定如果报告为空白, 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM