在 Visual Basic 中遇到 isEmpty 问题
Having trouble with isEmpty in visual Basic
我有一个简单的脚本可以将所有输入行打印到格式化输出中。我想跳过A列为空的任何行,但如果有数据仍会打印,因此根据A列跳过第1行,第2行打印,第3行打印,第4行跳过等。我该怎么做?我尝试了很多代码都没有结果。这是我当前的工作代码,它打印所有内容。
Sub PrintAllRecords()
RowCount = Worksheets("Input").Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
Next i
End Sub
这应该有效:
Sub PrintAllRecords()
Dim inputSheet As Worksheet: Set inputSheet = ThisWorkbook.Worksheets("input")
RowCount = inputSheet.Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
If Not IsEmpty(inputSheet.Cells(i, 1)) Then
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
End If
Next i
End Sub
试试这个
Sub PrintAllRecords()
RowCount = Worksheets("Input").Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
If Not IsEmpty(Worksheets("Input").Range("A" & i+1)) Then
msgbox i
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
End If
Next i
End Sub
使用 AutoFilter
打印非空白
- 这将打印第一列不为空的已使用范围的行。
- 调整常量部分中的值。
Option Explicit
Sub PrintNonBlanks()
Const dFolderPath As String = "C:\Test\"
Const dFileName As String = "Test.pdf"
Const wsName As String = "Sheet1"
Const CriteriaColumnNumber As Long = 1
Const CriteriaString As String = "<>" ' non-blanks
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containinig this code
' Create a reference to the worksheet.
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
' Remove any previous filter.
If ws.AutoFilterMode Then ws.AutoFilterMode = False
' Create a reference to the worksheets used range.
Dim urg As Range: Set urg = ws.UsedRange
' Filter first column by non-blanks.
urg.AutoFilter CriteriaColumnNumber, CriteriaString
' Test with either...
urg.ExportAsFixedFormat xlTypePDF, dFolderPath & dFileName
' ... or...
'ws.ExportAsFixedFormat xlTypePDF, dFolderPath & dFileName
' ... before printing...
'urg.PrintOut
' or...
'ws.PrintOut
' Remove the filter.
ws.AutoFilterMode = False
' Locate the file (folder) in Windows File Explorer.
'wb.FollowHyperlink dFolderPath
End Sub
我有一个简单的脚本可以将所有输入行打印到格式化输出中。我想跳过A列为空的任何行,但如果有数据仍会打印,因此根据A列跳过第1行,第2行打印,第3行打印,第4行跳过等。我该怎么做?我尝试了很多代码都没有结果。这是我当前的工作代码,它打印所有内容。
Sub PrintAllRecords()
RowCount = Worksheets("Input").Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
Next i
End Sub
这应该有效:
Sub PrintAllRecords()
Dim inputSheet As Worksheet: Set inputSheet = ThisWorkbook.Worksheets("input")
RowCount = inputSheet.Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
If Not IsEmpty(inputSheet.Cells(i, 1)) Then
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
End If
Next i
End Sub
试试这个
Sub PrintAllRecords()
RowCount = Worksheets("Input").Cells(Rows.Count, 1).End(xlUp).Row - 1
Worksheets("Print-Auto (2)").Select
For i = 1 To RowCount
If Not IsEmpty(Worksheets("Input").Range("A" & i+1)) Then
msgbox i
Range("B1").Value = i
ActiveSheet.PrintOut Copies:=1
End If
Next i
End Sub
使用 AutoFilter
打印非空白
- 这将打印第一列不为空的已使用范围的行。
- 调整常量部分中的值。
Option Explicit
Sub PrintNonBlanks()
Const dFolderPath As String = "C:\Test\"
Const dFileName As String = "Test.pdf"
Const wsName As String = "Sheet1"
Const CriteriaColumnNumber As Long = 1
Const CriteriaString As String = "<>" ' non-blanks
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containinig this code
' Create a reference to the worksheet.
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
' Remove any previous filter.
If ws.AutoFilterMode Then ws.AutoFilterMode = False
' Create a reference to the worksheets used range.
Dim urg As Range: Set urg = ws.UsedRange
' Filter first column by non-blanks.
urg.AutoFilter CriteriaColumnNumber, CriteriaString
' Test with either...
urg.ExportAsFixedFormat xlTypePDF, dFolderPath & dFileName
' ... or...
'ws.ExportAsFixedFormat xlTypePDF, dFolderPath & dFileName
' ... before printing...
'urg.PrintOut
' or...
'ws.PrintOut
' Remove the filter.
ws.AutoFilterMode = False
' Locate the file (folder) in Windows File Explorer.
'wb.FollowHyperlink dFolderPath
End Sub