UPDATE: Sorry still new at this, what I'm looking to achieve is to stop a user saving the worksheet until all "mandatory ranges" are completed across multiple sheets. There's probably a much easier way of doing what I'm trying but I'm still learning, sorry!
The error message I'm getting with the below "does not work" is:
Range(man_cell) = <Method 'Range'of object '_Global' failed>
This is part of a "Submit WorkSheet" macro tied to a command button.
I want to check if all these cells are blank, if any are pop up a MessageBox telling someone they've missed something. If they aren't continue as usual. When I use a single cell reference it works, when I use multiple it does not. I've got named ranges set for all the sheets, but none of them whatsoever would work and a single cell reference works perfectly.
This Works
man_cells = Array("'Engagement Form'!$I$6")
This does not work
man_cells = Array("'Engagement Form'!$I$6", "'Engagement Form'!$I$8'")
Code I'm trying to get to work:
'Establish if Mandatory Cells have been completed
Dim man_cells As Variant
Dim man_items_count As Integer
man_cells = Array("'Engagement Form'!$I$6'", _
"'Engagement Form'!$I$8", _
"'Engagement Form'!$B$12:$I$12", _
"'Engagement Form'!$B$16:$I$16", _
"'Engagement Form'!$I$18", "'Engagement Form'!$I$28", "'Engagement Form'!$I$30", "'Engagement Form'!$I$32", "'Engagement Form'!$I$36", _
"'Engagement Form'!$I$38", "'Engagement Form'!$I$40", "'Engagement Form'!$I$44", "'Engagement Form'!$I$46", "'Engagement Form'!$I$48", _
"'Engagement Form'!$I$56", "'Engagement Form'!$I$58", "'Engagement Form'!$I$60", "'Engagement Form'!$I$66", "'Engagement Form'!$I$68", _
"'Engagement Form'!$I$70", "'Engagement Form'!$I$72", "'Engagement Form'!$I$74", "'Engagement Form'!$I$78", "'Engagement Form'!$I$80", _
"'Engagement Form'!$I$82", "'Engagement Form'!$I$88", "'Engagement Form'!$I$96", "'Engagement Form'!$I$98", _
"'Engagement Form'!$B$104:$I$104", _
"'Engagement Form'!$I$106", _
"'Engagement Form'!$B$111:$I$111", _
"'Engagement Form'!$B$115:$I$115", _
"'Engagement Form'!$L$6:$Z$120", _
"'Data Protection Crib Sheet'!$B$9:$B$19", _
"'A & A Crib Sheet'!$B$9:$B$44", _
"'Governance Crib Sheet'!$B$9:$B$17")
CellCount = 0
man_items_count = UBound(man_cells) + 1
For Each man_cell In man_cells
Set rng = Range(man_cell)
If Len(rng.Text) > 0 Then
CellCount = CellCount + 1
End If
Next man_cell
If CellCount <> man_items_count Then
MsgBox etc
答案 0 :(得分:1)
SJR的第一条评论是你问题的解决方法:
中有太多单引号
route.toString()
第二条评论将简化您的代码
为了最大限度地减少拼写错误,我建议使用此结构
'Engagement Form'!$I$8'
修改
例如,要使用新的范围集添加新工作表(称为Option Explicit
Public Sub CheckRequiredCells1()
Const EM_1 = "I6,I8,B12:I12,B16:I16,I18,I28,I30,I32,I36,I38,I40,I44,I46,I48,"
Const EM_2 = "I56,I58,I60,I66,I68,I70,I72,I74,I78,I80,I82,I88,I96,I98,"
Const EM_3 = "B104:I104,I106,B111:I111,B115:I115,L6:Z120"
Const EM = EM_1 & EM_2 & EM_3 'Required ranges on Sheet "Engagement Form"
Const DP = "B9:B19" 'Required ranges on Sheet "Data Protection Crib Sheet"
Const AA = "B9:B44" 'Required ranges on Sheet "A & A Crib Sheet"
Const GC = "B9:B17" 'Required ranges on Sheet "Governance Crib Sheet"
Dim wsEM As Worksheet, wsDP As Worksheet, wsAA As Worksheet, wsGC As Worksheet
Set wsEM = ThisWorkbook.Worksheets("Engagement Form") 'Reference to the sheet
Set wsDP = ThisWorkbook.Worksheets("Data Protection Crib Sheet")
Set wsAA = ThisWorkbook.Worksheets("A & A Crib Sheet")
Set wsGC = ThisWorkbook.Worksheets("Governance Crib Sheet")
Dim rng As Variant, invalid As Long
For Each rng In Array(wsEM.Range(EM), wsDP.Range(DP), wsAA.Range(AA), wsGC.Range(GC))
'rng.Cells.Count = total cells in required range for the sheet being processed
'Application.WorksheetFunction.CountA(rng) = total cells containing data
invalid = invalid + (rng.Cells.Count - Application.WorksheetFunction.CountA(rng))
Next
If invalid > 0 Then MsgBox "Please fill out all " & invalid & " cells"
End Sub
),请添加如下行:
"Sheet1"
并在Const S1 = "A1:A2, B3:B5" 'Required ranges on Sheet1
Dim wsS1 As Worksheet
Set wsS1 = ThisWorkbook.Worksheets("Sheet1")
循环中更新此行,以包含For
:
wsS1.Range(S1)
,最终代码变为:
Array(wsEM.Range(EM), wsDP.Range(DP), wsAA.Range(AA), wsGC.Range(GC), wsS1.Range(S1))
答案 1 :(得分:0)
您无法在一系列单元格上执行len文本。它只会获取该范围内第一个单元格的len。这是一个主要问题。
以下是处理细胞计数与空白细胞计数的关系。因此将测试是否所有单元格都已完成。有更简单的方法来处理范围。例如,创建命名范围并使用它们。
我使用split来提取工作表名称和范围地址,以便我可以使用正确引用的那些。
Option Explicit
Public Sub Test()
'Establish if Mandatory Cells have been completed
Dim man_cells As Variant
man_cells = Array("'Engagement Form'!$I$6'", _
"'Engagement Form'!$I$8", _
"'Engagement Form'!$B$12:$I$12", _
"'Engagement Form'!$B$16:$I$16", _
"'Engagement Form'!$I$18", "'Engagement Form'!$I$28", "'Engagement Form'!$I$30", "'Engagement Form'!$I$32", "'Engagement Form'!$I$36", _
"'Engagement Form'!$I$38", "'Engagement Form'!$I$40", "'Engagement Form'!$I$44", "'Engagement Form'!$I$46", "'Engagement Form'!$I$48", _
"'Engagement Form'!$I$56", "'Engagement Form'!$I$58", "'Engagement Form'!$I$60", "'Engagement Form'!$I$66", "'Engagement Form'!$I$68", _
"'Engagement Form'!$I$70", "'Engagement Form'!$I$72", "'Engagement Form'!$I$74", "'Engagement Form'!$I$78", "'Engagement Form'!$I$80", _
"'Engagement Form'!$I$82", "'Engagement Form'!$I$88", "'Engagement Form'!$I$96", "'Engagement Form'!$I$98", _
"'Engagement Form'!$B$104:$I$104", _
"'Engagement Form'!$I$106", _
"'Engagement Form'!$B$111:$I$111", _
"'Engagement Form'!$B$115:$I$115", _
"'Engagement Form'!$L$6:$Z$120", _
"'Data Protection Crib Sheet'!$B$9:$B$19", _
"'A & A Crib Sheet'!$B$9:$B$44", _
"'Governance Crib Sheet'!$B$9:$B$17")
Dim i As Long, numberBlanks As Long, numberOfCells As Long
For i = LBound(man_cells) To UBound(man_cells)
numberBlanks = numberBlanks + Application.WorksheetFunction.CountBlank(ThisWorkbook.Worksheets(Split(man_cells(i), "'")(1)).Range(Split(Split(man_cells(i), "!")(1), "'")(0)))
numberOfCells = numberOfCells + ThisWorkbook.Worksheets(Split(man_cells(i), "'")(1)).Range(Split(Split(man_cells(i), "!")(1), "'")(0)).Cells.Count
Next i
If numberBlanks <> numberOfCells Then MsgBox "Etc"
End Sub
取消用户保存:
如果您希望它涉及停止用户保存,那么您可以将其作为函数调用从Workbook_BeforeSave事件中合并,例如
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If CancelSave = True Then Cancel = True
End Sub
Public Function CancelSave() As Boolean
Dim man_cells As Variant
man_cells = Array("'Engagement Form'!$I$6'", _
"'Engagement Form'!$I$8", _
"'Engagement Form'!$B$12:$I$12", _
"'Engagement Form'!$B$16:$I$16", _
"'Engagement Form'!$I$18", "'Engagement Form'!$I$28", "'Engagement Form'!$I$30", "'Engagement Form'!$I$32", "'Engagement Form'!$I$36", _
"'Engagement Form'!$I$38", "'Engagement Form'!$I$40", "'Engagement Form'!$I$44", "'Engagement Form'!$I$46", "'Engagement Form'!$I$48", _
"'Engagement Form'!$I$56", "'Engagement Form'!$I$58", "'Engagement Form'!$I$60", "'Engagement Form'!$I$66", "'Engagement Form'!$I$68", _
"'Engagement Form'!$I$70", "'Engagement Form'!$I$72", "'Engagement Form'!$I$74", "'Engagement Form'!$I$78", "'Engagement Form'!$I$80", _
"'Engagement Form'!$I$82", "'Engagement Form'!$I$88", "'Engagement Form'!$I$96", "'Engagement Form'!$I$98", _
"'Engagement Form'!$B$104:$I$104", _
"'Engagement Form'!$I$106", _
"'Engagement Form'!$B$111:$I$111", _
"'Engagement Form'!$B$115:$I$115", _
"'Engagement Form'!$L$6:$Z$120", _
"'Data Protection Crib Sheet'!$B$9:$B$19", _
"'A & A Crib Sheet'!$B$9:$B$44", _
"'Governance Crib Sheet'!$B$9:$B$17")
Dim i As Long, numberBlanks As Long, numberOfCells As Long
For i = LBound(man_cells) To UBound(man_cells)
numberBlanks = numberBlanks + Application.WorksheetFunction.CountBlank(ThisWorkbook.Worksheets(Split(man_cells(i), "'")(1)).Range(Split(Split(man_cells(i), "!")(1), "'")(0)))
numberOfCells = numberOfCells + ThisWorkbook.Worksheets(Split(man_cells(i), "'")(1)).Range(Split(Split(man_cells(i), "!")(1), "'")(0)).Cells.Count
Next i
If numberBlanks > 0 Then
MsgBox "Please complete all mandatory cells"
CancelSave = True
End If
End Function