错误日志如果数字不在其他工作表的范围内

我有一些麻烦写我的VBAmacros做了以下几点:

在单元格C3中,我将进入成本中心,并在单元格D3中input成本types编号。

现在我想要做的是显示一个错误消息,当C3中的值以3开始,D3中的值(总是7个数字)不在数组范围内时,会popup一个错误消息元数据。

我的代码到目前为止是以下内容:

'Do While... '... If Left(Cells(CLine, 3).Value, 1) = "3" Then If Cells(CLine, 5).Value = blank Then If SLine = 1 Then ErrLog = ErrLog & "Costcenter in Line" & " " & Dline SLine = SLine + 1 Else ErrLog = ErrLog & ", " & Dline End If End If End If Dline = Dline + 10 CLine = CLine + 1 Loop If SLine > 1 Then ErrLog = ErrLog & " requires WBS Element." & vbCrLf End If 

现在,如果我展开第一行像这样If Left(Cells(CLine, 3).Value, 1) = "3" and Left(Cells(CLine, 4).Value, 7) <> "1000000" Then而我的错误信息只会出现在单元格D3中input另一个超过1000000的值。

但是一旦我开始使用诸如<1000000和> 2000000之类的东西(为了在1000000到2000000之间的数字被使用时不能得到消息),我得到了错误之后的错误。

有人可以帮助我,并告诉我如何在我的代码的第一行中以简单的方式包含“之间”,以便在input10000000到2000000之间的数字时不会出错。

首先,你可以定义你想要检查的单元格D3是否在这样的值内:

 Dim Rng As Range ' set-up check range (modify Sheet name and Range to your needs) Set Rng = Sheets("metadata").Range("AG2:AG23") 

其次,如果单元格C3以“3”开始,并且单元格D3值超出该范围,则显示一条错误消息,请使用下面的行:

  If Left(Cells(CLine, 3).Value, 1) = "3" And _ (Cells(CLine, 4).Value < WorksheetFunction.Min(Rng) Or _ Cells(CLine, 4).Value > WorksheetFunction.Max(Rng)) Then 

第三,要检查某个单元格是否为空(或空),请更改您的行:

 If Cells(CLine, 5).Value = blank Then 

至 :

 If IsEmpty(Cells(CLine, 5)) Then