Share via

Run-time error 6 Overflow - Excel 2016 Macro

Anonymous
2018-01-17T15:15:41+00:00

Hello all,

I've the code below, and every time that i execute it, i get the error from the topic, to be more specific the error is being detected on the bold formatted line: (K = K +1).

I've tried already converting the k variable to Long instead of integer, but when i do this i get another error "runtime error 1004 vba application defined or object defined error"

Using the variables as Integer even getting the runtime error 6 the Macro works perfectly but i want to get rid of that error, does anybody have a suggestion for me?.

Thanks!

Sub Workbook_Copy()

'Variables definition

    Dim j As Integer

    Dim i As Integer

    Dim k As Integer

    i = 2

'Open and activate files to work with

    Set x = Workbooks.Open("C:\Users\v99023\Desktop\FMP SoExports\Test Macros\Second Test - Full Swing Related Examples\FMP Open SOs.xlsx")

    Set y = Workbooks.Open("C:\Users\v99023\Desktop\FMP SoExports\Test Macros\Second Test - Full Swing Related Examples\Matching Item FMP-Matrix.xlsx")

    Set Z = Workbooks.Open("C:\Users\v99023\Desktop\FMP SoExports\Test Macros\Second Test - Full Swing Related Examples\SalesOrder.csv")

    x.Activate

    y.Activate

    Z.Activate

'Do whihle loop intiates and validates if the value of the column is different fron blank

 Do While x.Worksheets("Sheet2").Cells(i, 23).Value <> ""

 'If the condition is true it will enter to the cicle and it validates if the customer column is blank

    If x.Worksheets("Sheet2").Cells(i, 1).Value <> "" Then

        Z.Worksheets("SalesOrder").Cells(i, 1) = x.Worksheets("Sheet2").Cells(i, 1)

        Z.Worksheets("SalesOrder").Cells(i, 2) = x.Worksheets("Sheet2").Cells(i, 23)

        Else

        If x.Worksheets("Sheet2").Cells(i, 1).Value = "" Then

        k = i

        Do While x.Worksheets("Sheet2").Cells(k, 1).Value = ""

        Z.Worksheets("SalesOrder").Cells(k, 2).Value = x.Worksheets("Sheet2").Cells(k, 23)

        k = k + 1

        Loop

        End If

    End If

    i = i + 1

Loop

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2018-01-17T15:30:37+00:00

IMHO it is obvious that this loop runs to the end of the column:

Do While x.Worksheets("Sheet2").Cells(k, 1).Value = ""

Means the issue is not due to the code, but to the data in your file.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-01-17T16:52:09+00:00

    Hi Andreas,

    I copied the data to a new file and it worked that way, thank you!

    Was this answer helpful?

    0 comments No comments