VBA - Application definition or object definition error

Asked

Viewed 5,283 times

0

I am trying to recover the amount of rows from an excel spreadsheet to format it, but am coming across application definition or object definition error. Below is the code snippet.

Private Function formataPlanilha(pathWorkbook As String, dic As Object)

objExcel = CreateObject("Excel.Application")

' ABRE ARQUIVO
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(pathWorkbook)
objExcel.Application.Visible = False

objExcel.Sheets.Add.Name = "TD"
objExcel.Sheets(dic("Sheet_Name")).Select

' INICIO DA FORMATAÇÃO
objExcel.Cells.Select
objExcel.Selection.Font.Name = "Calibri"
objExcel.Selection.Font.Size = 9
objExcel.Cells.EntireColumn.AutoFit
objExcel.Selection.RowHeight = 13.5
objExcel.Selection.AutoFilter

' RECUPERA QUANTIDADE DE LINHAS
objExcel.Range("A1").Select
objExcel.Selection.End(xlDown).Select
maxRows = objExcel.ActiveCell.Row

I believe the error is occurring in xlDown. Can anyone help me?

  • 1

    Good is that to declare a Range, you should name the Workbook and Worksheet to avoid some errors, if the code is inside the Excel spreadsheet. For example: Set ws = Sheets("Planilha1") and use ws.range("A1").Select. See an example of how to declare, may be the lack of statement of which book should be opened and which spreadsheet. You are programming only for the Excel spreadsheet or to interact with other software?

  • @danieltakeshi posts the answer Daniel to be marked as answered.

  • @danieltakeshi, I actually have access queries that generate my spreadsheet and a module responsible for exporting and formatting it. The generated spreadsheet is not empty. I have Workbook and Worksheet declared. I will edit the question for better understanding

  • It is always good to avoid the use of Select and use Ranges, I will check the spreadsheet better later and notice here. And the declaration of variables? how is done? Dim ... As Object?

  • Dim pathWorkbook As String Dim dic As Object Set dic = Createobject("Scripting.Dictionary") pathWorkbook = path & filename

1 answer

0


I got the correct number of lines with this test code, some changes can be made for you to use. The code was made in Excel-VBA 7.1. From what I understood the spreadsheet already exists.

Dim objExcel As Object: Set objExcel = CreateObject("Excel.Application")
Dim pathWorkbook As String: pathWorkbook = "C:\Users\nome_usuario\Desktop\Pasta1.xlsm"
Dim objWorkbook As Object: Set objWorkbook = objExcel.Workbooks.Open(pathWorkbook)
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
Dim ws As Worksheet
Dim nomePlanilha As String
Dim maxRows As Long

objExcel.Visible = False

nomePlanilha = "Planilha1"
Set ws = objWorkbook.Sheets(nomePlanilha)
' INICIO DA FORMATAÇÃO
With ws
    objExcel.Cells.Font.Name = "Calibri"
    objExcel.Cells.Font.Size = 9
    objExcel.Cells.EntireColumn.AutoFit
    objExcel.Cells.RowHeight = 13.5
    objExcel.Cells.AutoFilter
End With

' RECUPERA QUANTIDADE DE LINHAS
maxRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Debug.Print maxRows

On Error Resume Next
objWorkbook.Close (True)
On Error Goto 0
Set objWorkbook = Nothing

STEPS TO CORRECTLY DECLARE

  1. Create an Excel Application object Dim objExcel As Object: Set objExcel = CreateObject("Excel.Application")
  2. Create String in file path name Dim pathWorkbook As String: Let pathWorkbook = "Caminho"
  3. Open the Excel spreadsheet Dim objWorkbook As Object: Set objWorkbook = objExcel.Workbooks.Open(pathWorkbook)
  4. Declare the Excel spreadsheet Dim ws As Object and select its name Set ws = objWorkbook.Sheets(nomePlanilha) can be the name between quotation marks ("Spreadsheet 1") or index number (1,2,3, etc..)
  5. With the Ws spreadsheet declared, you can use it to perform the operations in Excel-VBA
  6. To close properly objWorkbook.Close (True) True saves the spreadsheet and False does not save.

Note

To see the window of Debug.Print, enable Excel VBA to Immediate Verification.

If the spreadsheet doesn’t exist yet, then you need to add it:

Dim ws As Worksheet
With objWorkbook
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    ws.Name = "TD"
End With

Edit:

What you may have forgotten is to enable the Excel objects in the references.

1st Open Access Visual Basic

2nd Enter Tools -> References...

3rd Enable Microsoft Excel Object as shown below:

Excel object reference

  • Thank you for your attention. I am facing problems with the declaration of Worksheet, informing compilation error, stating that the type defined by the user has not been defined. This occurs right on the line Dim Ws As Worksheet

  • Are you declaring inside Excel-VBA? And what version, year? Try to change to Dim ws As Object

  • I am in Access-VBA, in a module. I use version 7.1, 2012.

  • The code I have can export and format normally, but the same is found in the 2007 version of access. In 2016 it is not possible

  • Yeah, I was programming in Excel 2016, but then you said Access, so I changed it and it worked. I’ll edit the answer. Next time choose the correct tags and say that the programming is done in Access

  • My fault. Next week I will check the answer and already mark as correct if you meet me. Thank you for the attention, again

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.