从macros中与选定的行一起打印3个variables

我select行从7列的表打印。 我使用PrintOut方法将其打印出来。

有没有一种优雅的方式,在选定的行之前在纸张的顶部添加3个variables值?

我可以用这些variables的值设置一些单元格,然后在下一行粘贴从表中select的行,然后打印新创build的表。 但似乎太复杂了。

这是我的macros:

 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开始/结束variables放在最前面。

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

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

这里是你的代码的重写。 这并不意味着你的问题的答案。

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

 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 

这是粗糙的,但它的工作原理。 将其合并到脚本中。 相应地调整

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