Solved

VBA Run-Time Error Can't pin it down

Posted on 2014-11-07
4
132 Views
Last Modified: 2014-11-16
Hello, I just joined today!
I'm using VBA in Excel 2010. My code takes a cell value does several things too it and adds it to a table in another sheet. After this it compares the column header of the changed cell to an VBA IF statement and selects the correct formula to insert into the changed cell. The selection of the formula is working correctly however after the formula is selected it is passed back and it attempts to insert into the changed cell I then run into my run time error.
I'm not sure what is causing the issue.
Any ideas as to a solution?

This is where the run-time error occurs
cell is defined as a range and formula_1 is defined as a string
'Insert formula_1 into the changed cell
                cell.Formula = formula_1

Open in new window

'
Formula selection code
Public Function WorksheetFormula(ByVal target As Range, ColumnHeader As String, formula_1 As String, columnFormula As String)

If ColumnHeader = "% Discount" Then
           formula_1 = "=IFERROR((GETPIVOTDATA(""<wbr ></wbr>Max of ""&INDIRECT((ADDRESS(14,CO<wbr ></wbr>LUMN()))),<wbr ></wbr>'Database1<wbr ></wbr> PivotTable'!$A$1," & _
           """KEY"",LEFT(INDIRECT(CON<wbr ></wbr>CATENATE("<wbr ></wbr>"B"",SUM(R<wbr ></wbr>OW()-1))),<wbr ></wbr>4)&Current<wbr ></wbr>HFMFamily&<wbr ></wbr>(OFFSET(IN<wbr ></wbr>DIRECT(ADD<wbr ></wbr>RESS(ROW()<wbr ></wbr>,COLUMN())<wbr ></wbr>),-(SUM(RO<wbr ></wbr>W()-(6))),<wbr ></wbr>-(MOD(COLU<wbr ></wbr>MN()+1,4))<wbr ></wbr>))" & _
           "&CurrentYear,""PRODUCT"",<wbr ></wbr>CurrentHFM<wbr ></wbr>Family,""P<wbr ></wbr>ROGRAM"",L<wbr ></wbr>EFT(INDIRE<wbr ></wbr>CT(CONCATE<wbr ></wbr>NATE(""B""<wbr ></wbr>,SUM(ROW()<wbr ></wbr>-1))),4),"<wbr ></wbr> & _
           """CUSTOMERSEG"",CurrentCu<wbr ></wbr>stomerSegm<wbr ></wbr>ent,""MONT<wbr ></wbr>H"",(OFFSE<wbr ></wbr>T(INDIRECT<wbr ></wbr>(ADDRESS(R<wbr ></wbr>OW(),COLUM<wbr ></wbr>N())),-(SU<wbr ></wbr>M(ROW()-(6<wbr ></wbr>))),-(MOD(<wbr ></wbr>COLUMN()+1<wbr ></wbr>,4)))),""Y<wbr ></wbr>EAR"",Curr<wbr ></wbr>entYear)),<wbr ></wbr>"")"
        
        ElseIf ColumnHeader = "% Take" Then
            formula_1 = "=IFERROR((GETPIVOTDATA(""<wbr ></wbr>Max of ""&INDIRECT((ADDRESS(14,CO<wbr ></wbr>LUMN()))),<wbr ></wbr>'Database1<wbr ></wbr> PivotTable'!$A$1," & _
            """KEY"",LEFT(INDIRECT(CON<wbr ></wbr>CATENATE("<wbr ></wbr>"B"",SUM(R<wbr ></wbr>OW()-1))),<wbr ></wbr>4)&Current<wbr ></wbr>HFMFamily&<wbr ></wbr>(OFFSET(IN<wbr ></wbr>DIRECT(ADD<wbr ></wbr>RESS(ROW()<wbr ></wbr>,COLUMN())<wbr ></wbr>),-(SUM(RO<wbr ></wbr>W()-(6))),<wbr ></wbr>-(MOD(COLU<wbr ></wbr>MN()+1,4))<wbr ></wbr>))" & _
            "&CurrentYear,""PRODUCT"",<wbr ></wbr>CurrentHFM<wbr ></wbr>Family,""P<wbr ></wbr>ROGRAM"",L<wbr ></wbr>EFT(INDIRE<wbr ></wbr>CT(CONCATE<wbr ></wbr>NATE(""B""<wbr ></wbr>,SUM(ROW()<wbr ></wbr>-1))),4),"<wbr ></wbr> & _
            """CUSTOMERSEG"",CurrentCu<wbr ></wbr>stomerSegm<wbr ></wbr>ent,""MONT<wbr ></wbr>H"",(OFFSE<wbr ></wbr>T(INDIRECT<wbr ></wbr>(ADDRESS(R<wbr ></wbr>OW(),COLUM<wbr ></wbr>N())),-(SU<wbr ></wbr>M(ROW()-(6<wbr ></wbr>))),-(MOD(<wbr ></wbr>COLUMN()+1<wbr ></wbr>,4)))),""Y<wbr ></wbr>EAR"",Curr<wbr ></wbr>entYear)),<wbr ></wbr>"")"
        
        ElseIf ColumnHeader = "Dollars" Then
            formula_1 = "=PRODUCT((OFFSET(INDIRECT<wbr ></wbr>(ADDRESS(R<wbr ></wbr>OW(),COLUM<wbr ></wbr>N())),-(SU<wbr ></wbr>M(ROW()-(8<wbr ></wbr>))),(MOD(C<wbr ></wbr>OLUMN()+1,<wbr ></wbr>4)))),(OFF<wbr ></wbr>SET(INDIRE<wbr ></wbr>CT(ADDRESS<wbr ></wbr>(ROW(),COL<wbr ></wbr>UMN())),0,<wbr ></wbr>1))," & _
            "(OFFSET(INDIRECT(ADDRESS(<wbr ></wbr>ROW(),COLU<wbr ></wbr>MN())),0,2<wbr ></wbr>)))"
End If
   columnFormula = formula_1
End Function

Open in new window

0
Comment
Question by:R10131551
  • 2
4 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40429289
I don't understand the relationship between the first set of code and the second.

Need more context.
0
 

Author Comment

by:R10131551
ID: 40429303
The second set is the formula selection once this is determined it is set to variable "formula_1". This value is passed to the first set on line "cell.formula = formula_1". On this line I'm getting the 1004 error.

The problem seems to lie with using the "formula_1" string variable to house the formula.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40432245
Can you show me the context for calling "WorksheetFormula".

My best guess at this stage - you have not declared formula_1 with sufficient scope (e.g. a Dim or Public statement or similar).
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40439210
Place a message box in your code check formula_1 is defined at runtime

MsgBox formula_1
cell.Formula = formula_1

Open in new window


If it is not set then try declaring formula_1 as a global variable. That is declare it outside of a Function/Sub. Have a look at this link for more details
http://www.functionx.com/vbaexcel/Lesson03.htm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 154
WPF - External data binding to controls inside a UserControl 2 54
Best way to accomplish this task? 3 85
array6 challenfge 6 81
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

930 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

19 Experts available now in Live!

Get 1:1 Help Now