+ Reply to Thread
Results 1 to 9 of 9

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

Hybrid View

  1. #1
    Registered User
    Join Date
    10-28-2008
    Location
    Romania
    Posts
    3

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

    Hi All,

    This is my very first VBA project, and I'm a bit lost. I've got this run-time error:
    1004 - Application-defined or object-defined error.

    Could you please take a look into the program and suggest me where could the problem be?
    Sub FormatTable()
    Dim i As Long
    Dim rng As Range
    Dim celle As Range
    
    Set rng = Range(Sheets("Sheet1").[A1], Sheets("Sheet1").[A65536].End(xlUp))
    
    
    For Each celle In rng
      If IsEmpty(celle.value) and IsEmpty(celle.Offset(1, 0)) and IsEmpty(celle.Offset(2, 0)) and IsEmpty(celle.Offset(3, 0)) Then   
           celle.select
         Range("A1:H2").Select
         Selection.Copy
         Range("celle.offset(1,0)").Select
         ActiveSheet.Paste
           Range("celle.offset(-1,0)").Select
           Selection.Font.bold = True
        Range("celle.offset(5,0)").Select
        Selection.CurrentRegion.Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
      end if
    next celle
    
    end sub
    Thank you so much:-)
    beacska
    Last edited by Leith Ross; 10-28-2008 at 08:27 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Welcome to the forum.

    What line generates the error?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Gotta go. Try this:
    Sub FormatTable()
        Dim cell    As Range
    
        For Each cell In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp))
            If IsEmpty(cell.Value) And _
               IsEmpty(cell.Offset(1, 0).Value) And _
               IsEmpty(cell.Offset(2, 0).Value) And _
               IsEmpty(cell.Offset(3, 0).Value) Then
                Range("A1:H2").Copy Destination:=cell.Offset(1)
                cell.Offset(-1, 0).Font.Bold = True
                With cell.Offset(5, 0).CurrentRegion.Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
            End If
        Next cell
    End Sub

  4. #4
    Registered User
    Join Date
    10-28-2008
    Location
    Romania
    Posts
    3
    This is working perfect. :-) Thank you soo much sgh

    But there is just one problem, I need to cutomise this for every different set of tables. For the one I'm working on now it should find 7 blank cells and also the rage to copy is different. I did it in this way, but now it gives me this error: "Subscript out of range"

    Sub FormatTable()
        Dim cell    As Range
    
        For Each cell In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp))
            If IsEmpty(cell.Value) And _
               IsEmpty(cell.Offset(1, 0).Value) And _
               IsEmpty(cell.Offset(2, 0).Value) And _
               IsEmpty(cell.Offset(3, 0).Value) And _
               IsEmpty(cell.Offset(4, 0).Value) And _
               IsEmpty(cell.Offset(5, 0).Value) And _
               IsEmpty(cell.Offset(6, 0).Value) Then
                Range("A1:Z3").Copy Destination:=cell.Offset(1)
                cell.Offset(-1, 0).Font.Bold = True
                With cell.Offset(8, 0).CurrentRegion.Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
            End If
        Next cell
    End Sub
    What can be the problem?
    Thanks a lot
    Last edited by shg; 10-29-2008 at 09:13 AM. Reason: add code tags

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I've edited your post to add Code Tags. Please read the Forum Rules before posting again.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    What have you done to debug?

    What line in the code gives the error, when processing what cell?

  7. #7
    Registered User
    Join Date
    10-28-2008
    Location
    Romania
    Posts
    3
    I find out... it was a stupid mistake, that the name of the sheet didn't coincided with the name defined in the code.

    Thank you soooooo much for your help. :-)

    I have a lot of to learn to get a little bit used to VBA...

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    You're welcome. My compliments on taking the initiative to modify the code yourself.

  9. #9
    Registered User
    Join Date
    07-02-2008
    Location
    Fort Worth, TX
    Posts
    99
    Subscript out of range
    This typically means that whatever your trying to do in a specific location doesn't exist, ie: name of the sheet, cell range, workbook. I get this all the time, and almost always just a typo on my part, or forgetting to open a document I'm trying to edit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1