VBA如何在使用特定字符串时验证单元格?

时间:2018-06-14 10:42:54

标签: excel vba excel-vba

我正在使用Excel工作表,它使用宏来进行一些验证。我试图验证“表(T)/行(L)”列中的所有行(段落)对于表设置为“T”,所有其他行(段落)使用L选项。该列“Table(T)/ Line(L)”使用列表验证。因此,如果“文本块行”列中的当前单元格为空,则“表(T)/行(L)”列中其对应单元格中的值必须为" L" 。如果值为" T"和#34;文本块行"单元格为空,必须出现一个msgbox: 对于第7行中的表段,没有定义“文本块行”。 这是截至目前我完成的截图和我的代码:

http://prntscr.com/juuu20

http://prntscr.com/juwy3k-错误的宏msgbox

Sub checkcolumns()

Dim rngcheck As Range
Dim rngcheck2 As Range
Dim cell As Range
Dim cell2 As Range
Set rngcheck = Range("B4:B6")
Set rngcheck2 = Range("C4:C6")

 i = 0

For Each cell In rngcheck

For Each cell2 In rngcheck2

    i = i + 1

    j = j & cell.Address(0, 0) & vbNewLine
    d = d & cell2.Address(0, 0) & vbNewLine

  If IsEmpty(cell) Then

  If cell2.Value = "L" Then
  mess = mess & vbCrLf & "For the line paragraph in row:" & vbNewLine & d + "no “Text Block Row” can be defined"

  'For the table paragraph in row 7 is no “Text Block Row” defined
      End If

    Else

  cell2.Value = "T"
  mess = mess & vbCrLf & "For the table paragraph in row:" & vbNewLine & j + "is no “Text Block Row” defined"
 End If

      Next cell2
  Next cell

  If mess <> "" Then MsgBox mess

End Sub

1 个答案:

答案 0 :(得分:0)

你很难回答这个问题,因为你没有准确地解决你的问题。这是我最好的猜测。

Sub checkcolumns()

Dim rngcheck As Range
Dim rngcheck2 As Range
Dim cell As Range, j As String, d As String, mess As String

Set rngcheck = Range("B4:B6")
Set rngcheck2 = Range("C4:C6")

For Each cell In rngcheck
    j = j & cell.Address(0, 0) & vbNewLine
    d = d & cell.Offset(, 1).Address(0, 0) & vbNewLine
    If IsEmpty(cell) Then
        If cell.Offset(, 1).Value = "L" Then
            mess = mess & vbCrLf & "For the line paragraph in row:" & vbNewLine & d & "no “Text Block Row” can be defined"
            'For the table paragraph in row 7 is no “Text Block Row” defined
        ElseIf cell.Offset(, 1).Value = "T" Then
            mess = mess & vbCrLf & "For the table paragraph in row:" & vbNewLine & j & "is no “Text Block Row” defined"
        End If
    End If
Next cell

If mess <> "" Then MsgBox mess

End Sub