[英]Have an Excel macro skip lines if a cell is blank
I'm nearing the end of this macro I'm developing for the processing of text reports, but I'm not sure how to ignore when the reports I'm pulling in are blank.我正在为处理文本报告而开发的这个宏快要结束了,但是当我拉入的报告为空白时,我不确定如何忽略。
What occurs is that the text file is pulled in, and while there is some data in the text file, it's not relevant to what I need to extract.发生的情况是文本文件被拉入,虽然文本文件中有一些数据,但它与我需要提取的内容无关。 I've identified that Sheet1.Range("A5")
will be blank if the report is blank for my purposes, but I'm not sure how to tell the macro to skip the steps where I've left the comment "Copy values to main data table".我已经确定如果报告为空白, Sheet1.Range("A5")
将为空白,但我不确定如何告诉宏跳过我留下评论“复制值”的步骤到主数据表”。
Effectively, I want to go through the process all the way up to that comment, and then I need for the macro to look at Sheet1.Range("A5")
.实际上,我想一直执行到该评论的过程,然后我需要让宏查看Sheet1.Range("A5")
。 If it's blank, then I want it to skip ahead to the Sheet6.Activate
line and carry on executing.如果它是空白的,那么我希望它跳到Sheet6.Activate
行并继续执行。
Do I just put an If statement before the copy?我只是在副本之前放一个 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
An IF statement should do it: 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.