VB for excel - 打印控件出现问题

时间:2017-11-05 17:37:41

标签: excel-vba vba excel

需要新手帮助。我有一个excel表设置了代码来切换单元格与相邻的行 - 最后的结果是我们可以单击单元格,它只会打印相邻的行,如果他们被切换(有超过500行,并不是所有都需要每个项目 - 一个项目"做"列表)。要切换的单元格位于第5列到第8列(列E:H)。我正在努力让打印宏工作 - 特别是粗体代码,它指的是我想要的行和列。这是打印代码:

'------------------------------------------------
Sub PrintTickedRows()
    Dim MyRow As Long
    Dim LastRow As Long
    '--------------------
    Application.ScreenUpdating = False
    MyRow = 5 - 8
    LastRow = ActiveSheet.Range("A65536").End(xlUp).Row
    '- hide rows
    For MyRow = 5 - 8 To LastRow
        **If ActiveSheet.Cells(MyRow, 5 - 8).Value = "" Then**
            ActiveSheet.Rows(MyRow).EntireRow.Hidden = True
        End If
    Next
    '- print
    ActiveSheet.PrintOut
    '- make rows visible again
    For MyRow = 5 - 8 To LastRow
            ActiveSheet.Rows(MyRow).EntireRow.Hidden = False
    Next
    Application.ScreenUpdating = True
End Sub
'----------------------------------------------------

原始代码编写为使用多行但只有一列 - 我有多个切换列。切换代码有效。有人可以帮助我完成这个,因为我认为我已经在这个打印代码上跳过这个打印代码(我今天早上只有4个小时的编码经验):))

还是有点麻烦 - 这是你建议的编辑,但最后我还挂了正确的代码....

'------------------------------------------------
Sub PrintTickedRows()
    Dim MyRow As Long
    Dim LastRow As Long
    Dim Mycol As Integer
    '--------------------
    Application.ScreenUpdating = False
    Mycol = 5
    For Mycol = 5 To 8
    MyRow = 1
    LastRow = ActiveSheet.Range("A65536").End(xlUp).Row
    '- hide rows
    For MyRow = 1 To LastRow
          If ActiveSheet.Cells(MyRow, Mycol).Value = "" Then
            ActiveSheet.Rows(MyRow).EntireRow.Hidden = True
        End If
    Next
    '- print
    ActiveSheet.PrintOut
    '- make rows visible again
    For MyRow = 1 To LastRow
            ActiveSheet.Rows(MyRow).EntireRow.Hidden = False
    Next
    Application.ScreenUpdating = True
End Sub
'----------------------------------------------------

如果有帮助,这是excel表格屏幕截图:

***here is the excel sheet screen shot if that helps

2 个答案:

答案 0 :(得分:0)

我发现你很难说出变量的值。 如果您设置MyRow = 5 - 8,则MyRow的值为-3。 我的建议是为列创建一个新变量。

Dim MyCol  as Integer

MyCol = 5 'This should start your loop in column E
'Then create a loop for your column
For MyCol = 5 to 8 'This loop will work from Column E to Column H
   For MyRow = 5 To LastRow 'Define your initial Row, I'm supposing is 5.
       If Cells(MyRow, MyCol).Value = "" Then
           ActiveSheet.Rows(MyRow).EntireRow.Hidden = True 'You don't need to validate other columns?
       End If
   Next 'End of Row Loop
Next  ' End of Column Loop

希望此示例可以帮助您修改代码。

答案 1 :(得分:0)

我认为最简单的方法是隐藏所有行,然后取消隐藏其中至少有一个勾号的行:

Sub PrintTickedRows()
    Dim MyRow As Long
    Dim LastRow As Long
    Dim MyCol As Long
    Application.ScreenUpdating = False

    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Hide all rows from row 5 to the last used row
        .Rows("5:" & LastRow).Hidden = True

        For MyRow = 5 To LastRow
            For MyCol = 5 To 8
                ' Use 
                'If .Cells(MyRow, MyCol).Value = "" Then
                ' if your cells contain formulas to generate the ticks, otherwise
                ' you can just test whether the cell is empty
                If Not IsEmpty(.Cells(MyRow, MyCol).Value) Then
                    .Rows(MyRow).Hidden = False
                    Exit For
                End If
            Next
        Next

        '- print
        .PrintOut

        '- make rows visible again
        .Rows("5:" & LastRow).Hidden = False
    End With

    Application.ScreenUpdating = True
End Sub