簡體   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