vba粘贴不起作用

vba粘贴不起作用,vba,excel,Vba,Excel,到目前为止,我已经生成了这段代码,但是我无法让粘贴生效 这个想法贯穿了190本工作簿,并将公式粘贴到某些单元格中,其他单元格的常数范围为H1:Z160,这将为excel考试打分。如果手动操作,所有公式和常量都将粘贴并起作用 标签为的粘贴函数失败,出现以下错误: 这是现在更新和更正的代码: Option Explicit Sub Examnew() Dim rCell As Range, rRng As Range 'define loop names Dim wbmas

到目前为止,我已经生成了这段代码,但是我无法让粘贴生效

这个想法贯穿了190本工作簿,并将公式粘贴到某些单元格中,其他单元格的常数范围为H1:Z160,这将为excel考试打分。如果手动操作,所有公式和常量都将粘贴并起作用

标签为的粘贴函数失败,出现以下错误:

这是现在更新和更正的代码:

    Option Explicit

Sub Examnew()
    Dim rCell As Range, rRng As Range 'define loop names
    Dim wbmaster As Workbook                     'name for master workbook
    Dim wbtarget As Workbook                      'name for student workbook
   Set wbmaster = ActiveWorkbook               'set the name for the master
   Dim i As Long                                           'a counter for the result pasteback

With Application '<--|turn off screen & alerts only removed while testing
.ScreenUpdating = False
.EnableEvents = False
End With

i = 1   'Set the counter for result paste back

    'Student numbers in cells B3:B136 WARNING SET TO 2 STUDENTS ONLY FOR TEST
    'NOTE that st Nums are in col B with a duplicate in col A to collect results.
    Set rRng = wbmaster.Sheets("studentlist").Range("B3:B4")
    ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

    For Each rCell In rRng '<                 | loop through "students" range

         ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

      'now open Student exam workbook and set to name "wbtarget"
         Workbooks.Open ("/Users/michael/Final_V1/" & rCell.Value & ".xlsx")
         Set wbtarget = Workbooks(rCell.Value & ".xlsx")

     'do copy & paste from Master to Target
         wbmaster.Sheets("Answers_Source").Range("h1:z160").Copy
         wbtarget.Sheets("ANSWERS").Range("h1:z160").PasteSpecial

         Application.CutCopyMode = False      'Clear the copy command

    'Now collect the result in cell I4 and paste it back into column B using the rCell
    'for that student number matches the st num in col A
        wbtarget.Sheets("Answers").Range("I4").Copy
        wbmaster.Sheets("studentlist").Range("B" & 2 + i).PasteSpecial xlPasteValues

        Application.CutCopyMode = False      'Clear the copy command

     'now save and close the student file...
        wbtarget.Close (True)

        i = i + 1      'increment i for next pasteback


    Next rCell   '<                            | next student number
   'save the results file
   wbmaster.Save


       ActiveSheet.DisplayPageBreaks = True '<    | turn back on page breaks once all done

'turn screen & alerts back on
With Application
.ScreenUpdating = True: .DisplayAlerts = True
'.DisplayPageBreaks = True
End With
End Sub
非常好用,谢谢各位。

试着删除这些在上下文中毫无意义的内容

   'do copy from reference "Answers_Source" worksheet
   wb.Sheets("Answers_Source").Range("h1:z160").Copy

   'now paste the formulas into the student exam workbook
   wb2.Sheets("Answers").Range("h1:z160").Paste      
尝试删除这些在上下文中毫无意义的内容

   'do copy from reference "Answers_Source" worksheet
   wb.Sheets("Answers_Source").Range("h1:z160").Copy

   'now paste the formulas into the student exam workbook
   wb2.Sheets("Answers").Range("h1:z160").Paste      

尝试转到visual basic编辑器->工具->参考。检查您正在使用的引用,并查看是否激活了所有需要的引用。此问题的根本原因似乎与中提到的问题有关,请尝试转到visual basic编辑器->工具->参考。检查您正在使用的引用,并查看是否激活了所有需要的引用。出现这种情况的根本原因似乎与中提到的问题有关,并且

