【问题标题】:Excel VBA always runs on visible sheet instead of referenced sheetExcel VBA 始终在可见工作表而不是引用工作表上运行
【发布时间】:2016-04-10 07:14:51
【问题描述】:

我正在编写一个宏,它将更新工作簿中的特定工作表(摘要)。它需要插入一行并向其添加值,但无论运行宏时哪个工作表处于活动状态,它都需要始终更新摘要表。目前,它在任何一张活跃的工作表上运行。每次我给出范围或单元格引用时,我都尝试明确命名工作表。 (我对宏比较陌生,所以录制了一个宏来给我一个起点,因此选择和复制的方式很冗长)。

Sub PasteValuesSummary()

     Dim LookupValue As String

     Dim LookupRange As Range
     Dim ws As Worksheet

     Set ws = ThisWorkbook.Sheets("Summary")

     Set LookupRange = Sheets("Summary").Range("B1:B1000")

     LookupValue = Sheets("LastUpdate").Range("A1").Value

     If Not IsError(Application.Match(LookupValue, LookupRange, 0)) Then

        Sheets("Summary").Select
        Sheets("Summary").Rows("4:4").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Sheets("Summary").Range("B4").Select
        Sheets("Summary").Range("B4") = "=LastUpdate!R[-3]C[-1]"
        Sheets("Summary").Range("C2:AP2").Select
        Selection.Copy
        ActiveWindow.ScrollColumn = 1
        Sheets("Summary").Range("C4").Select
        Sheets("Summary").Paste
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Summary").Range("B4:AP4").Select
        Application.CutCopyMode = False
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

     End If

End Sub

【问题讨论】:

  • 在“if not iserror..”行之前尝试“ws.Activate”?
  • 它提供了与之前完全相同的行为,它似乎没有更改活动工作表,并在我运行宏时在视图中插入新行。
  • Set xlSheet = Worksheets("Summary") 然后将Sheets("Summary") 更改为xlSheet

标签: vba excel


【解决方案1】:

试试这个代码。我认为LookupValue 应该是约会?

Sub PasteValuesSummary()

     Dim LookupValue As Date
     Dim rFound As Range

     Dim LookupRange As Range
     Dim ws As Worksheet

     Set ws = ThisWorkbook.Worksheets("Summary")

     Set LookupRange = ws.Range("B1:B1000")

     LookupValue = ThisWorkbook.Worksheets("LastUpdate").Range("A1").Value

     Set rFound = LookupRange.Find(What:=LookupValue, SearchFormat:=True)
     If Not rFound Is Nothing Then
        With ws 'Search help on 'With' keyword.
            .Rows("4:4").Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            .Range("B4").FormulaR1C1 = "=LastUpdate!R[-3]C[-1]"
            .Range("C2:AP2").Copy Destination:=.Range("C4")

            'This is just removing the formula and leaving the values.
            .Range("B4:AP4").Copy
            .Range("B4:AP4").PasteSpecial Paste:=xlPasteValues 'No need to add the other arguments - they're default.
        End With
    End If

End Sub

【讨论】:

  • 我提出 3 项改进:1) Set rFound = LookupRange.Find(What:=LookupValue, LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=True) 因为 LookInLookAtSearchOrder 参数始终保持与以前使用 Find 方法时一样,甚至从 UI 中!因此,始终明确设置它们会更安全。 2) 使用.Rows(4) 而不是.Rows("4:4"),仅出于可读性目的 3) 使用Range("B4").value = LookupValue 然后.Range("C2:AP2").Copy 最后.Range("C4").PasteSpecial Paste:=xlPasteValues 作为剩余的With 语句:它们的作用相同,没有多余的复制/粘贴
猜你喜欢
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多