在宏复制粘贴中设置 TargetCell 的问题

Issue with setting TargetCell in macro copy paste

我是这个论坛的新手,想寻求一些宏方面的帮助。 我有两个工作表:一个用于输入数据(“DataEntry”),另一个用于存储数据(“DataSheet”)。整个想法是,用户可以在 DataEntry 工作表中添加他想要的任意数量的数据,并且所有条目都按连续顺序列出,从 DataSheet!F10 开始,然后继续 F11、F12 等。 我已经使用这个论坛的一些代码来完成整个复制粘贴操作,但由于某些原因无法在 F10 上设置 TargetCell。 所有这些条目都应使用浅黄色字体,并在 E10 中以 1 开始编号,在 E11 中以 2 开始编号,依此类推。

期待阅读您的解决方案!谢谢

编码如下:

Sub ConstrProgramme_addition()

    Dim DataEntry As Worksheet, DataSht As Worksheet
    Dim ItemName As Range, ItemCount As Range
    Dim NRow As Long, TargetCell As Range

    With ThisWorkbook
        Set DataEntry = .Sheets("DataEntry")
        Set DataSht = .Sheets("Datasheet")
    End With

    With DataEntry
        Set ItemName = .Range("C4")
        Set ItemCount = .Range("E4")
    End With

    With DataSht
        
        If IsEmpty(Range("F10")) = True Then
        Set TargetCell = .Range("F10")
        Else
        Set TargetCell = ActiveCell.Offset(NRow, 0).Select
    
        
        NRow = .Range("F" & Rows.Count).End(xlUp).Row + 1
        
        End If
        
        TargetCell.Resize(ItemCount.Value, 1).Value = ItemName.Value
    End With

End Sub

你可以缩短一点:

Sub ConstrProgramme_addition()

    Dim DataEntry As Worksheet, TargetCell As Range

    Set DataEntry = ThisWorkbook.Sheets("DataEntry")
    
    With ThisWorkbook.Sheets("Datasheet")
        Set TargetCell = .Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0)
        If TargetCell.Row < 10 Then Set TargetCell = .Range("F10")
        TargetCell.Resize(DataEntry.Range("E4").Value, 1).Value = DataEntry.Range("C4").Value
    End With

End Sub