Solved

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

Posted on 2013-01-20
8
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 43

Expert Comment

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

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
Independent Software Vendors: 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 34

Accepted Solution

by:
Norie earned 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

752 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