该行代码失败的原因是没有用于范围对象的粘贴方法

复制粘贴有两种方法

1向复制方法中的目标参数发送一个值。这样就不需要粘贴命令: wb.SheetsAnswers_来源.范围1:z160.Copy_ 目的地:=wb2.SheetsAnswers.Rangeh1:z160

2在复制后对目标范围使用PasteSpecial方法,默认情况下,该方法会像标准粘贴一样粘贴所有内容

wb2.SheetsAnswers.范围1:z160.1


然后,要停止您复制的单元格周围的选框或移动蚂蚁,请使用Application.CutCopyMode=False完成该操作。

该代码行失败的原因是没有用于范围对象的粘贴方法

复制粘贴有两种方法

1向复制方法中的目标参数发送一个值。这样就不需要粘贴命令: wb.SheetsAnswers_来源.范围1:z160.Copy_ 目的地:=wb2.SheetsAnswers.Rangeh1:z160

2在复制后对目标范围使用PasteSpecial方法,默认情况下,该方法会像标准粘贴一样粘贴所有内容

wb2.SheetsAnswers.范围1:z160.1


然后,要停止您复制的单元格周围的选框或行进蚂蚁,请使用Application.CutCopyMode=False完成。即使已回答此问题,也应将其作为此问题的选项

如果只查看CopyPasteValue,则最好将Range Value属性调整为与源范围值相等

有两个优点:

没有应用程序。CutCopyMode=False。 屏幕不需要闪存更新/滚动。 应该更快。 你甚至不需要取消隐藏或激活你不需要的复制,但人们认为你可以。。。所以我把它列出来了!。 所以我用这些更改重建了您的宏,尽管我并没有做任何其他更改,所以无论您修复了什么,都可能需要再次执行。我还包括了第二个宏TimerMacro,您可以使用它来计时它运行的时间,以防您想要测试性能差异。如果不使用任何日期,可以使用属性Value2,尽管我没有看到这方面有多大改进。 祝你好运

Sub Examnew_NEW()
    Dim rCell As Range, rRng As Range 'define loop names
    Dim wbmaster As Workbook                     'name for master workbook
    Dim wbtarget As Workbook                      'name for student workbook
   Set wbmaster = ActiveWorkbook               'set the name for the master
   Dim i As Long                                           'a counter for the result pasteback

With Application '<--|turn off screen & alerts only removed while testing
.ScreenUpdating = False
.EnableEvents = False
End With

i = 1   'Set the counter for result paste back

    'Student numbers in cells B3:B136 WARNING SET TO 2 STUDENTS ONLY FOR TEST
    'NOTE that st Nums are in col B with a duplicate in col A to collect results.
    Set rRng = wbmaster.Sheets("studentlist").Range("B3:B4")
    ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

    For Each rCell In rRng '<                 | loop through "students" range

         ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

      'now open Student exam workbook and set to name "wbtarget"
         Workbooks.Open ("/Users/michael/Final_V1/" & rCell.Value & ".xlsx")
         Set wbtarget = Workbooks(rCell.Value & ".xlsx")

     'do copy & paste from Master to Target
     'PGCodeRider CHANGED!!!!!!!!!!!!!!
     wbtarget.Sheets("ANSWERS").Range("h1:z160").Value = _
         wbmaster.Sheets("Answers_Source").Range("h1:z160").Value


         Application.CutCopyMode = False      'Clear the copy command

    'Now collect the result in cell I4 and paste it back into column B using the rCell
    'for that student number matches the st num in col A


        'PGCodeRider CHANGED!!!!!!!!!!!!!!
        wbmaster.Sheets("studentlist").Range("B" & 2 + i).Value = _
            wbtarget.Sheets("Answers").Range("I4").Value

        Application.CutCopyMode = False      'Clear the copy command

     'now save and close the student file...
        wbtarget.Close (True)

        i = i + 1      'increment i for next pasteback


    Next rCell   '<                            | next student number
   'save the results file
   wbmaster.Save


       ActiveSheet.DisplayPageBreaks = True '<    | turn back on page breaks once all done

