我遇到了这段代码的问题:
这是如此奇怪的问题。打印报告后,我只是丢失欧洲键盘布局,键盘的数字面,逗号进入点。但只能在Excel内部,而不是Outlook,不在Windows内部 并且只有在纸上打印之后。当我选择在PrintPreview中取消打印时,没有错误。
还询问: http://www.ozgrid.com/forum/showthread.php?t=199847
Sub print_fuel_report()
'
' PRINT_fuel_report Macro
'
On Error GoTo ifERROR
'This is in case if selected out of print range but clicking print in cell("V1")
If ActiveCell.Column() > 21 Or ActiveCell.Row() > 61 Or ActiveCell.Row() = 1 _
Or ActiveCell.Value = "" Then
MsgBox " W R O N G RANGE!" & vbNewLine & " " & vbNewLine & _
" or there is no print data !", 64, ""
GoTo ifERROR
Else
Application.EnableEvents = False
Dim MSG As String, ANS As Variant
MSG = " DO YOU WANT PRINT REPORT?" & " " & _
Range("A" & ActiveCell.Row) & " " & Range("C" & ActiveCell.Row)
ANS = MsgBox(MSG, vbQuestion + vbYesNo + vbDefaultButton2, "Print report")
Select Case ANS
Case vbYes
Application.ScreenUpdating = False
Application.DecimalSeparator = ","
Application.ThousandsSeparator = "."
Application.UseSystemSeparators = False
Range("A" & ActiveCell.Row).Select
Selection.Copy
Sheets("List2").Select
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("B" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("C" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("E" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("D" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("K9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("F" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F16").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("G" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("H14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("H" & ActiveCell.Row & ":K" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("G12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("L" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("M" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F19").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("N" & ActiveCell.Row & ":Q" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("G13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("R" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("S" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F21").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("T" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("U" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("H16:J19").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll DOWN:=-30
Range("A1:L30").Select
Application.CutCopyMode = False
' In case there is no printing device:
On Error Resume Next
Application.Dialogs(xlDialogPrinterSetup).Show
Selection.PrintOut Preview:=True
On Error GoTo 0
Application.ScreenUpdating = False
Range("H16:J19").Select
Selection.ClearContents
Range("F22").Select
Selection.ClearContents
Range("F21").Select
Selection.ClearContents
Range("F19").Select
Selection.ClearContents
Range("F18").Select
Selection.ClearContents
Range("F16:F17").Select
Range("F17").Activate
Selection.ClearContents
ActiveWindow.SmallScroll DOWN:=-18
Range("G12:J14").Select
Selection.ClearContents
Range("K9").Select
Selection.ClearContents
Range("C9").Select
Selection.ClearContents
Range("C7").Select
Selection.ClearContents
Range("C6").Select
Selection.ClearContents
Range("C4").Select
Selection.ClearContents
Sheets("List1").Select
ActiveWindow.SmallScroll ToLeft:=12
Range("A1").Select
Application.UseSystemSeparators = True
Case vbNo
GoTo QUIT:
End Select
ifERROR:
End If
Application.EnableEvents = True
Exit Sub
QUIT:
Application.EnableEvents = True
End Sub
答案 0 :(得分:0)
问题:运行宏来打印报告后,键盘配置会出现故障,直到重新启动Excel应用程序。
解决方案:
更新 - 说明:
将此代码插入与print_fuel_report
相同的代码模块中。您将不再直接运行print_fuel_report
。而是运行RunMacroInBackUpCopy
。它将在另一个Excel应用程序实例中运行print_fuel_report
。
Option Explicit Sub RunMacroInBackUpCopy() Const PASSWORD As String = "" Dim TempFileName As String Dim xlApp As Excel.Application Dim xlWB As Excel.Workbook Application.DisplayAlerts = False TempFileName = getTempFileName(ThisWorkbook.Name) ThisWorkbook.SaveCopyAs TempFileName Set xlApp = New Excel.Application Set xlWB = xlApp.Workbooks.Open(Filename:=TempFileName, PASSWORD:=PASSWORD) xlApp.Run "'" & xlWB.Name & "'!print_fuel_report" xlWB.Close False xlApp.Quit Kill TempFileName Set xlWB = Nothing Set xlApp = Nothing Application.DisplayAlerts = False End Sub Function getTempFileName(s As String) As String getTempFileName = ThisWorkbook.Path & "\TempWorkBook." & Right(s, Len(s) - InStrRev(s, ".")) End Function
答案 1 :(得分:0)
Option Explicit
Sub RunMacroInBackUpCopy()
Const PASSWORD As String = ""
Dim TempFileName As String
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Application.DisplayAlerts = False
TempFileName = getTempFileName(ThisWorkbook.Name)
ThisWorkbook.SaveCopyAs TempFileName
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(Filename:=TempFileName, PASSWORD:=PASSWORD)
xlApp.Run "'" & xlWB.Name & "'!pRINTA_IZVJESTAJ_GORIVA"
xlWB.Close False
xlApp.QUIT
Kill TempFileName
Set xlWB = Nothing
Set xlApp = Nothing
Application.DisplayAlerts = False
End Sub
Function getTempFileName(s As String) As String
getTempFileName = ThisWorkbook.Path & "\TempWorkBook." & Right(s, Len(s) - InStrRev(s, "."))
End Function
Sub pRINTA_IZVJESTAJ_GORIVA()
'
' PRINTA_IZVJESTAJ_GORIVA Macro
'
On Error GoTo AkoGRESKA
'Ovo ako se klikne negdje van raspona a odabere se printer("V1")
If ActiveCell.Column() > 21 Or ActiveCell.Row() > 61 Or ActiveCell.Row() = 1 _
Or ActiveCell.Value = "" Then
MsgBox " K R I V I R A S P O N !" & vbNewLine & " " & vbNewLine & _
" ILI NEMA PODATAKA ZA ISPIS !", 64, ""
GoTo AkoGRESKA
Else
Application.EnableEvents = False
Dim MSG As String, ANS As Variant
MSG = " ŽELITE LI ISPISATI IZVJEŠTAJ ULAZA?" & " " & _
Range("A" & ActiveCell.Row) & " " & Range("C" & ActiveCell.Row)
ANS = MsgBox(MSG, vbQuestion + vbYesNo + vbDefaultButton2, "Ispis izvještaja")
Select Case ANS
Case vbYes
Application.ScreenUpdating = False
Range("A" & ActiveCell.Row).Select
Selection.Copy
Sheets("List2").Select
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("B" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("C" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("E" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("C9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("D" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("K9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("F" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F16").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("G" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("H14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("H" & ActiveCell.Row & ":K" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("G12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("L" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("M" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F19").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("N" & ActiveCell.Row & ":Q" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("G13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("R" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("S" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F21").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("T" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("F22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("List1").Select
Range("U" & ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("List2").Select
Range("H16:J19").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll DOWN:=-30
Range("A1:L30").Select
Application.CutCopyMode = False
' U slučaju da nema printera:
On Error Resume Next
Application.Dialogs(xlDialogPrinterSetup).Show
Selection.PrintOut Preview:=True
On Error GoTo 0
Application.ScreenUpdating = False
Range("H16:J19").Select
Selection.ClearContents
Range("F22").Select
Selection.ClearContents
Range("F21").Select
Selection.ClearContents
Range("F19").Select
Selection.ClearContents
Range("F18").Select
Selection.ClearContents
Range("F16:F17").Select
Range("F17").Activate
Selection.ClearContents
ActiveWindow.SmallScroll DOWN:=-18
Range("G12:J14").Select
Selection.ClearContents
Range("K9").Select
Selection.ClearContents
Range("C9").Select
Selection.ClearContents
Range("C7").Select
Selection.ClearContents
Range("C6").Select
Selection.ClearContents
Range("C4").Select
Selection.ClearContents
Sheets("List1").Select
ActiveWindow.SmallScroll ToLeft:=12
Range("A1").Select
Case vbNo
GoTo QUIT:
End Select
AkoGRESKA:
End If
Application.EnableEvents = True
Exit Sub
QUIT:
Application.EnableEvents = True
End Sub

答案 2 :(得分:0)
三个月后我发现了。问题出现在PrintPreview中,但是 WHYYY MICROSOFT,为什么!
'如果没有打印设备: On Error Resume Next Application.Dialogs(xlDialogPrinterSetup).Show Selection.PrintOut Preview:= True
当我设置Selection.PrintOut预览:= False 一切正常,没有错误。
但是,为什么!我真的需要打印预览:(;(