我正在使用以下宏:
'Copy active agency ID and paste into search on Worker Details
ActiveCell.Select
Selection.Copy
Range("L5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Call macro to run the agency details search
Call AgencyDetails
基本上,它使用活动单元格,将其粘贴到搜索字段中,然后运行一个宏,根据该条件提取数据。
当单步执行时,它会复制并粘贴活动单元格,然后搜索工作正常。
运行宏时,似乎不会将活动单元格复制并粘贴到搜索字段。或者被调用的宏运行得太早......
我尝试过添加暂停和doevents
等,但我认为doevents
用于odbc连接。
使事情进一步复杂化。我有另一个几乎相同的宏,它将文本复制到搜索字段,然后根据该标准返回数据:
'Copy active worker ID and paste into search on Worker Details
ActiveCell.Select
Selection.Copy
Sheets("Worker Details").Select
Range("E5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Call macro to run the worker details search
Call WorkerDetails
这很好用。
有什么想法吗?可能非常简单,因为我的vba并不精彩。
谢谢,
Sub AgencyDetails()
Dim BlankCheckAgency As Range
Set BlankCheckAgency = Range("AgencyDetails[[#Headers],[Agency ID]]")
Dim BlankCheckWorkers As Range
Set BlankCheckWorkers = Range("AgencyWorkers[[#Headers],[auto_number]]")
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVisible
'Clear Data
Range("G9,L9,G12,I12,G15,I15,G18,L18,Q9,Q12,Q15").Select
Selection.ClearContents
Range("G28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("I28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("K28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("L5").Select
'Refresh Data
ActiveWorkbook.Connections("AgencyDetails").Refresh
ActiveWorkbook.Connections("AgencyBDM").Refresh
ActiveWorkbook.Connections("AgencyAM").Refresh
ActiveWorkbook.Connections("AgencySalesRep").Refresh
ActiveWorkbook.Connections("AgencyWorkers").Refresh
'DataCheck for agency details
Sheets("Agency Search Data").Select
BlankCheckAgency.Select
ActiveCell.Offset(1).Select
If IsEmpty(ActiveCell) = False Then
GoTo Data
Else
GoTo NoData
End If
NoData:
'Go back to search window and display message
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
msgBox "No agency on record matched the ID you have searched for." & vbNewLine & vbNewLine & "If you think this is wrong, please contact OSD"
GoTo Finish
Data:
'Agency Name
Range("AgencyDetails[Agency Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Unmerge Address
Range("L9").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'Full Address
Sheets("Agency Search Data").Select
Range("AgencyDetails[Full Address]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("L9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge Address
Range("L9:O15").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'Agency Status
Sheets("Agency Search Data").Select
Range("AgencyDetails[Agency Status 2]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Agency Brand
Sheets("Agency Search Data").Select
Range("AgencyDetails[Brand]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Agency Reg
Sheets("Agency Search Data").Select
Range("AgencyDetails[Agency Reg]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'VAT Reg
Sheets("Agency Search Data").Select
Range("AgencyDetails[Vat Reg]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Unmerge GNotes
Range("G18").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'General Notes
Sheets("Agency Search Data").Select
Range("AgencyDetails[General Notes]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge GNotes
Range("G18:J24").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'Unmerge SNotes
Range("L18").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'Sales Notes
Sheets("Agency Search Data").Select
Range("AgencyDetails[Sales Notes]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("L18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Merge SNotes
Range("L18:O24").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
'BDM
Sheets("Agency Search Data").Select
Range("AgencyBDM[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sales Rep
Sheets("Agency Search Data").Select
Range("AgencySalesRep[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'AM
Sheets("Agency Search Data").Select
Range("AgencyAM[Full Name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("Q15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'DataCheck for workers
Sheets("Agency Search Data").Select
BlankCheckWorkers.Select
ActiveCell.Offset(1).Select
If IsEmpty(ActiveCell) = False Then
GoTo Data2
Else
GoTo NoData2
End If
NoData2:
Rows("1:1000").Select
Selection.RowHeight = 15
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
msgBox "The agency details have been pulled but there are no workers associated with the Agency" & vbNewLine & vbNewLine & "If you think this to not be true, please contact OSD"
GoTo Finish
Data2:
'Pull worker IDs
Sheets("Agency Search Data").Select
Range("AgencyWorkers[auto_number]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("G28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Pull worker first name
Sheets("Agency Search Data").Select
Range("AgencyWorkers[first_name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("I28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Pull worker last name
Sheets("Agency Search Data").Select
Range("AgencyWorkers[last_name]").Select
Selection.Copy
Sheets("Agency Search").Select
Range("K28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("1:1000").Select
Selection.RowHeight = 15
Range("L5").Select
Finish:
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:4)
建议尽可能不使用Select和Activate方法,将所选单元格中的值传递到所需的单元格,如下所示:
Range("L5").Value = ActiveCell.Value
'Call macro to run the agency details search
Call AgencyDetails
正如Vityata所提到的,最好完全限定你的范围,例如:
Sheet1.Range("L5").Value
甚至是Sheets("Sheet1").Range("L5").Value
,这样您的代码就不会采用ActiveSheet,而是会从定义的范围中获取值。
<强>更新强>
Sub AgencyDetails()
Dim BlankCheckAgency As Range
Set BlankCheckAgency = Sheets("Agency Search Data").Range("AgencyDetails[[#Headers],[Agency ID]]")
Dim BlankCheckWorkers As Range
Set BlankCheckWorkers = Range("AgencyWorkers[[#Headers],[auto_number]]")
'
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVisible
'Clear Data
ActiveSheet.Range("G9,L9,G12,I12,G15,I15,G18,L18,Q9,Q12,Q15").ClearContents
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
Range("G28:G" & LastRow).ClearContents
Range("I28:I" & LastRow).ClearContents
Range("K28:K" & LastRow).ClearContents
'Range("L5").Select
'Refresh Data
ActiveWorkbook.Connections("AgencyDetails").Refresh
ActiveWorkbook.Connections("AgencyBDM").Refresh
ActiveWorkbook.Connections("AgencyAM").Refresh
ActiveWorkbook.Connections("AgencySalesRep").Refresh
ActiveWorkbook.Connections("AgencyWorkers").Refresh
'DataCheck for agency details
If IsEmpty(BlankCheckAgency.Offset(1)) = False Then
GoTo Data
Else
GoTo NoData
End If
NoData:
'Go back to search window and display message
Sheets("Agency Search").Select
ActiveWorkbook.Sheets("Agency Search Data").Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
MsgBox "No agency on record matched the ID you have searched for." & vbNewLine & vbNewLine & "If you think this is wrong, please contact OSD"
GoTo Finish
Data:
'Agency Name
Range("AgencyDetails[Agency Name]").Copy
Sheets("Agency Search").Range("G9").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
答案 1 :(得分:3)
这两个代码的问题在于您没有引用正确的工作表,而是假设活动表。
确保您引用它并避免使用ActiveCell
:
Sub TestMe()
With Worksheets("SomeDetails")
.Range("A1").Copy
.Range("L5").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Call AgencyDetails
End Sub
How to avoid using Select in Excel VBA
每当您在Excel中复制和粘贴时,最好使用Application.CutCopyMode = False
。
确保粘贴范围未被选中,与按 Esc 相同。
答案 2 :(得分:1)
一个考虑因素可能是Application.Calculation
模式 - 如果这是xlCalculationManual
或xlCalculationSemiautomatic
,则Excel可能无法注册$ L $ 5在调用宏时已更新。
您可以强制重新计算所有内容(使用Application.Calculate
),仅使用ActiveSheet(ActiveSheet.Calculate
)或特定范围(Range("L5").Calculate
或Cells(5,12).Calculate
)
在大/复杂的宏中,将计算模式设置为手动并明确决定何时计算可以节省大量时间,与设置Application.ScreenUpdating = False
的方式相同。请记住之后重置它们! (非常长时间运行的宏可能还需要DoEvents
某处让Windows知道Excel没有崩溃!)