Application definition or object definition error

0

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

I'm using the following macro. Does something know the reason for the error? The error occurs on the line that > > > < <

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 where the macro is and another where I did a data processing and then copied from it and pasted it into the macro worksheet.

There are no merged cells.

I noticed that up to 100000 lines the macro works perfectly. After that the error is displayed.

What I want to do is the following. 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
    
asked by anonymous 17.08.2018 / 23:58

1 answer

0

I tested it and the code worked for me with just a few lines. So I'll list below code changes and some observations of what might be happening.

Loop

If the data looks like this:

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

The code no longer works ...

So 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 this way:

Dim i, j, UltimaLinha, UltimaColuna As Long

Where i, j, UltimaLinha is declared only as Variant and UltimaColuna as Long . The correct would be:

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

(Strange syntax that occurs in VBA) What's not a problem, since then they are transformed to Long, but it's best not to let VBA "figure out" what kind of variable will be used.

Line Threshold

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

Spreadsheet Statement

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Nome_da_planilha")

It is good to declare the worksheet used to prevent clicking the other worksheet in the wrong place.

Or if you are 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
    
20.08.2018 / 14:28