0

I'm doing an extraction from a PDF file and pasting it into Excel, which it works great. When I try to process the extracted data I keep getting a 'Run-Time error 1004'

The code I have is the following

Sub PDF_Upload()
    Dim objWord As Object
    Dim objDoc As Object
    Dim wdFileName
    Dim LastRow As Long
    Dim fso As New FileSystemObject
    Dim fileName As String

    fileName = fso.GetFileName("C:\ABC.pdf")
    Title = Replace(fileName, ".pdf", "")

    Set objWord = CreateObject("word.Application")
    wdFileName = "C:\ABC.pdf"

    Set objDoc = objWord.Documents.Open(wdFileName)
    objWord.Selection.WholeStory
    objWord.Selection.Copy

    ' create new sheet and call it the pdf name
    Sheets.Add
    ActiveSheet.name = Title
    [A1].Select
    ActiveSheet.Paste

    ' Close Word
    objDoc.Close SaveChanges:=False
    objWord.Quit
    Application.DisplayAlerts = True

    ' check
    Sheets(Title).Select
    ' process fails in the next line
    LastRow = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.Count - 1, 0).End(xlUp).Row
End Sub

Everything works until I get to the last line 'LastRow = ActiveSheet....'

Any ideas on what is causing the error. I have look into other posting on 1004 error in StackOverflow (1,2,3,...), but I can not find anything that helps.

Selrac
  • 2,203
  • 9
  • 41
  • 84

1 Answers1

0

Thanks to the comments above managed to get the solution. Posting it here to help someone:

With Worksheets(Title)
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Selrac
  • 2,203
  • 9
  • 41
  • 84
  • Just realized that `Title` was a variable, not the actual name of the `Worksheet`. My bad on including the quotation marks around it in the comments. – BigBen Sep 13 '18 at 14:45