gpt4 book ai didi

excel - 宏 - 将同一行中的某些单元格(由复选框触发)复制到不同的工作表

转载 作者:行者123 更新时间:2023-12-03 03:16:47 25 4
gpt4 key购买 nike

需要一些帮助。我有一个模板,可以将数据从不同的程序导出到其中。数据行因导出而异,每次导出都需要一个新的工作簿。

我目前编写了一个“主”宏,用于清理工作表(格式、文本到数字等),并在包含数据的每行末尾添加复选框。这些复选框链接到一个单元格。运算符(operator)完成工作表后,他们将需要选中“不符合规范”的每行数据的复选框。然后,这些行将被复制到工作簿中的下一张工作表上。这是由按钮触发的。当我只想复制“A”列到“I”列中的单元格时,我当前的宏除了复制整行数据外,还可以工作。 “J”列及其列中的单元格包含不需要复制的数据。

这是我当前的宏,正如我所说,它复制整行:

Sub CopyRows() 
Dim LRow As Long, ChkBx As CheckBox, WS2 As Worksheet
Set WS2 = Worksheets("T2 FAIR (Single Cavity)")
LRow = WS2.Range("A" & Rows.Count).End(xlUp).Row

For Each ChkBx In ActiveSheet.CheckBoxes
If ChkBx.Value = 1 Then
LRow = LRow + 1
WS2.Cells(LRow, "A").Resize(, 14) = Range("A" & _
ChkBx.TopLeftCell.Row).Resize(, 14).Value
End If
Next
End Sub

最佳答案

在等式的右侧,您的 Range()对象未正确限定(使用工作表)。所以,我用了假的wsX在这个例子中。

此外,我使用了“D”结尾栏 - 但您可以更改为您需要的任何内容。

LRow = LRow + 1
r = ChkBx.TopLeftCell.Row

ws2.Range(ws2.Cells(LRow, "A"), ws2.Cells(LRow, "D")) = wsX.Range( _
wsX.Cells(r, "A"), wsX.Cells(r, "D"))

ws2.Range("A" & LRow & ":D" & LRow) = wsX.Range("A" & r & ":D" & r)
<小时/>

From Comment: The templates ALWAYS start, with the imported data, in "A19". When I run this macro, to copy the checked data to the next worksheet, it starts in with cell "A18". I have no idea as to why. How do I specify that the checked data is to be copied starting with "A19" on the next worksheet?

If it's always off by one, you can just add 1. I am not sure how your layout is, so this will be something you will have to either add to LRow or r. So either

ws2.Range("A" & LRow + 1 & ":D" & LRow + 1) = ...

or

... = wsX.Range("A" & r + 1 & ":D" & r + 1)

关于excel - 宏 - 将同一行中的某些单元格(由复选框触发)复制到不同的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53053599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com