我在 ecxel 中遇到问题,当我在一个单元格(A1)中输入数据时,当我在键盘中按回车键时,数据应该复制到另一张工作表(工作表)中,当我在该单元格(A1)上再次输入数据时,应该将数据复制到另一张工作表(工作表)中。再次复制到另一张纸(sheet2)上,但当我按回车键时,复制到下一行。我不擅长编码。
我尝试在 excel 中使用基本公式在 youtube 中搜索教程。这些都不能解决我的问题
工作表更改:将每个输入的值复制到列
- 以下屏幕截图说明了发生的情况
Sheet2
进入后1,2,3,4 and 5
进入细胞A1
of Sheet1
.
- 将以下代码复制到源工作表的工作表模块中(
Sheet1
),您将在其中输入值。
(在里面项目浏览器,双击Sheet1(Sheet1)
,然后将打开正确的窗口。)
- 观察显示的蓝色标题栏
... Sheet1 (Code)
: 注意这个Sheet1
指的是Sheet1
在左边(在项目浏览器) 不在括号内。这是工作表的代码名称。括号中的名称是选项卡名称(您在 Excel 中看到的名称),它可能不同。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Define constants.
Const SRC_CELL As String = "A1"
Const DST_SHEET As String = "Sheet2"
Const DST_FIRST_CELL As String = "A2"
' Reference the source cell.
Dim sCell As Range: Set sCell = Me.Range(SRC_CELL)
' Check if the source cell was not changed.
If Intersect(sCell, Target) Is Nothing Then Exit Sub
' Reference the first destination cell.
Dim dCell As Range:
Set dCell = Me.Parent.Sheets(DST_SHEET).Range(DST_FIRST_CELL)
With dCell
' Reference the destination range, the range starting from
' the first destination cell to the last cell in the column
' i.e. e.g. for 'A2' this means the range 'A2:A1048576'.
Dim drg As Range: Set drg = .Resize(Me.Rows.Count - .Row + 1)
' Attempt to reference the last (bottom-most) non-empty cell
' of the destination range.
Dim dlCell As Range:
Set dlCell = drg.Find("*", , xlFormulas, , , xlPrevious)
' If the attempt was succesful i.e. there is a non-empty cell,...
If Not dlCell Is Nothing Then
' ... check if the last cell is not the last cell
' of the destination range (there is no cell below it).
If dlCell.Row = Me.Rows.Count Then Exit Sub
' Reference the cell below the last cell.
Set dCell = dlCell.Offset(1)
'Else ' If the attempt failed i.e. all cells are empty,...
' ... do nothing i.e. use the already referenced destination cell.
End If
End With
' Write the value from the source cell to the destination cell.
dCell.Value = sCell.Value
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)