简体   繁体   English

这段代码是错误的还是我使用不正确?

[英]Is this code wrong or I'm not using it correctly?

I was reviewing this piece of code 我正在查看这段代码

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Column = 1 And Target.Offset(1, 0) = "" Then
        Target.Offset(-1, 0).Copy
        Target.PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
    End If
End Sub

Basically what this is supposed to do is to copy the format of the cell above the one you are typing/pasting data so if you paste several cells it will start copying the format down to all the cells. 基本上,这是要复制要键入/粘贴数据的单元格上方的单元格格式,因此,如果粘贴多个单元格,它将开始将格式向下复制到所有单元格。

I've tested this code but most of the times I get an error '13 runtime the other thing is when it does work it only works for Column A, but for some reason the code is not stable its really flaky and take about 1.3 seconds for it to work. 我已经测试了这段代码,但是大多数时候我会收到一个错误信息'13运行时,另一件事是,它只能在A列上工作,但是由于某种原因,代码不稳定,它确实很不稳定,大约需要1.3秒为它工作。 and it does not work when I paste the info into the cell just if I type it in which the actual purpose of the code is exactly that to force any type of data which is pasted to have the format of the cell above it 并且当我将信息粘贴到单元格中时,即使我在其中键入代码(代码的实际目的恰恰是要强制粘贴的任何类型的数据都具有其上方的单元格格式),它也不起作用

How can we make this code to copy/paste the format of the cell above the one I'm inputting data but to be applicable to all Columns not just Column A 我们如何制作此代码来复制/粘贴单元格的格式,使其高于我正在输入数据的单元格的格式,但不仅适用于列A,而且还适用于所有列

Furthermore, is there a way to make the code run faster? 此外,有没有办法使代码运行更快? If I delete anything I also get the Error '13. 如果删除任何内容,也会出现错误'13。 Also if I paste the code into an excel sheet which I already worked on it won't work at all. 另外,如果我将代码粘贴到我已经处理过的Excel工作表中,将根本无法工作。

I mentioned the Event loop in the comment above, it's important to be mindful of that when using event handlers. 我在上面的注释中提到了事件循环,使用事件处理程序时请务必注意这一点。

This line limits your code to Column A only, specifically the bit before the "And": 此行将您的代码限制为仅A列,特别是“ And”之前的位:

If Target.Column = 1 And Target.Offset(1, 0) = "" Then

I'm going to make those changes here, but also limit it to exclude Row 1 (you'll get an error on Target.Offset(-1,0) if you you do that in row 1). 我将在此处进行这些更改,但也将其限制为排除第1行(如果在第1行执行此操作,则会在Target.Offset(-1,0)上收到错误)。

I am also going to limit it to only work on a target of a single cell. 我还将限制它仅适用于单个单元格的目标。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.EnableEvents = False
    If Not Target.Cells.Count = 1 Or Target.Row = 1 Then Exit Sub
    If Target.Offset(1, 0) = "" Then
        Target.Offset(-1, 0).Copy
        Target.PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
    End If
    Application.EnableEvents = True
End Sub

Of course it is possible to use the event handler on a range of cells, but then we would get an error on Target.Offset(1,0) (which is really asking for the .Value of that cell, which will fail if it is a range of multiple cells .). 当然可以在一系列单元格上使用事件处理程序,但是随后我们会在Target.Offset(1,0)上收到错误Target.Offset(1,0) (这实际上是在询问该单元格的.Value ,如果它的值是多个单元格的范围。)。 Like I said, there's ways around it, but without actually knowing what you need, it's hard to make precise suggestion. 就像我说的那样,有很多解决方法,但是如果不真正知道您的需求,就很难提出准确的建议。

This might work; 这可能有效; still limited to a single row but I think will work for multiple cells. 仍限于单一 ,但我认为将多个小区的工作。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.EnableEvents = False
    Dim nextRow as Variant

    If Not Target.Rows.Count = 1 Or Target.Row = 1 Then Exit Sub

    'store the next row in an array:
    nextRow = Application.Transpose(Application.Transpose(Target.Offset(1, 0).Value)

    If Join(nextRow, "") = "" Then
        Target.Offset(-1, 0).Copy
        Target.PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
    End If
    Application.EnableEvents = True
End Sub

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

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