Results 1 to 6 of 6

Thread: [Excel VBA] Error 1004

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    [Excel VBA] Error 1004

    Hello

    I am working on some code and it sometimes gives Error 1004 and I can not for the life of me figure out why. I have a bunch of data and the data in Column L (a string) sometimes gets broken into pieces and spread across 2 or more lines. This code is supposed to fix it and put everything back together how it should be.

    So, if I'm in a certain row, CurrentRow, sometimes the string in Column L is broken up so that part of it is in the correct spot, but then a bit more of it is in row CurrentRow + 1 in column A. There may be more in rows CurrentRow + 2, CurrentRow + 3, and so on, but after 4 or 5 rows, we're usually done. The rest of the data that is supposed to appear in CurrentRow columns M through AA is on the last row containing the data from column L in Columns B through whatever.

    The error always occurs at this line of code:

    Code:
    .Range("L" & CurrentRow).Value = .Range("L" & CurrentRow).Value & .Range("A" & CurrentRow + 1).Value

    It works on most lines where the data is broken up but sometimes it doesn't. I can't give actual data where it messes up because it is policy data. But, there's nothing suspicious about it to me. It's a string. Any ideas come to mind? Thanks for any help

    Code:
    Sub ScrubNewData2()
        Application.ScreenUpdating = False
        Dim LastRow As Long, CurrentRow As Long, MessedUpRows As Long
        Dim YearCellValue As String, DescripCellValue As String
        Dim LastNonCurrencyCol As String, FirstCurrencyCol As String, LastCol As String
    
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' These are the only things that need to be changed from spreadsheet to spreadsheet
        LastNonCurrencyCol = "U"
        FirstCurrencyCol = "V"
        LastCol = "AA"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
        With Worksheets("New")
    
            NumCellsCopied = .Range("M1", LastCol & "1").Count
            LastRow = .Range("A500000").End(xlUp).Row
            
            CurrentRow = 2
            Do Until CurrentRow > LastRow
                If Left(.Range("L" & CurrentRow).Value, 3) = "=T(" Then
                    
                    
                    ' Count number of rows messed up
                    MessedUpRows = 1
                    If .Range("B" & CurrentRow + 1) = "" Then
                        MessedUpRows = .Range("B" & CurrentRow).End(xlDown).Row - CurrentRow
                        For i = 1 To MessedUpRows - 1
                            If .Range("A" & CurrentRow + 1) <> "" Then
                                .Range("L" & CurrentRow).Value = .Range("L" & CurrentRow).Value & .Range("A" & CurrentRow + 1).Value
                            End If
                            
                            .Range("A" & CurrentRow + 1).EntireRow.Delete
                            LastRow = LastRow - 1
                            
                                
                        Next i
                    End If
                    .Range("L" & CurrentRow).Value = .Range("L" & CurrentRow).Value & .Range("A" & CurrentRow + 1).Value
                    .Range("L" & CurrentRow).Copy
                    .Range("L" & CurrentRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
    
        
                    .Range("M" & CurrentRow, LastCol & CurrentRow).Value = .Range("B" & CurrentRow + 1).Resize(1, NumCellsCopied).Value
                    .Range("A" & CurrentRow + 1).EntireRow.Delete
                    LastRow = LastRow - 1
                End If
                CurrentRow = CurrentRow + 1
            Loop
            
        End With
    End Sub
    Last edited by ActSciMan; May 21st, 2012 at 01:57 PM. Reason: Forgot to mention specific piece of code

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [Excel VBA] Error 1004

    Put some code in to stop when it encounters that error, then see what the value of L+current row and A+current row+1 are.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Re: [Excel VBA] Error 1004

    Thanks, actually I already have used the Immediate window to see the values. They both are long strings. I even tried:

    Code:
    print .Range("L" & CurrentRow).Value & .Range("A" & CurrentRow + 1).Value
    and it works fine and does what I would expect it to do.

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [Excel VBA] Error 1004

    Any way you can "dummy up" a small part of the data and then show us the workbook? Not sure what to suggest without seeing it behave. Does it always fail at the same place?

  5. #5

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Re: [Excel VBA] Error 1004

    Yes, it is stopping at the same place every time.

    First part of the string which is in cell L CurrentRow is:
    =T("&lt;&lt;PER PH* OTHER&gt;&gt;

    Second part which is in Column A of next row is just a long string, no special characters of any sort (has , and .)

    The error is happening when trying to set the value of L CurrentRow to the concatenation of these two values. The part that is weird is that this works fine for many other similar such concatenations that occur before it. All contain weird things like "&gt;&gt;". The first string always starts with =T("
    Last edited by ActSciMan; May 21st, 2012 at 03:41 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Re: [Excel VBA] Error 1004

    Well, I still don't understand why an error was caused but I believe I have fixed it. I created a string variable TempString to store the value in and concatenated with that... TempString = TempString & ... and then finally put that back in L CurrentRow. Now, it works.

Tags for this Thread

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