5

I run a macro that copies tables from a PDF file and saves them on Excel. some of the tables contain empty cells and in my analysis I need to know the number of cells that are empty. I have a function that iterates through each column to check if the value within that cell is numeric or not. the trouble is when I run this function on an empty cell it returns true. I even tried manually cheeking the cells using the Isblank() function and it returns "false". (if I try this on any cell outside the pasted range it returns "true")

I am guessing that when I copy and paste things from PDF it somehow pastes some value for the empty cells.

did anyone ever encounter a similar problem? if so, any ideas on how it can be solved?

if it is any help here is the code I use to copy and paste

'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
4

3 回答 3

5

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

例如尝试下面的代码

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

您可以查看 VBE 中的即时窗口CTRL+G以查看哪些单元格地址是空的 ,或者 等到宏完成执行,您将收到一个消息框,说明该范围内有多少个空单元格

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

完成的

于 2013-08-14T07:50:22.077 回答
4

这是一种 hackish 解决方案,但可以作为一个简单的解决方案。

由于 'IsNumeric(p variant)' 使用变体,因此您可以将“-”附加到输入参数。这意味着 null 被解释为不是数字的“-”,而真正的数字将被视为负数,从而满足真正是数字的条件。(虽然现在是负面的)

IsNumeric("-" & string_Value)IsNumeric(str_Value)

于 2019-02-01T18:30:35.207 回答
2

我现在用一个空单元格检查了它(根本不涉及 PDF 文件),你是对的:IsNumeric返回True空单元格。

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

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

或者在更通用的版本中(在任何情况下对任何类型的字符串都非常可靠):

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

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

于 2013-08-14T07:50:13.443 回答