英文:
Excel - automatically add row, once the cell is populated in another Sheet
问题
在Sheet1中,我有A和B两列。它们是空的。
在Sheet2中,我也有A、B、C三列。在Sheet2中,A和C列已经填写,而B列是空的,用户需要手动填写。
一旦我在Sheet2中为任何现有行填写了B列,我应该将具有A和B值的该行复制并粘贴到Sheet1中,为Sheet1的A和B列创建一个新行,值与Sheet2中的相同(A和B列)。
Sheet1具有一些不同的计算,这些计算应该保留在单独的工作表中,所以我必须为创建新行进行这种自动化。
这种做法可行吗?
英文:
In Sheet1 I have columns A and B. Those are empty.
In Sheet2 I have columns A,B,C as well. In Sheet2 A and C columns are ppoulated, while B column is empty, and users need to populate this manually.
Once I populate the B column in Sheet2 for any of the existing rows, I should copy that row with A and B values and paste it to the Sheet1, by creating a new row in Sheet1 for A and B columns with same values ( A and B columns) from the Sheet2.
That Sheet1 has some different calculation that should be kept in separate sheet, so I must make this kind of automation for creation of new row.
Is this feasible somehow?
答案1
得分: 2
以下是已翻译的内容:
这将类似于:
Private Sub Worksheet_Change(ByVal Target As Range)
'有些内容已更改,是否在B列中?
如果目标范围与Sheet2.Range("B:B")有交集 Then
'更改发生在B列中。
'让我们找出Sheet1中最后一个未使用的行
lastRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row + 1
'现在我们将从A和B中获取值并将它们放入Sheet1
Sheet1.Range("A" & lastRow).Value = Target.Offset(, -1).Value '更改的行的A列
Sheet1.Range("B" & lastRow).Value = Target '更改的行的B列
End If
End Sub
这段代码放在Sheet2的代码页中。
英文:
This will look something like:
Private Sub Worksheet_Change(ByVal Target As Range)
'Something changed, was it in column B?
If Not Intersect(Target, Sheet2.Range("B:B")) Is Nothing Then
'The change was in column B.
'Lets figure out the last unused row in Sheet1
lastRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row + 1
'Now we will grab our values from A and B and stick them in Sheet1
Sheet1.Range("A" & lastRow).Value = Target.Offset(, -1).Value 'col A for the row that changed
Sheet1.Range("B" & lastRow).Value = Target 'col B for the row that changed
End If
End Sub
That goes in to Sheet2's code page.
答案2
得分: 0
在Excel 365或Excel 2021中,FILTER
公式应该可以胜任(参见此处以获取帮助)。
=FILTER(Sheet2!A:B, NOT(ISBLANK(B:B)))
这将创建一个宽度为2列的自动扩展范围,无论您在何处输入此公式。
然后,要从自动扩展范围构建其他公式,您可以参考我在另一个答案中输入的方法/公式。
英文:
In Excel 365 or Excel 2021, a FILTER
formula should do the trick (see here for help).
=FILTER(Sheet2!A:B, NOT(ISBLANK(B:B)))
This creates a 2-column-wide spilled range wherever you type this formula.<br/>Then, to build other formulas from the spill range, you can refer to the method/formulas I had typed in that other answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论