从宏中选择打印3个变量和选定的行

时间:2017-09-10 19:05:28

标签: excel vba excel-vba

我选择要从包含7列的表格打印的行。我使用PrintOut方法将它们打印出来。

是否有一种优雅的方法可以在选定的行之前在纸张顶部添加3个变量值?

我可以使用这些变量的值设置一些单元格,然后在下一行粘贴表格中的选定行,然后打印新创建的表格。但它似乎太复杂了。

这是我的宏:

Sub FindMyNubmer()
    Dim a As Range, b As Range
    Dim firstRow As Long
    Dim lastRow As Long
    Dim eurStart As Long
    Dim eurEnd As Long
    Dim usdStart As Long
    Dim usdEnd As Long
    Dim gbpStart As Long
    Dim gbpEnd As Long
    Set a = ActiveSheet.Range("A1:A65000")

    For Each b In a.Rows
        If ActiveSheet.Range("D" & b.Row) = "EUR" Or ActiveSheet.Range("D" & b.Row) = "EUR-" Then
            eurStart = eurStart + ActiveSheet.Range("G" & b.Row)
        End If
        If ActiveSheet.Range("D" & b.Row) = "USD" Or ActiveSheet.Range("D" & b.Row) = "USD-" Then
            usdStart = usdStart + ActiveSheet.Range("G" & b.Row)
        End If
        If ActiveSheet.Range("D" & b.Row) = "GBP" Or ActiveSheet.Range("D" & b.Row) = "GBP-" Then
            gbpStart = gbpStart + ActiveSheet.Range("G" & b.Row)
        End If
        If b.Value = ActiveSheet.Range("H4").Value Then
            If firstRow = "0" Then
                firstRow = b.Row
            End If
            lastRow = b.Row
            If ActiveSheet.Range("D" & b.Row) = "EUR" Or ActiveSheet.Range("D" & b.Row) = "EUR-" Then
                eurEnd = eurEnd + ActiveSheet.Range("G" & b.Row)
            End If
            If ActiveSheet.Range("D" & b.Row) = "USD" Or ActiveSheet.Range("D" & b.Row) = "USD-" Then
                usdEnd = usdEnd + ActiveSheet.Range("G" & b.Row)
            End If
            If ActiveSheet.Range("D" & b.Row) = "GBP" Or ActiveSheet.Range("D" & b.Row) = "GBP-" Then
                gbpEnd = gbpEnd + ActiveSheet.Range("G" & b.Row)
            End If
        End If
    Next
    eurEnd = eurStart - eurEnd
    usdEnd = usdStart - usdEnd
    gbpEnd = gbpStart - gbpEnd
    ActiveSheet.Range("A" & firstRow & ":G" & lastRow).Select
    MsgBox eurStart & " - " & eurEnd
    MsgBox usdStart & " - " & usdEnd
    MsgBox gbpStart & " - " & gbpEnd
End Sub

如果我将.Select方法更改为PrintOut,它将按预期打印Range中的单元格。

我想在打印之前将eur / usd / gbp开始/结束变量置于顶部。

由于该表有7列,我想在表格单元格之前打印这样的内容。

EUR       eurStart var.          eurEnd var.
USD       ...                    ...
GBP       ...                    ...

2 个答案:

答案 0 :(得分:1)

这里是对代码的重写。它并不是对你问题的回答。

你有一些逻辑错误。即使在欧元匹配后,您的代码仍会检查美元和英镑。

Sub FindMyNubmer()

    Dim firstRow As Long, lastRow As Long

    Dim eurStart As Long, eurEnd As Long
    Dim usdStart As Long, usdEnd As Long
    Dim gbpStart As Long, gbpEnd As Long

    Dim ddd As String
    Dim ggg As Long

    Dim b As Range
    For Each b In ActiveSheet.Range("A1:A65000")

        ddd = b.Columns("D")
        ggg = b.Columns("G")

        Select Case ddd
            Case "EUR", "EUR-":    eurStart = eurStart + ggg
            Case "USD", "USD-":    usdStart = usdStart + ggg
            Case "GBP", "GBP-":    gbpStart = gbpStart + ggg
        End Select

        If b.Value = ActiveSheet.Range("H4").Value Then

            If firstRow = 0 Then firstRow = b.Row

            lastRow = b.Row

            Select Case ddd
                Case "EUR", "EUR-":    eurEnd = eurEnd + ggg
                Case "USD", "USD-":    usdEnd = usdEnd + ggg
                Case "GBP", "GBP-":    gbpEnd = gbpEnd + ggg
            End Select
        End If
    Next

    eurEnd = eurStart - eurEnd
    usdEnd = usdStart - usdEnd
    gbpEnd = gbpStart - gbpEnd

    ActiveSheet.Range("A" & firstRow & ":G" & lastRow).Select

    MsgBox "eur" & vbTab & eurStart & " - " & eurEnd & vbCrLf _
         & "usd" & vbTab & usdStart & " - " & usdEnd & vbCrLf _
         & "gbp" & vbTab & gbpStart & " - " & gbpEnd

End Sub

答案 1 :(得分:0)

这很粗糙,但确实有效。将其合并到您的脚本中。相应调整

With ActiveSheet.PageSetup
.LeftHeader = _
"EUR               " & eurStartvar & "               " & eurEndvar & Chr(10) & "USD               " & usdStartvar & "               " & usdEndvar & Chr(10) & "GBP                " & gbpStartvar & "               " & gbpEdnvar & Chr(10) & Chr(10)

End With