Solved

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

Posted on 2013-01-20
8
380 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 33

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
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38799352
Can you send us a small sample workbook?
0
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.

 

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 33

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

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
Vb.net dynamic formulas in runtime 11 61
excel connection properties parameters grayed out 5 26
Easy Excel formula needed 4 26
Help Updated Qtr 2 9
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

910 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

23 Experts available now in Live!

Get 1:1 Help Now