根据 Excel 中的条件标准创建打印按钮

Creating a print button based on conditional criteria in Excel

首先是我的免责声明。虽然我有一些编程背景,但我不精通 VB 脚本,所以我可能需要一些帮助,但我非常感谢您提供的任何帮助。

我正在创建一个打印按钮,该按钮将根据用户输入的条件打印工作表。基本上我需要脚本来检查一行中的某些单元格,如果这些单元格中有数据,则移至下一行。冲洗并重复,直到您到达那些特定单元格中没有数据的行,然后根据数据自动打印正确的页数。我希望这是有道理的。我希望这是有道理的。

我尝试编写一段代码来检查某些列,并 return 当所有这些列都为空时的值。希望对你有帮助

   Sub Printing()
        Dim CheckCol1 As Integer, CheckCol2 As Integer
        Dim rowCount As Integer, rowCount1 As Integer, rowCount2 As Integer, currentRow As Integer
        Dim currentRowValue1 As String, currentRowValue2 As String
        Dim found As String
        found = "No"
        CheckCol1 = 1   'column A has a value of 1
        CheckCol2 = 2   'column B has a value of 2

        rowCount1 = Cells(Rows.Count, CheckCol1).End(xlUp).Row
        rowCount2 = Cells(Rows.Count, CheckCol2).End(xlUp).Row
        rowCount = Application.Max(rowCount1, rowCount2)

        ' find the first blank cell on both the columns
        For currentRow = 1 To rowCount
            currentRowValue1 = Cells(currentRow, CheckCol1).Value
            currentRowValue2 = Cells(currentRow, CheckCol2).Value

            If (IsEmpty(currentRowValue1) Or currentRowValue1 = "") And (IsEmpty(currentRowValue2) Or currentRowValue2 = "") Then
              MsgBox ("No data on Column A and B in row" & currentRow)
              found = "Yes"
            End If
        Next
        If found = "No" Then ' This will return rowcount+1 when the columns have values throughout the range 
           MsgBox ("No data on Column A and B in row" & rowCount + 1)
         End If

    End Sub

注意:- 您可以通过添加几个变量来增加要检查的列数。您可以尝试通过添加 Checkcol3、rowcount3、currentrowvalue3 并向 if 子句添加一个条件来添加第三列