Advertisement

11.23.2006 at 10:30AM PST, ID: 22070902
[x]
Attachment Details

VBA to copy down formula to last row with data

Asked by seriousnick in Microsoft Excel Spreadsheet Software

Tags: vba, copy, row, last

Hi Experts

I am using the macro below to format and juggle a sheet including merging 2 cells into 1 on each row.

However, the sheet I am using has x amount of rows and I need to carry out this macro on other sheets with varying amount of rows.

Rather than referencing a set number of rows to autofill (in this case 790), how can I adapt the code to only select the "live data" rows so that it will only autofill down to the last live row?

I then have another macro I run to merge all these sheets into 1.

Many thanks

Nick

**************
Sub Working()
    With Selection
           .WrapText = False
    End With
    Range("A:A,B:B,D:D,H:H").Select
    Range("H1").Activate
    Selection.Delete Shift:=xlToLeft
        Range("E1").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]&"" ""&RC[-1]"
    Range("E1").Select
        'option to autofill just data rows required here
    Selection.AutoFill Destination:=Range("E1:E790"), Type:=xlFillDefault
    Range("E1:E790").Select
    Columns("E:E").Select
    Selection.Copy
    Columns("C:C").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("D:E").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("A:A").Select
    Selection.Cut Destination:=Columns("D:D")
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("A:C").Select
    Columns("A:C").EntireColumn.AutoFit
    Columns("A:B").Select
    Selection.HorizontalAlignment = xlLeft
    Columns("C:C").Select
    Selection.HorizontalAlignment = xlRight
    Range("A1").Select
End SubStart Free Trial
[+][-]11.23.2006 at 10:45AM PST, ID: 18004247

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.23.2006 at 11:03AM PST, ID: 18004317

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.23.2006 at 11:29AM PST, ID: 18004405

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Excel Spreadsheet Software
Tags: vba, copy, row, last
Sign Up Now!
Solution Provided By: byundt
Participating Experts: 1
Solution Grade: A
 
 
[+][-]11.23.2006 at 11:40AM PST, ID: 18004441

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32