【问题标题】:Copy data validations only from a single row from one table to all rows of another table仅将数据验证从一个表的单行复制到另一个表的所有行
【发布时间】:2016-03-24 18:36:40
【问题描述】:

我只想将数据验证从工作表 TEMPLATE (Maint.) 上名为 Table1_1 的表中复制到另一个名为 的工作表上的表中模板。我已经查看了可用的主题,但没有一个与我正在寻找的主题非常接近。

其中一个问题是这些工作表中的任何一张上的表格最终可能会移动,因此当我构建此宏时,我需要考虑到这一点。

到目前为止,我拥有的是:

  1. 复制工作表 TEMPLATE (Maint.)Table1_1 的第一行(也是唯一一行)。
  2. 查看工作表 TEMPLATE 上的单元格 A3 并获取它所属的表格。
  3. 找到在步骤 2 中找到的表格的第一行。
  4. 将表格中所有列的数据验证粘贴到第一行。
  5. 对表格的所有行重复步骤 3 和 4。

我目前的代码:

Dim TotalSheets As Integer
Dim p As Integer
Dim iAnswer As VbMsgBoxResult

' This Dim is supposed to be to add worksheets, for the process _
' of copying the data validations to the new sheets, to a skip _
' list. An array perhaps? Skip any sheets listed in this array?
Dim DNCToShts As ?

' The cell to get the table that it is apart of for copying from _
' the other worksheet, "TEMPLATE (Maint.)"
Dim GetCellsTable_Copy As String
' The cell to get the table that it is apart of for pasting onto _
' the other worksheets.
Dim GetCellsTable_Paste As String

' This is the cell to reference on "TEMPLATE (Maint.)" worksheet _
' to get the table name of, this will always be "Table1_1"
GetCellsTable_Copy = "A3"
' This is the cell to reference on each sheet to get the table name.
GetCellsTable_Paste = "A3"

With Aplication
    .DisplayAlerts = False
    .ScreenUpdating = False
End With

iAnswer = MsgBox("You are about to copy data validations! Do you _
want to proceed?", vbOKCancel + vbExclamation _
+ vbDefaultButton2 + vbMsgBoxSetForeground + vbApplicationModal, _
"Copying Data Valadations")

' Instead of copying the whole table I just need to copy the first row _
' of data, intending to copy just the data validations portion.
Range("Table1_1").Copy

If iAnswer = vbYes Then
    p = 1 To Sheets.Count
        If UCase$(Sheets(p).Name) <> DNCToShts
        StoreTableName = Range(GetCellsTable_Paste).ListObject.Name

我已经创建了一个图表,显示了我的每个 Excel VBA 模块的目标。请记住,这可能不包括所有细节,我正在处理仅第 1 部分。:

【问题讨论】:

  • 请发布您的部分或全部代码以供参考
  • @TheGuyThatDoesntKnowMuch 我已经添加了我已经积累的代码。有什么你可以收集到的或者有什么需要进一步解释的吗?
  • 除了上面粘贴的内容之外,我基本上没有写太多代码。 :-/

标签: vba excel


【解决方案1】:

Excel VBA 联机帮助包含完成此操作所需的一切。仅 Validation 对象成员的帮助页面就足够了。

以下例程会将验证从一个单元格复制到另一个单元格。您应该能够在双循环中调用它(针对目标行和列)。完成测试后,这应该是 Private 函数

Sub CopyValidation(ByRef rngSourceCell As Range, ByRef rngTargetCell As Range)
    With rngTargetCell.Validation
        .Delete
        .Add Type:=rngSourceCell.Validation.Type, _
            AlertStyle:=rngSourceCell.Validation.AlertStyle, _
            Operator:=rngSourceCell.Validation.Operator, Formula1:=rngSourceCell.Validation.Formula1, Formula2:=rngSourceCell.Validation.Formula2
        .ErrorMessage = rngSourceCell.Validation.ErrorMessage
        .ErrorTitle = rngSourceCell.Validation.ErrorTitle
        .IgnoreBlank = rngSourceCell.Validation.IgnoreBlank
        .IMEMode = rngSourceCell.Validation.IMEMode
        .InCellDropdown = rngSourceCell.Validation.InCellDropdown
        .InputMessage = rngSourceCell.Validation.InputMessage
        .InputTitle = rngSourceCell.Validation.InputTitle
        .ShowError = rngSourceCell.Validation.ShowError
        .ShowInput = rngSourceCell.Validation.ShowInput
    End With
End Sub

【讨论】:

  • 让我看看我是否可以将它集成到我的代码中,这看起来很有希望。仅供参考,是的,我将其作为自己的 Sub (Sub Copy_Data_Validations()) 运行。
  • 您的意思是说“...这应该是它自己的私有函数”。而不是“...私有函数”?
  • 我应该说“私人潜艇”。 “功能”这个词用错了。它已经被写成一个 Sub。我的意思是您可以使用它来使用调试窗口对其进行测试。一旦你对它没问题,你可以在它前面添加 Private 限定符。没什么大不了的,不管怎样。 (我倾向于默认将所有内容设为私有,并且只明确将我需要公开的内容设为公开。)
  • 好吧 MikeC,没问题。这一次只能复制和粘贴一个单元格吗?可以一次粘贴一系列单元格吗?我需要一种最适合将维护选项卡中的一行验证复制到单个表的方法,对于每个选项卡上的每一行减去选择工作表。
  • 这会将数据验证从一个单元格复制到另一个单元格。这允许您将其放入循环中,将验证从您想要的任何源单元格复制到任何目标单元格(例如,一个源到多个目标)。这允许将具有不同验证的不同源单元复制到不同数量的目标单元。只需在循环中根据需要重复调​​用它即可。
猜你喜欢
  • 1970-01-01
  • 2012-11-24
  • 2023-03-16
  • 1970-01-01
  • 2015-07-18
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多