'turn screen & alerts back on
With Application
.ScreenUpdating = True: .DisplayAlerts = True
'.DisplayPageBreaks = True
End With
End Sub


Sub timerMACRO()
'Run this if you want to run your macro and then get a timed result
Dim beginTime As Date: beginTime = Now

Call Examnew_NEW

MsgBox DateDiff("S", beginTime, Now) & " seconds."

End Sub

尽管这一点已经得到了回答,但这个问题的选项中应该包含一些内容

如果只查看CopyPasteValue,则最好将Range Value属性调整为与源范围值相等

有两个优点:

没有应用程序。CutCopyMode=False。 屏幕不需要闪存更新/滚动。 应该更快。 你甚至不需要取消隐藏或激活你不需要的复制,但人们认为你可以。。。所以我把它列出来了!。 所以我用这些更改重建了您的宏,尽管我并没有做任何其他更改,所以无论您修复了什么,都可能需要再次执行。我还包括了第二个宏TimerMacro,您可以使用它来计时它运行的时间,以防您想要测试性能差异。如果不使用任何日期,可以使用属性Value2,尽管我没有看到这方面有多大改进。 祝你好运

Sub Examnew_NEW()
    Dim rCell As Range, rRng As Range 'define loop names
    Dim wbmaster As Workbook                     'name for master workbook
    Dim wbtarget As Workbook                      'name for student workbook
   Set wbmaster = ActiveWorkbook               'set the name for the master
   Dim i As Long                                           'a counter for the result pasteback

With Application '<--|turn off screen & alerts only removed while testing
.ScreenUpdating = False
.EnableEvents = False
End With

i = 1   'Set the counter for result paste back

    'Student numbers in cells B3:B136 WARNING SET TO 2 STUDENTS ONLY FOR TEST
    'NOTE that st Nums are in col B with a duplicate in col A to collect results.
    Set rRng = wbmaster.Sheets("studentlist").Range("B3:B4")
    ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

    For Each rCell In rRng '<                 | loop through "students" range

         ActiveSheet.DisplayPageBreaks = False '<  | turn off page breaks for speed

      'now open Student exam workbook and set to name "wbtarget"
         Workbooks.Open ("/Users/michael/Final_V1/" & rCell.Value & ".xlsx")
         Set wbtarget = Workbooks(rCell.Value & ".xlsx")

     'do copy & paste from Master to Target
     'PGCodeRider CHANGED!!!!!!!!!!!!!!
     wbtarget.Sheets("ANSWERS").Range("h1:z160").Value = _
         wbmaster.Sheets("Answers_Source").Range("h1:z160").Value


         Application.CutCopyMode = False      'Clear the copy command

    'Now collect the result in cell I4 and paste it back into column B using the rCell
    'for that student number matches the st num in col A


        'PGCodeRider CHANGED!!!!!!!!!!!!!!
        wbmaster.Sheets("studentlist").Range("B" & 2 + i).Value = _
            wbtarget.Sheets("Answers").Range("I4").Value

        Application.CutCopyMode = False      'Clear the copy command

     'now save and close the student file...
        wbtarget.Close (True)

        i = i + 1      'increment i for next pasteback


    Next rCell   '<                            | next student number
   'save the results file
   wbmaster.Save


       ActiveSheet.DisplayPageBreaks = True '<    | turn back on page breaks once all done

'turn screen & alerts back on
With Application
.ScreenUpdating = True: .DisplayAlerts = True
'.DisplayPageBreaks = True
End With
End Sub


Sub timerMACRO()
'Run this if you want to run your macro and then get a timed result
Dim beginTime As Date: beginTime = Now

Call Examnew_NEW

MsgBox DateDiff("S", beginTime, Now) & " seconds."

End Sub

谢谢你:我想是控制力太强了。谢谢你:我想是控制力太强了。方法一不想玩?但是方法2很好,很高兴方法2奏效了。对于1,可能是我在编辑框中键入的方式。1的一个简化示例是RangeA1。复制目标:=RangeB1方法1不想播放?但是方法2很好,很高兴方法2奏效了。对于1,可能是我在编辑框中键入的方式。1的一个简化示例是RangeA1。复制目标:=RangeB1