Excel macro won't loop

Closed
srp7777 Posts 2 Registration date Sunday November 22, 2009 Status Member Last seen November 24, 2009 - Nov 23, 2009 at 03:08 PM
anonymoues Posts 2 Registration date Tuesday December 1, 2009 Status Member Last seen December 2, 2009 - Dec 2, 2009 at 05:52 AM
Hello,

I'm new to this forum and to VBA. I need help. Basically I've been search the internet for code to do what I need and nothing seems to work completely. This is what I need:

I have an excel 2003 file with a "Data" tab and a "Template"tab. The data tab will have over 400 rows of client data that I need to copy individual client information into the template tab and then save the "Template" tab only as a new file using the data field (Borrower Name and Month End Date) as the file name.

I can get the data to copy in to the "Template" tab and save as a new file but I can not get it to loop. Can someone help please. I'll attach a sample. Thanks in advance.

Sub FasLoop()
'
' FasLoop Macro
' Macro recorded 11/20/2009 by sphee
'
      ' Select cell A2, *first line of data*.
    Sheets("Data").Select
    Range("A2").Select
                  ' Set Do loop to stop when an empty cell is reached.
      Do Until IsEmpty(ActiveCell)
         ' Insert your code here.
    Sheets("Template").Select
    Range("E10").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-8]C[-2]"
    Range("E11").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-9]C[9]"
    Range("E12").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-10]C[10]"
    Range("E14").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-12]C[-1]"
    Range("K10").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-8]C[-6]"
    Range("K11").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-9]C[-4]"
    Range("E19").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-17]C[23]"
    Range("E21").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-19]C[24]"
    Range("E23").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-21]C[26]"
    Range("J19").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-17]C[22]"
    Range("J21").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-19]C[23]"
    Range("J23").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-21]C[27]"
    Range("J24").Select
    ActiveWindow.SmallScroll Down:=9
    Range("J25").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-23]C[28]"
    Range("J26").Select
    ActiveWindow.SmallScroll Down:=6
    Range("F35").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-33]C[-5]"
    Range("G35").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-33]C[9]"
    Range("G36").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-34]C[40]"
    Range("G37").Select
    ActiveWindow.SmallScroll Down:=3
    Range("J37:J40").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-35]C[35]"
    Range("K37:L40").Select
    ActiveCell.FormulaR1C1 = "=Data!R[-35]C[35]"
    Range("K41").Select
    ActiveWindow.SmallScroll Down:=3
    Range("A1").Select
    
    Selection.Copy
    Cells.Select
    Application.CutCopyMode = False
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    ActiveSheet.Paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveWindow.SmallScroll Down:=18
    Range("F35").Select
    Application.CutCopyMode = False
    Range("F35").Select
    ActiveCell.FormulaR1C1 = "'10_31_2009"
    Range("F35").Select
    ActiveCell.FormulaR1C1 = "'10-31-2009"
    Range("P1").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(R[9]C[-11],"" "",R[34]C[-10])"
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 1
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    
    fname = Range("P1").Formula
    ActiveWorkbook.SaveAs Filename:=fname

    Range("P1").Select
    Selection.ClearContents
    Cells.Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        
    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets("Data").Delete
    Application.DisplayAlerts = True
    ActiveWorkbook.Close SaveChanges:=True
    
    Workbooks.Open Filename:="I:\Stacie\Nadine\Fas 114 Template Orginal.xls"
    Range("A2").Select

    
         ' Step down 1 row from present location.
    ActiveCell.Offset(1, 0).Select
      
      Loop
   End Sub
Related:

2 responses

Slayer2004 Posts 3 Registration date Monday November 30, 2009 Status Member Last seen December 1, 2009
Dec 1, 2009 at 04:58 AM
have you considered a FOR loop at all?

Andy
0
anonymoues Posts 2 Registration date Tuesday December 1, 2009 Status Member Last seen December 2, 2009
Dec 2, 2009 at 05:52 AM
Hi Srp7777,

You should try using For loop..

First u have to count no of rows used..
RowUsed=Worksheets("template").UsedRange.Rows.Count

For i=1 to RowUSed
'put your code here.
Next i
0