Results 1 to 4 of 4

Thread: [Excel] Runtime Error 1004 in very simple script

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    1

    [Excel] Runtime Error 1004 in very simple script

    Hi I'm getting a Runtime Error 1004 @ the bolded line in the following script and I cannot understand why:

    Code:
    Public Sub MACRO()
        Dim x As Integer
        Dim y As Integer
        Dim n As Integer
        Sheets("Cognos").Select
        For x = 46 To 53
            For y = 13 To 639
                CellValue = Cells(y, x).Text
                Cells(y, x).Select
                If CellValue = "ADD SKU" Then
                    Cells(y, 2).Resize(1, 1).Select
                    n = ActiveCell.Value
                    Sheets("Detail").Select
                    NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
                    Cells(NextRow, 5).Select
                    ActiveCell.Value = n               
                    Sheets("Cognos").Select
                End If
            Next y
        Next x
    End Sub
    Any help would be greatly appreciated.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Runtime Error 1004 in very simple script

    n = ActiveCell.Value
    Sheets("Detail").Select
    NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Cells(NextRow, 5).Select
    ActiveCell.Value = n
    try
    vb Code:
    1. sheets("detail").cells(sheets("detail").rows.count, 1).offset(1, 4).value = activecell.value
    better to avoid using active anything, work with fully qualified ranges
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [Excel] Runtime Error 1004 in very simple script

    Try not to use variables without declaring them first (CellValue, NextRow). Turn on Option Explicit to prevent from doing that.

    Code:
    Cells(y, 2).Resize(1, 1).Select
    n = ActiveCell.Value
    resize(1, 1) does nothing on a single cell. instead of those 2 statements:

    Code:
    n =  Cells(y, 2).Value
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] Runtime Error 1004 in very simple script

    Hello EmilN

    Welcome to the forums

    kaliman79912 rightly suggested that you should declare your variables and use the option explicit. I would further suggest you to read this

    Topic: To 'Err' is Human - Good Coding Practices
    Link: http://siddharthrout.wordpress.com/2...-err-is-human/

    There are few problems with the code.

    1) Avoid using the Integer type. You have declared n As Integer. What if the 'ActiveCell' Value is 1111111?

    To understand the data type, I would recommend this link.

    Topic: The Integer, Long, and Byte Data Types
    Link: http://msdn.microsoft.com/en-us/library/aa164754%28v=office.10%29.aspx

    2) Like Pete correctly suggested, avoid the use of .Select They slow down your code and are also responsible for many errors

    Consider this example

    Sheets("Detail").Select
    NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Cells(NextRow, 5).Select
    ActiveCell.Value = n
    This can be also written as

    Code:
    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet, NextRow As Long, n As Long
        '
        '~~> Rest of Code
        '
        Set ws = Sheets("Detail")
        
        With ws
            NextRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
            .Range("E" & NextRow).Value = n
        End With
        '
        '~~> Rest of Code
        '
    End Sub
    3) There is no error handling. If you are developing for a client then your client will be left high and dry as the code will break leaving your client clueless as to what exactly went wrong.

    The code that you posted in post1 can also be written as

    Code:
    Option Explicit
    
    Public Sub MACRO()
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim x As Long, y As Long, n As Long
        Dim CellValue As String
        
        On Error GoTo whoa
        
        Application.ScreenUpdating = False
        
        Set ws1 = Sheets("Cognos")
        Set ws2 = Sheets("Detail")
        
        With ws1
            For x = 46 To 53
                For y = 13 To 639
                    CellValue = .Cells(y, x).Text
                    If CellValue = "ADD SKU" Then
                        n = .Cells(y, 2).Value
                        NextRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
                        ws2.Cells(NextRow, 5).Value = n
                    End If
                Next y
            Next x
        End With
        
    LetsContinue:
        Application.ScreenUpdating = True
        Exit Sub
    Err:
        MsgBox Err.Description
        Resume LetsContinue
    End Sub
    Hope this helps

    Sid
    Last edited by Siddharth Rout; Aug 6th, 2011 at 11:18 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width