Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Insert formula in column A & B for any row where column D is not null/blank

Posted on 2013-01-20
8
Medium Priority
?
411 Views
Last Modified: 2013-01-21
I have a formula in A2 and B2 and I am currently using the following to auto-fill the formula from A2 "=M2-7" into column A and "=L2" into column B of any row where column D is not blank/null.  The problem is that it's not very consistent. sometimes it works and sometimes it doesn't.

Seems like it would be better to just write the formula into each cell where D is not blank/null?

This is how I am doing it now.

Sub Autofill ()

    Sheets("Engines").Select
    Range("A2").Select
   
    Dim lastrow As Long
    lastrow = Worksheets("Engines").Cells(Rows.Count, "D").End(xlUp).Row
    Selection.AutoFill Destination:=Range("A2:A" & lastrow)
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B" & lastrow)

End Sub

Thanks,

swjtx99
0
Comment
Question by:swjtx99
  • 4
  • 2
  • 2
8 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38799114
In what way does it NOT work?
0
 
LVL 35

Expert Comment

by:Norie
ID: 38799136
Does this work any better?
Sub MyAutofill ()
Dim lastrow As Long

    With Worksheets("Engines")
           lastrow = .Cells(Rows.Count, "D").End(xlUp).Row
           .Range("A2:B2").Copy .Range("A2:B" & lastrow)
    End With
End Sub

Open in new window

0
 

Author Comment

by:swjtx99
ID: 38799349
Hi ssaqibh,

Very strange sometimes it just deletes everything in columns a and b. I've messed with it for hours and can't figure out why. Thought maybe just doing something like formula.r1c1? but I've been googling for examples how to do that and haven't found anything close enough yet.

Hi imnorie,

I think the problem is that the formula I have put into A2 and B2 gets deleted for some unknown reason so not sure your solution would work either. Funny it doesn't happen every time.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38799352
Can you send us a small sample workbook?
0
 

Author Comment

by:swjtx99
ID: 38799386
Hi,

Ok, I just figured out why the formulas in A2 and B2 are getting deleted.

In a section above, I'm deleting all rows that match a certain criteria in column G. The reason why sometimes it worked and sometimes it didn't is because sometimes the value in G2 matched (and thus got deleted) and sometimes it didn't. I suppose I could have that search/delete happen AFTER I populate columns A and B. Might be simpler than trying to insert the formula and copy it down?

swjtx99
0
 

Author Comment

by:swjtx99
ID: 38799438
Hi,

Ok, I moved the code and all is well.

Thanks for taking a look at my question.

swjtx99
0
 
LVL 35

Accepted Solution

by:
Norie earned 2000 total points
ID: 38799553
If the formulas are as simple as you posted.
Sub MyAutofill ()
Dim lastrow As Long

    With Worksheets("Engines")
           lastrow = .Cells(Rows.Count, "D").End(xlUp).Row
           .Range("A2:B" & LastRow).Formula = Array("=M2-7", "=L2")
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:swjtx99
ID: 38801886
I think moving the code as I did solved the problem but your solution also works and I can archive and save for later. Populating a formula into a Cell is a good solution to have.

Thanks for the help.

swjtx99
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

916 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