Solved

Error 1004 "Application-defined or Object-defined error" when selecting a range

Posted on 2015-01-08
3
118 Views
Last Modified: 2015-01-14
Hello,

One of my Subs in my project used to work for last year... but now it errors out.

Error is on this line:

rng1.Value = rng2.Value

As you see, this Sub adds data from one file ("Raw Data" sheet) onto the other file's "Raw Data".
The destination file has YTD (year worth of data).

Any ideas how to fix it, please?

Thanks!!!

Here is the Sub:

Sub CRQs_YTD_UpdateData(lngLastYTDRow As Long, AsOfDate As Date)

  Dim lngLastSrcRow, lngNumSrcDataRows As Long
  Dim intCol As Integer
  Dim rng1, rng2 As Range

  Dim fndCell As Range
 

  'If we already have rows for this reporting week we need to refresh by overriding the values.
  'Otherwise append the new reporting week.
  Set fndCell = ActiveSheet.Columns(1).Find(What:=AsOfDate, After:=Cells(1, 1), LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
 
  If Not fndCell Is Nothing Then
    'Replace/Refresh
    lngLastSrcRow = Workbooks(pubStrWklyOutputFileName).Sheets("RawData").UsedRange.Rows.Count
    lngNumSrcDataRows = lngLastSrcRow - 1

    Set rng1 = Range(Cells(fndCell.Row, 1), Cells(fndCell.Row + lngNumSrcDataRows - 1, 1))
    rng1.Value = AsOfDate
   
    Set rng1 = Range(Cells(fndCell.Row, 2), Cells(fndCell.Row + lngNumSrcDataRows - 1, 53))
    Set rng2 = Range(Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(2, 1), _
                     Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(lngLastSrcRow, 52))
    rng1.Value = rng2.Value
 
  Else
    'Append
    lngLastSrcRow = Workbooks(pubStrWklyOutputFileName).Sheets("RawData").UsedRange.Rows.Count
    lngNumSrcDataRows = lngLastSrcRow - 1

    Set rng1 = Range(Cells(lngLastYTDRow + 1, 1), Cells(lngLastYTDRow + lngNumSrcDataRows, 1))
    rng1.Value = AsOfDate

    Set rng1 = Range(Cells(lngLastYTDRow + 1, 2), Cells(lngLastYTDRow + lngNumSrcDataRows, 53))
    Set rng2 = Range(Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(2, 1), _
                     Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(lngLastSrcRow, 52))
   
    rng1.Value = rng2.Value
  End If
 
  'Update Week-Ending date
  Names("Wk_Ending").RefersToLocal = str(AsOfDate)

End Sub
0
Comment
Question by:CABRLU63
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 100 total points
ID: 40538940
I'm not sure which clause of the If...Then...Else statement your code is erroring out, but if any of the row arguments in the Cells() statements equals zero, that would cause an error.

Also, I note that rng1.Value is being assigned the variable AsOfDate, then re-assigned to rng2.Value (lines 21 and 33).  This seems unnecessary.

Sub CRQs_YTD_UpdateData(lngLastYTDRow As Long, AsOfDate As Date)

   Dim lngLastSrcRow, lngNumSrcDataRows As Long
   Dim intCol As Integer
   Dim rng1, rng2 As Range

   Dim fndCell As Range
   
   'If we already have rows for this reporting week we need to refresh by overriding the values.
   'Otherwise append the new reporting week.
   Set fndCell = ActiveSheet.Columns(1).Find(What:=AsOfDate, After:=Cells(1, 1), LookIn:=xlFormulas, _
             LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
             MatchCase:=False, SearchFormat:=False)
   
    If Not fndCell Is Nothing Then
        'Replace/Refresh
        lngLastSrcRow = Workbooks(pubStrWklyOutputFileName).Sheets("RawData").UsedRange.Rows.Count
        lngNumSrcDataRows = lngLastSrcRow - 1
        
        Set rng1 = Range(Cells(fndCell.Row, 1), Cells(fndCell.Row + lngNumSrcDataRows - 1, 1))
        rng1.Value = AsOfDate
        
        Set rng1 = Range(Cells(fndCell.Row, 2), Cells(fndCell.Row + lngNumSrcDataRows - 1, 53))
        Set rng2 = Range(Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(2, 1), _
                         Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(lngLastSrcRow, 52))
        rng1.Value = rng2.Value
    Else
        'Append
        lngLastSrcRow = Workbooks(pubStrWklyOutputFileName).Sheets("RawData").UsedRange.Rows.Count
        lngNumSrcDataRows = lngLastSrcRow - 1
        
        Set rng1 = Range(Cells(lngLastYTDRow + 1, 1), Cells(lngLastYTDRow + lngNumSrcDataRows, 1))
        rng1.Value = AsOfDate
        
        Set rng1 = Range(Cells(lngLastYTDRow + 1, 2), Cells(lngLastYTDRow + lngNumSrcDataRows, 53))
        Set rng2 = Range(Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(2, 1), _
                         Workbooks(pubStrWklyOutputFileName).Sheets("RawData").Cells(lngLastSrcRow, 52))
        
        rng1.Value = rng2.Value
    End If
   
    'Update Week-Ending date
    Names("Wk_Ending").RefersToLocal = Str(AsOfDate)

 End Sub

Open in new window


-Glenn
0
 

Assisted Solution

by:CABRLU63
CABRLU63 earned 0 total points
ID: 40540513
Ugh...
I've noticed there is a value (a Note with 10 000 characters in it) in one of the cells, that's the rown on which the code stops Inserting routine...
Funny thing is that in the past this Sub had no problems to even load larger Notes (with 16 000+ number of characters).
Once I manually trimmed that particular Note cell, the code works like charm...
0
 

Author Closing Comment

by:CABRLU63
ID: 40548529
- this (trimming) cuold help other users with similar issue.
For now, there is not definite solution for this. I'm going just go ahead and trim that value with VBA code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multi-user Excel not reflecting who actually has the file opened 10 30
location range 4 22
Boolean help 6 27
Excel - Transposing columns/rows (sort of!) 7 12
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now