Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

839 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