Run-time error 1004: Application-defined or object-defined error

Warflion

New Member
Joined
Aug 11, 2017
Messages
10
Hi, I am just started to learn VBA and messing codes around. I got this runtime error with my codes. Here's how it sets up.

A1 to A100 are ID number 1 to 100. B1 to B100 are "values" which all starts at 100. D1 to D100 are randomly generated number working as ID pointer indicating value flow, equation: "=ROUND(RAND(),2)*100". C1 to C100 are copy and paste number from column D so the number remain static.

Following is the code. Thanks in advance.

Sub Test3()
For m = 1 To 17000
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Call CopyPasteSpecial
For n = 1 To 100
If Range("B" & n).Value > 0 Then
x = Range("C" & n).Value
Range("B" & n).Value = Range("B" & n).Value - 1
Cells(x, 2).Value = Cells(x, 2).Value + 1
Else
Range("B" & n).Value = Range("B" & n).Value
End If
Next n
Range("G1").Value = Range("G1").Value + 1
Next m
End Sub

Sub CopyPasteSpecial()
'
' CopyPasteSpecial Macro
'


Range("D1", "D1000").Select
Application.CutCopyMode = False
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Although you test to see if the value in B is greater than 0 you then set x to the value of C, which could be 0

Rich (BB code):
Sub Test3()
Dim m As Long, n As Long, x As Long
Application.ScreenUpdating = False
For m = 1 To 17000
    Call CopyPasteSpecial
    For n = 1 To 100
        If Range("B" & n).Value > 0 Then
            x = Range("C" & n).Value ' this is probably returning 0, see below
            Range("B" & n).Value = Range("B" & n).Value - 1
            Cells(x, 2).Value = Cells(x, 2).Value + 1 ' Runtime error here if x is 0, as above
        Else
            Range("B" & n).Value = Range("B" & n).Value
        End If
    Next n
    Range("G1").Value = Range("G1").Value + 1
Next m
Application.ScreenUpdating = True
End Sub


Sub CopyPasteSpecial()
'
' CopyPasteSpecial Macro
'
Range("C1:C1000").Value = Range("D1:D1000").Value
End Sub
 
Upvote 0
Although you test to see if the value in B is greater than 0 you then set x to the value of C, which could be 0

Rich (BB code):
Sub Test3()
Dim m As Long, n As Long, x As Long
Application.ScreenUpdating = False
For m = 1 To 17000
    Call CopyPasteSpecial
    For n = 1 To 100
        If Range("B" & n).Value > 0 Then
            x = Range("C" & n).Value ' this is probably returning 0, see below
            Range("B" & n).Value = Range("B" & n).Value - 1
            Cells(x, 2).Value = Cells(x, 2).Value + 1 ' Runtime error here if x is 0, as above
        Else
            Range("B" & n).Value = Range("B" & n).Value
        End If
    Next n
    Range("G1").Value = Range("G1").Value + 1
Next m
Application.ScreenUpdating = True
End Sub


Sub CopyPasteSpecial()
'
' CopyPasteSpecial Macro
'
Range("C1:C1000").Value = Range("D1:D1000").Value
End Sub

Totally solved it. replace round() with roundup() now its working. THANK YOU.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top