简体   繁体   English

粘贴值的VBA Excel错误1004问题

[英]VBA Excel ERROR 1004 problems with Paste Value

I am kind of new with VBA and I have a problem I cannot solve and cannot find the right solutions in earlier questions. 我对VBA有点VBA ,但有一个我无法解决的问题,无法在较早的问题中找到正确的解决方案。 In fact it looks quite simple. 实际上,它看起来很简单。 I want to copy a range in worksheet with the name "Blad3" and paste the values in in worksheet with the name "Blad1" . 我想在工作表中复制一个名称为"Blad3" ,并将值粘贴在工作表中,命名为"Blad1" This is what I made and where does it go wrong? 这是我做的,哪里出错了?

    ActiveSheet.Cells(dattel, 4).Select

    ActiveCell.Range("A1:J1").Copy

    Sheets("Blad1").Select
    Cells(8 + aantkk, 6).Select
    ActiveSheet.Unprotect
'    ActiveCell.PasteSpecial xlPasteValues
    Selection.PasteSpecial Paste:=xlValue
'    ActiveSheet.Paste

    ActiveSheet.Protect

Would this work for you? 这对您有用吗?

Sheets("Blad3").Range("A1:J1").Copy Sheets(“ Blad3”)。Range(“ A1:J1”)。Copy

Sheets("Blad1").Range("A1:J1").PasteSpecial Paste:=xlValue Sheets(“ Blad1”)。Range(“ A1:J1”)。PasteSpecial Paste:= xlValue

I tested it on a new workbook and it seemed to work just fine. 我在新的工作簿上对其进行了测试,它似乎工作正常。

First, the real answer to your dilemma is to protect the worksheet with the UserInterfaceOnly:=True parameter so that you do not have to Unprotect it to write values using VBA code. 首先,真正解决难题的办法是使用UserInterfaceOnly:= True参数保护工作表 ,以便您不必取消保护它即可使用VBA代码写入值。

Run this once. 运行一次。

sub protectBlad1FromUser()
    worksheets("Blad1").unprotect
    worksheets("Blad1").protect UserInterfaceOnly:=True
end sub

Now you can do anything you want to the Blad1 worksheet in VBA while protecting it from the user. 现在,您可以对VBA中的Blad1工作表执行任何操作,同时保护它免受用户的攻击。

As to your original code, it is confusing. 至于您的原始代码,这很令人困惑。 If .Cells(dattel, 4) is D4 on the Blad3 worksheet then ActiveCell.Range("A1:J1").Copy doesn't copy A1:J1; 如果.Cells(dattel, 4)在Blad3工作表上为D4,则ActiveCell.Range("A1:J1").Copy不会复制A1:J1; it copies D4:M4. 它复制D4:M4。 In any event, direct value transfer is a more efficient method of transferring values than Copy, Paste Special, Values. 无论如何,直接价值转移是一种比复制,特殊粘贴,价值更有效的价值转移方法。

dim rng as range
set rng = worksheets("blad3").cells(dattel, 4).resize(1, 10)  '<~~ figure out what this is supposed to be
with worksheets("Blad1")
    .cells(8 + aantkk, 6).resize(rng.rows.count, rng.columns.count) = rng.value
end with

May be try this 可以试试这个

Sub Demo()
    Dim srcSht As Worksheet, destSht As Worksheet
    Dim rng As Range
    Set srcSht = ThisWorkbook.Sheets("Blad3")   'this is source sheet
    Set destSht = ThisWorkbook.Sheets("Blad1")  'this is destination sheet

    With destSht
        .Unprotect                           'unprotect sheet Blad1
        Set rng = srcSht.Range("A1:J1")      'set range to copy
        .Cells(8 + aantkk, 6).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value 'paste only values
        .Protect                             'protect sheet Blad1
    End With
End Sub

SELECT and ACTIVATE should be avoided. 应该避免SELECTACTIVATE See this for details. 请参阅了解详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM