仅将数据验证从一个表中的一行复制到另一个表的所有行

时间:2016-03-24 18:36:40

标签: excel vba excel-vba

我只想将工作表 TEMPLATE(维护)上名为 Table1_1 的表中的数据验证复制到另一个名为的工作表上的表中TEMPLATE 即可。我查看了可用的主题,但没有一个与我正在寻找的内容非常接近。

其中一个问题是这些工作表中的任何一个表都可能最终移位,所以当我构建这个宏时,我需要考虑到这一点。

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

  1. 在工作表 TEMPLATE(维护)上复制 Table1_1 的第一行(也是唯一一行)。
  2. 查看工作表 TEMPLATE 上的单元格A3,并获取它所属的表格。
  3. 找到步骤2中找到的表格的第一行。
  4. 粘贴第一行表格中所有列的数据验证。
  5. 对表格的所有行重复步骤3和4。
  6. 到目前为止我的代码:

    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部分Diagram showing the sequence of sub usage

1 个答案:

答案 0 :(得分:1)

Excel VBA在线帮助包含您完成此操作所需的一切。只需验证对象成员的帮助页面即可。

以下例程会将验证从一个单元格复制到另一个单元格。您应该能够在双循环中调用它(对于目标行和列)。一旦您完成测试,这应该是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