VBA 仅将单元格的某些值从一个 sheet 粘贴到另一个 sheet
VBA to paste only certain values of cell from one sheet to another
有人可以帮我写下面的代码吗,我要找的是 sheet "Form" 2 组数组中提到的单元格的某些值。
第一组数组应该复制到 sheet "Tracker" C3 之后,第二组数组从下一个单元格开始,在第一组数组结束后说 EF3 之后。
而现在第一个设置是从 A3 粘贴,第二个是从 A4 粘贴。如有任何问题,请告诉我。
以下是我现在使用的代码:
Sub AddEntry()
Dim LR As Long, i As Long, cls
Dim LR2 As Long, j As Long, cls2
cls = Array("C2", "C3", "G2", "G3", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "A17", "C17", "D17", "F17", "G17", "H17", "A18", "C18", "D18", "F18", "G18", "H18", "A19", "C19", "D19", "F19", "G19", "H19", "A20", "C20", "D20", "F20", "G20", "H20", "A21", "C21", "D21", "F21", "G21", "H21", "A25", "B25", "C25", "D25", "E25", "F25", "G25", "H25", "A26", "B26", "C26", "D26", "E26", "F26", "G26", "H26", "A27", "B27", "C27", "D27", "E27", "F27", "G27", "H27", "A28", "B28", "C28", "D28", "E28", "F28", "G28", "H28", "A32", "C32", "E32", "G32", "H32", "A33", "C33", "E33", "G33", "H33", "A34", "C34", "E34", "G34", "H34", "A35", "C35", "E35", "G35", "H35", "A39", "D39", "F39", "A40", "D40", "F40", "A41", "D41", "F41", "A45", "C45", "E45", "G45", "A46", "C46", "E46", "G46", "A47", "C47", "E47", "G47", "D51", "D52", "D53", "D54", "D55", "D56", "D57", "D58", "D59", "D60", "D61", "D62", "D63", "D64", "D65", "D66", "D67")
With Sheets("Tracker")
LR = WorksheetFunction.Max(3, .Range("C" & Rows.Count).End(xlUp).Row + 1)
For i = LBound(cls) To UBound(cls)
.Cells(LR, i + 1).Value = Sheets("Form").Range(cls(i)).Value
Next i
End With
cls2 = Array("E51", "E52", "E53", "E54", "E55", "E56", "E57", "E58", "G59", "E60", "E61", "E62", "G63", "E64", "E65", "E66", "E67", "C70", "D70", "E70", "F70", "G70", "H70", "C71", "E71", "G71", "C72", "E72", "G72", "C73", "E73", "G73", "C74", "E74", "G74", "C75", "E75", "G75", "C76", "E76", "G76", "C77", "E77", "G77", "C78", "E78", "G78", "C79", "E79", "G79", "C82", "D82", "E82", "F82", "G82", "H82", "C83", "E83", "G83", "C84", "E84", "G84", "B88", "B89", "B90", "B91", "C88", "C89", "C90", "C91", "D88", "D89", "D90", "D91", "E88", "E89", "E90", "E91", "F88", "F89", "F90", "F91", "G88", "G89", "G90", "G91", "H88", "H89", "H90", "H91")
With Sheets("Tracker")
LR2 = WorksheetFunction.Max(3, .Range("EW" & Rows.Count).End(xlUp).Row + 1)
For j = LBound(cls2) To UBound(cls2)
.Cells(LR, j + 1).Value = Sheets("Form").Range(cls2(j)).Value
Next j
End With
End Sub
我看了你的档案;我做的第一件事是翻阅 VBA & try 来编译它——顺便说一句,我会向任何人推荐作为 第一步 与下载 XLSM
。 (我还没有看到恶意宏,我想保持这种状态!)
我可以看到这个文件是 "work in progress" 因为这里那里有一些代码没有正确编译,例如 Me
语句指向丢失的用户窗体,并且对 mis-named 工作表的引用,例如 Form (View)
而不是 View_Form
.
理想情况下,应将此项目从 Excel 移动到 Access。 Excel 可以 用于填写表单和存储数据,但如果这可能会变得相当大,您最好使用 "the right tool for the job"。将您的表单复制到 Access 表单中立即消除了将某些单元格复制到某些工作表的需要,更不用说验证、报告、安全性和无限扩展空间以及在 Excel、Access 之间轻松移动数据的需要了, 展望等
(你甚至在一个地方称电子表格为数据库!)如果你担心你不熟悉 Access,如果你设计了这个工作簿,迁移到 Access 将是一个 breeze 一旦你明白table 和表单设计的基础知识。
甚至 Outlook 也有一些非常漂亮的表单功能,可以在收到电子邮件表单时自动填充数据 table。
如果您需要留在 Excel,用用户表单代替 sheet-based 表单怎么样?我经常看到人们忘记了 Office 的 built-in 功能并从头开始。也就是说,我使用 MS Office 已有 25 年,从未 使用过 Excel 用户表单。 当我想到"form"时,我想到MS Access。
另一种选择,如果您想继续使用工作sheet-based 形式,而不是列出数组中的所有单元格等,稍作重新设计可以使其更简单。一种方法是在表单选项卡上有一个隐藏行,这样您就可以在一个不间断的行中存储所有需要存储的数据。例如,您可以隐藏第 1 行和第 2 行,将第 1 行的标题设置为 Sourced
Processed
Year
Address
等,然后第 2 行可以是 "interim"存放数据的地方,所以A2公式为=C2
,B2为=C3', B3 is
=C5`等
最后,另一个偷偷摸摸的选项可能是在每个包含需要保存数据的单元格中添加 隐藏评论,然后当表单完成时,遍历所有单元格寻找评论,每个评论都包含一个标题或单元格引用,指示该单元格的数据需要去哪里。
目的地应该非常简单 table 根据需要使用尽可能多的列,但它不是用于格式化或公式的地方。 (想想数据库!)
例如,C2
(来源)可能有一个 隐藏 评论,例如 "Tracker:C",然后当填写表单时,您可以解析评论并动态移动数据(而不是硬编码 250 个单元格地址!)使用类似:
Option Explicit
Sub moveData() 'untested; example only
Dim cell As Variant, nextBlankRow As Integer
Dim comm As String, sht As String, col As String
nextBlankRow = 5 'calculate this somehow
'loop through cells with comments
For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeComments)
If cell.Comment.Text <> "" Then
'get comment
comm = cell.Comment.Text
'extract location for data like "Sheetname:Columnletter"
sht = Left(comm, InStr(comm, ":") - 1)
col = Right(comm, Len(comm) - InStr(comm, ":"))
'populate correct location with data
Sheets(sht).Range(col & nextBlankRow).Value = cell.Value
End If
Next cell
End Sub
与 Excel(或一般办公室)中的任何内容一样,您可以通过多种方式完成相同的任务。选择那些不涉及一遍又一遍地重复相同代码或硬编码数据的代码。规划未来(意外的)增长非常重要,调试也很重要 as-you-go,这是我最后的建议:
Option Explicit
在每个模块的顶部,Alt+DL经常编译,删除或 commenting-out 未使用的代码。
底线,最好的选择:Access、Excel、Outlook 都具有内置的表单功能。将表单用于表单,您现在就不会头疼了 and 稍后.
希望这能给你一些想法。
祝你好运!
假设你想在sheet"Tracker"开始单元格条目更靠右,你可以添加列号而不是+1(=列A)并编写如下:
数组 1:从 C 列开始分配单元格值
.Cells(LR, i + [C1].Column).Value = Sheets("Form").Range(cls(i)).Value
数组 2:从列 EF
开始分配单元格值
' should be LR2 instead of LR :-)
.Cells(LR2, j + [EF1].Column).Value = Sheets("Form").Range(cls2(j)).Value
备注
[C1].column
returns 栏目编号(在任何作品中sheet),例如C 列计数 3.
有人可以帮我写下面的代码吗,我要找的是 sheet "Form" 2 组数组中提到的单元格的某些值。 第一组数组应该复制到 sheet "Tracker" C3 之后,第二组数组从下一个单元格开始,在第一组数组结束后说 EF3 之后。
而现在第一个设置是从 A3 粘贴,第二个是从 A4 粘贴。如有任何问题,请告诉我。
以下是我现在使用的代码:
Sub AddEntry()
Dim LR As Long, i As Long, cls
Dim LR2 As Long, j As Long, cls2
cls = Array("C2", "C3", "G2", "G3", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "A17", "C17", "D17", "F17", "G17", "H17", "A18", "C18", "D18", "F18", "G18", "H18", "A19", "C19", "D19", "F19", "G19", "H19", "A20", "C20", "D20", "F20", "G20", "H20", "A21", "C21", "D21", "F21", "G21", "H21", "A25", "B25", "C25", "D25", "E25", "F25", "G25", "H25", "A26", "B26", "C26", "D26", "E26", "F26", "G26", "H26", "A27", "B27", "C27", "D27", "E27", "F27", "G27", "H27", "A28", "B28", "C28", "D28", "E28", "F28", "G28", "H28", "A32", "C32", "E32", "G32", "H32", "A33", "C33", "E33", "G33", "H33", "A34", "C34", "E34", "G34", "H34", "A35", "C35", "E35", "G35", "H35", "A39", "D39", "F39", "A40", "D40", "F40", "A41", "D41", "F41", "A45", "C45", "E45", "G45", "A46", "C46", "E46", "G46", "A47", "C47", "E47", "G47", "D51", "D52", "D53", "D54", "D55", "D56", "D57", "D58", "D59", "D60", "D61", "D62", "D63", "D64", "D65", "D66", "D67")
With Sheets("Tracker")
LR = WorksheetFunction.Max(3, .Range("C" & Rows.Count).End(xlUp).Row + 1)
For i = LBound(cls) To UBound(cls)
.Cells(LR, i + 1).Value = Sheets("Form").Range(cls(i)).Value
Next i
End With
cls2 = Array("E51", "E52", "E53", "E54", "E55", "E56", "E57", "E58", "G59", "E60", "E61", "E62", "G63", "E64", "E65", "E66", "E67", "C70", "D70", "E70", "F70", "G70", "H70", "C71", "E71", "G71", "C72", "E72", "G72", "C73", "E73", "G73", "C74", "E74", "G74", "C75", "E75", "G75", "C76", "E76", "G76", "C77", "E77", "G77", "C78", "E78", "G78", "C79", "E79", "G79", "C82", "D82", "E82", "F82", "G82", "H82", "C83", "E83", "G83", "C84", "E84", "G84", "B88", "B89", "B90", "B91", "C88", "C89", "C90", "C91", "D88", "D89", "D90", "D91", "E88", "E89", "E90", "E91", "F88", "F89", "F90", "F91", "G88", "G89", "G90", "G91", "H88", "H89", "H90", "H91")
With Sheets("Tracker")
LR2 = WorksheetFunction.Max(3, .Range("EW" & Rows.Count).End(xlUp).Row + 1)
For j = LBound(cls2) To UBound(cls2)
.Cells(LR, j + 1).Value = Sheets("Form").Range(cls2(j)).Value
Next j
End With
End Sub
我看了你的档案;我做的第一件事是翻阅 VBA & try 来编译它——顺便说一句,我会向任何人推荐作为 第一步 与下载 XLSM
。 (我还没有看到恶意宏,我想保持这种状态!)
我可以看到这个文件是 "work in progress" 因为这里那里有一些代码没有正确编译,例如 Me
语句指向丢失的用户窗体,并且对 mis-named 工作表的引用,例如 Form (View)
而不是 View_Form
.
理想情况下,应将此项目从 Excel 移动到 Access。 Excel 可以 用于填写表单和存储数据,但如果这可能会变得相当大,您最好使用 "the right tool for the job"。将您的表单复制到 Access 表单中立即消除了将某些单元格复制到某些工作表的需要,更不用说验证、报告、安全性和无限扩展空间以及在 Excel、Access 之间轻松移动数据的需要了, 展望等
(你甚至在一个地方称电子表格为数据库!)如果你担心你不熟悉 Access,如果你设计了这个工作簿,迁移到 Access 将是一个 breeze 一旦你明白table 和表单设计的基础知识。
甚至 Outlook 也有一些非常漂亮的表单功能,可以在收到电子邮件表单时自动填充数据 table。
如果您需要留在 Excel,用用户表单代替 sheet-based 表单怎么样?我经常看到人们忘记了 Office 的 built-in 功能并从头开始。也就是说,我使用 MS Office 已有 25 年,从未 使用过 Excel 用户表单。 当我想到"form"时,我想到MS Access。
另一种选择,如果您想继续使用工作sheet-based 形式,而不是列出数组中的所有单元格等,稍作重新设计可以使其更简单。一种方法是在表单选项卡上有一个隐藏行,这样您就可以在一个不间断的行中存储所有需要存储的数据。例如,您可以隐藏第 1 行和第 2 行,将第 1 行的标题设置为 Sourced
Processed
Year
Address
等,然后第 2 行可以是 "interim"存放数据的地方,所以A2公式为=C2
,B2为=C3', B3 is
=C5`等
最后,另一个偷偷摸摸的选项可能是在每个包含需要保存数据的单元格中添加 隐藏评论,然后当表单完成时,遍历所有单元格寻找评论,每个评论都包含一个标题或单元格引用,指示该单元格的数据需要去哪里。
目的地应该非常简单 table 根据需要使用尽可能多的列,但它不是用于格式化或公式的地方。 (想想数据库!)
例如,C2
(来源)可能有一个 隐藏 评论,例如 "Tracker:C",然后当填写表单时,您可以解析评论并动态移动数据(而不是硬编码 250 个单元格地址!)使用类似:
Option Explicit
Sub moveData() 'untested; example only
Dim cell As Variant, nextBlankRow As Integer
Dim comm As String, sht As String, col As String
nextBlankRow = 5 'calculate this somehow
'loop through cells with comments
For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeComments)
If cell.Comment.Text <> "" Then
'get comment
comm = cell.Comment.Text
'extract location for data like "Sheetname:Columnletter"
sht = Left(comm, InStr(comm, ":") - 1)
col = Right(comm, Len(comm) - InStr(comm, ":"))
'populate correct location with data
Sheets(sht).Range(col & nextBlankRow).Value = cell.Value
End If
Next cell
End Sub
与 Excel(或一般办公室)中的任何内容一样,您可以通过多种方式完成相同的任务。选择那些不涉及一遍又一遍地重复相同代码或硬编码数据的代码。规划未来(意外的)增长非常重要,调试也很重要 as-you-go,这是我最后的建议:
Option Explicit
在每个模块的顶部,Alt+DL经常编译,删除或 commenting-out 未使用的代码。
底线,最好的选择:Access、Excel、Outlook 都具有内置的表单功能。将表单用于表单,您现在就不会头疼了 and 稍后.
希望这能给你一些想法。
祝你好运!
假设你想在sheet"Tracker"开始单元格条目更靠右,你可以添加列号而不是+1(=列A)并编写如下:
数组 1:从 C 列开始分配单元格值
.Cells(LR, i + [C1].Column).Value = Sheets("Form").Range(cls(i)).Value
数组 2:从列 EF
开始分配单元格值 ' should be LR2 instead of LR :-)
.Cells(LR2, j + [EF1].Column).Value = Sheets("Form").Range(cls2(j)).Value
备注
[C1].column
returns 栏目编号(在任何作品中sheet),例如C 列计数 3.