Solved

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

Posted on 2015-01-08
3
124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

749 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