Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

705 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

16 Experts available now in Live!

Get 1:1 Help Now