简体   繁体   English

excel中强制单元格的验证总结

[英]Validation summary of mandatory cells in excel

I have got an excel workbook, it has 5 static tabs and more tabs can be created using a template tab.我有一个 excel 工作簿,它有 5 个 static 选项卡,并且可以使用模板选项卡创建更多选项卡。 In each tab there is a certain field or a range that is mandatory to be filled out also in the new created tabs (might be up to 60).在每个选项卡中,必须在新创建的选项卡中填写某个字段或范围(最多可能为 60 个)。

My question is how can I go about seeing in, lets say in mainsheet, a summary which shows me:我的问题是我怎么能 go 关于看到,让我们在主表中说,一个向我展示的摘要:

  1. Which tab has missing fields哪个选项卡缺少字段
  2. Which fields is missing (an address of a cell)缺少哪些字段(单元格的地址)

I tried naming the range "MyRange" and counting if the cells are non blank.我尝试将范围命名为“MyRange”并计算单元格是否为非空白。 But this will not work for the newly created sheets.但这不适用于新创建的工作表。

I also tried a conditional formatting but again this will not give me a summary.我也尝试了条件格式,但这不会给我一个总结。

In the meantime I also bumped into a sort of solution but this is also not the thing I am looking for:与此同时,我也遇到了一种解决方案,但这也不是我想要的:

Sub listEmptyCells()

    Dim emptyAddresses() As String
    Dim i As Long
    Dim ws As Worksheet
    Dim rng As Range
    
    Set ws = Sheets("1.Data Source") ' CHANGE AS NECESSARY
    Set rng = ws.Range("B30:B32")
    
    If WorksheetFunction.CountBlank(rng) = 0 Then
        MsgBox ("No empty cells in the range")
        Exit Sub
    End If
    
    emptyAddresses() = Split(rng.SpecialCells(xlCellTypeBlanks).Address, ",")
    
    For i = LBound(emptyAddresses) To UBound(emptyAddresses)
        ws.Cells(i + 1, 2).Value = emptyAddresses(i)
    Next i

End Sub

Your help and guidance here would be highly appreciated非常感谢您的帮助和指导

All the best Jacek万事如意

Here's one approach:这是一种方法:

Sub listEmptyCells()
    Const CHECK_RANGE As String = "B30:B32" 'range to locate empty cells in

    Dim i As Long, r As Long, rngCheck As Range, rngEmpty As Range
    Dim ws As Worksheet, wb As Workbook, wsSummary As Worksheet
    Dim rwSummary As Range, s As String, c As Range

    Set wb = ThisWorkbook
    Set wsSummary = wb.Worksheets("Summary")
    Set rwSummary = wsSummary.Range("A2:B2") 'first row of results
    rwSummary.Resize(wb.Worksheets.Count).Clear 'remove previous results

    For i = 1 To wb.Worksheets.Count
        Set ws = wb.Worksheets(i)
        If ws.Name <> wsSummary.Name Then 'exclude specific sheet(s)
            
            s = ""
            Set rngEmpty = Nothing

            'which range to check - special case or use default?
            Select Case ws.Name
                Case "Sheet One": Set rngCheck = ws.Range("A1:A10")
                Case "Sheet Two": Set rngCheck = ws.Range("G34:G56,H10")
                Case Else: Set rngCheck = ws.Range(CHECK_RANGE) 'default range
            End Select
            
            'loop cells in check range
            For Each c In rngCheck.Cells
                If Len(c.Value) = 0 Then
                    If rngEmpty Is Nothing Then
                        Set rngEmpty = c
                    Else
                        Set rngEmpty = Application.Union(rngEmpty, c)
                    End If
                End If
            Next c
            If Not rngEmpty Is Nothing Then
                s = rngEmpty.Count & " required cell(s) not filled:" & _
                   rngEmpty.Address(False, False)
            End If

            With rwSummary 'record results
                .Cells(1).Value = ws.Name
                .Cells(2).Value = IIf(s <> "", s, "OK")
                .Font.Color = IIf(s <> "", vbRed, vbGreen)
            End With
            Set rwSummary = rwSummary.Offset(1, 0) 'next summary row
        End If
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM