AllExperts > Excel 
Search      
Excel
Volunteer
Answers to thousands of questions
 Home · More Excel Questions · Answer Library  · Encyclopedia ·
More Excel Answers
Question Library

Ask a question about Excel
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Bill
Expertise
I can provide help with most all EXCEL questions and most all questions about writing EXCEL macros. I have been developing macros for about 10 years in EXCEL and have switched to it from Lotus 1-2-3 after about 10 years of writing macros in it. Typically, I will not write a macro for you unless it is very short because of all the details a macro has to know about to work every time all the time. Please understand that I do not know it all and will be the first to say so. As politely as possible, I don't write macros for people on this site who need one, want one, seem to imply that they need one, and/or seem to think I am expected to write one UNLESS they are very short, quick, and simple. 99% of all macros are more involved than what you think and rarely am I provided with enough specific and complete details to have the code work the first time and every time. This typically means too many follow-up emails, and subsequent macro changes due to lack of specific details, just to get those details so that the macro would work, all of which is on my own free time. The voice of experience from responding to many questions from people who ask me to write a macro for them from this site tells me this. I don't mean to come across as unhelpful but macros are usually very specific and without ALL of the specifics the macro I would write will not address all of your needs and the layout, location, formatting, conditions, etc. of your data and any related files the macro would have to work with. What seems like a simple task to you is almost always more involved than what you think to have the macro ALWAYS work in EVERY situation. If you have a macro you have already written and have a question about it then perhaps I could help with that. I am sure and hope you can and do understand.

 
   

You are here:  Experts > Computing/Technology > Microsoft Software > Excel > Excel VBA Formula Problem

Excel - Excel VBA Formula Problem


Expert: Bill - 7/10/2009

Question
Hey Bill! I'm encountering a problem when trying to use a macro to put a formula into a cell. When it puts the formula into the cell, the formula reads as =IF(LEFT(Cells(2,1),12)=(LEFT(Cells(3,1),12)),1,0) when I want it to read as =IF(LEFT(A2,12)=(LEFT(A3,12)),1,0). I don't know why this is happening. Basically what I am doing is writing a macro that will see if A2 matches the first 12 characters of any other cell in the column. Then I want to see if A3 matches any of the other cells in the column, etc. Any advice on how to do that? My code is below. Thanks in advance!


Sub Match_Rows()

Dim x As Integer
Dim y As Integer



Columns("I:I").Select
Selection.NumberFormat = "0.00"


x = 2
y = 3

Do Until Cells(y, 1) = ""

Cells(x, 9).Formula = "=IF(LEFT(Cells(" & x & ",1),12)=(LEFT(Cells(" & y & ",1),12)),1,0)"

If CInt(Cells(x, 9).Value) = 1 Then
Rows("x:x").Select
   With Selection.Interior
       .Pattern = xlSolid
       .PatternColorIndex = xlAutomatic
       .Color = 65535
       .TintAndShade = 0
       .PatternTintAndShade = 0
       
End With

ElseIf CInt(Cells(x, 9).Value) = 0 Then
 y = y + 1
End If


Loop
   
       
    
End Sub  

Answer
Based on the way you are doing this and without suggesting a different way then try this easier to read approach:

Dim FirstCell As String
Dim SecondCell As String
FirstCell = Cells(x, 1).Address _(rowabsolute:=False,_columnabsolute:=False)
SecondCell = Cells(y, 1).Address _
(rowabsolute:=False, columnabsolute:=False)

Cells(x, 9).Formula = "=IF(LEFT(" & FirstCell & ",12)=LEFT(" & SecondCell & " ,12),1,0)"

Since x is not changing within the loop you don't need that variable and can just substitute 2 for x and therefore you can use:

Cells(2, 9).Formula = "=IF(LEFT(" & FirstCell & ",12)=LEFT(" & SecondCell & " ,12),1,0)" in one example of where the substitution can be made.

Finally, there is no need to write a formula to a cell via code and then have the code check the value of that calculated cell.  

Just let the code do all of that internally by assigning that calculation's value to a VBA variable and then have the code check the value of that variable.  

You are making EXCEL work a lot harder than it needs to although this approach is not "wrong".  It is just inefficient.

Add to this Answer   Ask a Question


 
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.