Solved

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

Posted on 2015-01-08
3
126 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

732 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