Application definition or object definition error

Asked

Viewed 2,000 times

0

Hello. I am trying to format a table to base a graph, however I am getting the following error message: "run-time error ? 1004': error defined by the application or defined by the object"

I am using the following macro. Does anything know the reason for the error? The error occurs on the line >>><<<

Sub Botão_ReplicadorColuna()

Dim i, j, UltimaLinha, UltimaColuna As Long

UltimaLinha = 1000000
UltimaColuna = 30

Application.ScreenUpdating = False
For j = 4 To UltimaColuna
    For i = UltimaLinha To 1 Step -1
        If Cells(i, j).Value <> "" Then
           Cells(i + 1, 2).EntireRow.Insert
           Cells(i + 1, 1).Value = Cells(i, 1).Value
           >>>>Cells(i + 1, 2).Value = Cells(i, 2).Value<<<<
           Cells(i + 1, 3).Value = Cells(i, j).Value
           Cells(i, j).Value = ClearContents
        End If
    Next
Next

Application.ScreenUpdating = True
Range("A1").Select


End Sub

I have two spreadsheets. One in which the macro is and the other in which I did a data processing and then copied it and pasted in the macro spreadsheet.

There are no mixed cells.

I could notice that up to 100000 lines the macro works perfectly. After that it presents the error.

What I wish to do is this. I want to replicate the id and content for each existing theme column so that a row is made up of only three columns.

    **Original:**
    ID / Content / Theme
    1 / words / theme/ theme2/ theme3
    2 / words / theme/ them2
    3 / words / theme

    **Pós-macro:**
    ID / Content / Theme
    1 / words / theme
    1 / words / theme2
    1 / words / theme3
    2 / words / theme
    2 / words / theme2
    3 / words / theme
  • In which line does the error occur? How many worksheets do you have? There are merged cells?

  • If you have lost access to your original account or wish to merge it with your new account please go to https://answall.com/contact and choose the corresponding reason

1 answer

0

I tested and the code worked for me with a few lines. Then I will list below changes in the code and some observations of what may be happening.

Loop

If the data is this way:

  1   words   theme   theme2   theme3  
  2   words   theme                    
  3   words   theme   them2   

The code no longer works...

Then I suggest reversing the loop to:

For i = 1 To UltimaLinha
    For j = UltimaColuna To 4 Step -1
    Next j
Next i

Statement

The statement is incorrect in this way:

Dim i, j, UltimaLinha, UltimaColuna As Long

In which i, j, UltimaLinha are declared only as Variant and UltimaColuna as Long. The right thing would be:

Dim i As Long, j As Long, UltimaLinha As Long, UltimaColuna As Long

(Strange syntax that occurs in VBA) What is not a problem, because then they are transformed to Long, but it is better not to let the VBA "find out" what type of variable will be used.

Line Boundary

I don’t know the amount of data in the spreadsheet, but Excel has a limit of 1 048 576 lines, so when it reaches the 100,000 line it may be trying to insert a line and there is no more space. Check if this amount of lines is being exceeded.

Worksheet Statement

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Nome_da_planilha")

It is good to declare the used spreadsheet to prevent that when clicking another spreadsheet, the macro is activated in the wrong location.

Or if you’re in another Workbook:

Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open(Filename:="C:\aminho\Para\o\Arquivo\excel.xls", ReadOnly:=True)
Set ws = wb.Worksheets("Nome_da_planilha")

Code

Option Explicit
Sub Botão_ReplicadorColuna()

    Dim i As Long, j As Long, UltimaLinha As Long, UltimaColuna As Long
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Nome_da_planilha")

    UltimaLinha = 1048576
    UltimaColuna = 30

    Application.ScreenUpdating = False
    With ws
        For i = 1 To UltimaLinha
            For j = UltimaColuna To 4 Step -1
                If .Cells(i, j).Value <> "" Then
                    .Cells(i + 1, 2).EntireRow.Insert
                    .Cells(i + 1, 1).Value = .Cells(i, 1).Value
                    .Cells(i + 1, 2).Value = .Cells(i, 2).Value
                    .Cells(i + 1, 3).Value = .Cells(i, j).Value
                    .Cells(i, j).ClearContents
                End If
            Next j
        Next i
    End With
    Application.ScreenUpdating = True
    Range("A1").Select
End Sub
  • Thank you for the answer, Daniel. If I am responding using the wrong functionality, please tell me that I will make it the right one. I tried here and applying the code I received "runtime error 9 - Subscript out of range". This error is marking the following line: Set Ws = Wb.Worksheets("Redes_lula_treated_pré_macro") If I try to specify the folder, the code runs, but only a new sheet opens. The macro does not start. The folder the file is in is "C: Users Stilingue Desktop networks" and the file is Redes_lula_treated_pré_macro.xlsx.

  • Set ws = wb.Worksheets("Nome_da_planilha") is the name of the Spreadsheet tab and to open the file: Set wb = Workbooks.Open(Filename:="C:\Users\Stilingue\Desktop\redes\Redes_Lula_tratado_pré_macro.xlsx", ReadOnly:=True)

  • Thank you! The tab is called Exportsheet. I specified in the macro and it was, but I got another error. Now it’s back to error 1004, "range failed class Insert method". It signaled the following line: ". Cells(i + 1, 2).EntireRow.Insert"

  • See the part of the answer where I speak of the lines, you may have reached the last possible line in Excel... or have merged cells.

  • Daniel, I checked the program and the same error appeared. The table was treated up to the line 108629. After that it went unnoticed. I checked the number of lines, and there’s no way she can cross 1kk of lines, and there’s no meshed cells. In total there are 249 thousand lines so far and with the format would go up to 500 thousand at most. Do you have any idea what it might be? I have a curiosity too, I used this macro for two lines and it took about 2 hours to format them. Is this normal time? Thank you so much for your availability to help!

  • Your code is really heavy, you loop from the first line to the last (more than 1 million) and 26 columns (totaling more than 26 million). With many interactions between the VBE and the Excel spreadsheet, for example inserting lines... To optimize I suggest working with matrices and popular a new spreadsheet with data from this matrix. Or perform only loops to the required lines, not all of them including numerous blank lines...

Show 1 more comment

Browser other questions tagged

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