Whats wrong with my code?

KateA

New Member
Joined
Jun 24, 2014
Messages
6
Hi,
I'm compiling a master spreadsheet that pulls values from about 75 other sheets.
I made a mistake and dont want to fix it cell by cell, so ive been trying to learn how to write a macro (trying and failing). Here's what is in the cells of the range concerned;

=SUM(IF((SOURCESHEETX!D20:D40="VAT Gtee")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))+SUM(IF((SOURCESHEETX!D20:D40="VRT Gtee")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))+SUM(IF((SOURCESHEETX!D20:D40="Performance Gtee")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))+SUM(IF((SOURCESHEETX!D20:D40="Gtee")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))+SUM(IF((SOURCESHEETX!D20:D40="Performance Guarantee")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))+SUM(IF((SOURCESHEETX!D20:D40="Letter of Credit")*(SOURCESHEETX!H20:H40="EUR"),SOURCESHEETX!L20:L40,0))

The value for SourceSheetX changes for each cell in the column.
Ive been trying to replace "Gtee" with "C&E Gtee", but the macro I write just keeps deleting everything in the range...(help!)

Here it is

Sub FixMistake ()

OriginalText = MyCell.Value<o:p></o:p>
CorrectedText = Replace(OriginalText, "Gtee", "C&E Gtee", 310, 1)<o:p></o:p>
For Each MyCell In Range("R27:R80")<o:p></o:p>
MyCell.Value = CorrectedText<o:p></o:p>
Next MyCell<o:p></o:p>
End Sub

Whats wrong? Any help would be great. Thanks a mill!
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Instead of Value try using Formula.
Code:
Sub FixMistake ()
    For Each MyCell In Range("R27:R80")
        OriginalText = MyCell.Formula
        CorrectedText = Replace(OriginalText, "Gtee", "C&E Gtee", 310, 1)
        MyCell.Formula = CorrectedText
    Next MyCell
End Sub
 

KateA

New Member
Joined
Jun 24, 2014
Messages
6
Instead of Value try using Formula.
Code:
Sub FixMistake ()
    For Each MyCell In Range("R27:R80")
        OriginalText = MyCell.Formula
        CorrectedText = Replace(OriginalText, "Gtee", "C&E Gtee", 310, 1)
        MyCell.Formula = CorrectedText
    Next MyCell
End Sub

A message box flashed up, displaying Run Time Error '1004': Application Defined or Object Defined error.
I forgot to mention that the formula I'm using needs to be entered as an array. Does that change anything?
Thank you so much for your time!
 

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,334
Try using
Code:
MyCell.FormulaArray
, the code is unable to tell if the formula you want to enter into a cell is an array one or not.
 

KateA

New Member
Joined
Jun 24, 2014
Messages
6
Now the message box is telling me I'm 'unable to set the FormulaArray property of the range class. Any solutions?

Again thanks a million!
 

Forum statistics

Threads
1,082,139
Messages
5,363,363
Members
400,731
Latest member
Jackserver

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top