复制&在另一个工作簿中按下按钮时粘贴与条件匹配的行

发布于 2024-12-13 02:38:25 字数 915 浏览 2 评论 0原文

我不是 VBA 专家,因为我最近开始研究这个问题。

我想知道以下是否可能。

我有几个工作簿和三个工作表(旧的、摘要的、新的)。

条件是,名称为 New 的工作表中 D 列中值为 H 或 M 的所有单元格将指示必须将整行复制到同一工作簿中名称为 Summary 的工作表中。

当按下另一个工作簿中的按钮时,应该发生上面提到的情况,并且理想情况下文件/工作簿的名称应该是可更改的,以便我可以选择要运行宏的工作簿。

理想情况下,包含数据的工作簿不应包含任何宏,所有宏都应从带有按钮的第二个工作簿运行。

如果可能并且不太复杂,我将不胜感激带有注释的示例代码(以便我可以学习)。

如果有不清楚的地方,请不要犹豫提出任何问题...

更新

这是我设法编写的宏,查看不同的代码

Sub CopyPaste()

Set NewWorkPlan = Sheets("New Workplan")
Set NewExecSummary = Sheets("New Exec Summary")
Dim d
Dim j

d = 1
j = 2

Do Until j = 200

  If NewWorkPlan.Range("D" & j) = "M" Or NewWorkPlan.Range("D" & j) = "H" Then
    d = d + 1
    NewExecSummary.Rows(d).Value = NewWorkPlan.Rows(j).Value

  End If
  j = j + 1
 'MsgBox (j)

Loop    

End Sub

这对我来说非常适合,并向前迈出了一步我希望能够从另一个工作簿执行此操作,使目标工作簿宏保持空闲。

问题是我想要运行宏的工作簿将被共享,并且我认为您无法运行宏。

I am not an expert on VBA as I've recently started looking into this.

I wonder if the following is possible.

I have several workbooks with three worksheets (Old,Summary,New).

The condition is that all cells in column D with value H or M in worksheet with name New will indicate that the entire row would have to be copied to the worksheet with name Summary in the same workbook.

This mentioned above should have to happen when a button from another workbook is pressed and ideally the name of the file/workbook should be changeable so I can select what workbook do I want to run the macro.

Ideally the workbook with the data should not have any macro, all macros should be run from the second workbook with the button.

I would appreciate an example code with comments (so I can learn) if this is possible and not too complicated.

Please don't hesitate on making any questions if something is unclear...

UPDATE

Here is the macro I manage to compose looking at different codes

Sub CopyPaste()

Set NewWorkPlan = Sheets("New Workplan")
Set NewExecSummary = Sheets("New Exec Summary")
Dim d
Dim j

d = 1
j = 2

Do Until j = 200

  If NewWorkPlan.Range("D" & j) = "M" Or NewWorkPlan.Range("D" & j) = "H" Then
    d = d + 1
    NewExecSummary.Rows(d).Value = NewWorkPlan.Rows(j).Value

  End If
  j = j + 1
 'MsgBox (j)

Loop    

End Sub

This works perfect for me, and to take it an step forward I'd like to be able to execute this from another workbook, leaving target workbook macro free.

The problem is that the workbook I want to run the macro is going to be shared and I don't think you can run macros.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-12-20 02:38:26

只需更改对所需工作簿的引用,它应该可以正常工作(只要其他工作簿打开,但如果没有打开,您也可以从代码中打开它。

参考:

Set NewExecSummary = Workbooks("TargetWorkbook").Sheets("New Exec Summary")

Just change your references to the workbook you want and it should work fine (as long as the other workbook is open, though if it's not, you can open it from code also.

Reference:

Set NewExecSummary = Workbooks("TargetWorkbook").Sheets("New Exec Summary")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文