IsNumeric函数对空单元格返回true

我运行一个macros从PDF文件复制表格,并将其保存在Excel中。 一些表格包含空单元格,在我的分析中,我需要知道空单元格的数量。 我有一个函数遍历每列以检查该单元格中的值是否为数字。 麻烦的是当我在一个空单元格上运行这个函数时,它返回true。 我什至尝试手动使用Isblank()函数咀嚼单元格,并返回“false”。 (如果我在粘贴的范围之外的任何单元格上尝试此操作,则返回“true”)

我猜测,当我从PDF中复制和粘贴东西时,它会以某种方式为空单元格粘贴一些值。

有没有人遇到类似的问题? 如果是的话,关于如何解决的任何想法?

如果这里有任何帮助是我用来复制和粘贴的代码

'Initialize Acrobat by creating App object Set PDFApp = CreateObject("AcroExch.App") 'Set AVDoc object Set PDFDoc = CreateObject("AcroExch.AVDoc") 'Open the PDF If PDFDoc.Open(PDFPath, "") = True Then PDFDoc.BringToFront 'Maximize the document Call PDFDoc.Maximize(True) Set PDFPageView = PDFDoc.GetAVPageView() 'Go to the desired page 'The first page is 0 Call PDFPageView.GoTo(DisplayPage - 1) '------------- 'ZOOM options '------------- '0 = AVZoomNoVary '1 = AVZoomFitPage '2 = AVZoomFitWidth '3 = AVZoomFitHeight '4 = AVZoomFitVisibleWidth '5 = AVZoomPreferred 'Set the page view of the pdf Call PDFPageView.ZoomTo(2, 50) End If Set PDFApp = Nothing Set PDFDoc = Nothing On Error Resume Next 'Show the adobe application PDFApp.Show 'Set the focus to adobe acrobat pro AppActivate "Adobe Acrobat Pro" 'Select All Data In The PDF File's Active Page SendKeys ("^a"), True 'Right-Click Mouse SendKeys ("+{F10}"), True 'Copy Data As Table SendKeys ("c"), True 'Minimize Adobe Window SendKeys ("%n"), True 'Select Next Paste Cell Range("A" & Range("A1").SpecialCells(xlLastCell).Row).Select 'Cells(1, 1).Select 'Paste Data In This Workbook's Worksheet ActiveSheet.Paste 

有些情况下,最好是检查单元格内的字符的长度 ,而不是使用isNumeric() ,或检查错误等…

例如尝试下面的代码

它build立活动工作表中使用的Range,然后通过检查每个单元的长度(len())来进行迭代

您可以查看VBE中的立即窗口CTRL + G ,以查看哪些单元格地址为空, 或者 等待macros执行完毕,然后用消息框指出在该范围内有多less个空单元格

 Option Explicit Sub CheckForEmptyCells() Dim lastCol As Range Set lastCol = ActiveSheet.Cells.Find(What:="*", After:=ActiveSheet.Cells(1, 1), LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False) Dim rng As Range Set rng = Range("A1:" & lastCol.Address) Dim cnt As Long cnt = 0 Dim cell As Range For Each cell In rng If Len(cell) < 1 Then Debug.Print cell.Address cnt = cnt + 1 End If Next MsgBox "there are " & cnt & " empty cells within the range " & rng.Address End Sub 

完

我已经检查了一个空单元格(现在没有涉及PDF文件),你是正确的: IsNumeric为空单元格返回True

我从来没有遇到过这个问题,因为在编码时,我并不打算把内置函数“限制到极限”(确定一个空单元格是否可以被认为是数字,甚至可能是值得讨论的)。 在对单元格(或通常的string)执行任何types的分析之前,我总是做的是确保它不是空的:

 Dim valIsNumeric As Boolean If (Not IsEmpty(Range("A1"))) Then valIsNumeric = IsNumeric(Range("A1")) End If 

或者更通用的版本(在任何情况下都可以使用任何types的string):

 If (Len(Trim(Range("A1").Value))) Then valIsNumeric = IsNumeric(Range("A1")) End If 

确保给定的单元格/string不为空仅代表一小部分代码,并明显提高了任何方法的可靠性。