excelotter
New Member
- Joined
- Jul 17, 2011
- Messages
- 5
Hello,
I pretty much have no idea what I'm doing in Excel macros, but have been having a go at editing a recorded macro all day without success.
My macro needs 2 loops. The first works fine, selecting a series of workbooks to apply the rest of the macro to. For the second I need a loop which repeats the action for each of 300 odd columns. I first tried to type out the action for each column and the macro became too long to function! Very bad coding practice I know.
Here's the first loop that works fine:
For m = 2 To 42
f$ = Format(m) & ".xlsx"
ChDrive "N:"
ChDir "N:\My Documents\INDICATOR MACROS 666\stage 2 - requirements filtered 666"
Workbooks.Open Filename:=f$
Range("A2:Y2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ChDir "N:\My Documents\INDICATOR MACROS 666"
Workbooks.Open Filename:="N:\My Documents\INDICATOR MACROS 666\Resource met assessment 666.xlsx"
Range("A3").Select
ActiveSheet.Paste
...............[code here]
Windows(f$).Activate
Range("W2").Select
ActiveSheet.Paste
Range("W1").Select
ActiveCell.FormulaR1C1 = "max"
Range("X1").Select
ActiveCell.FormulaR1C1 = "sum"
ActiveWorkbook.Save
ActiveWindow.Close
ActiveWindow.Close False
Next m
End Sub
Where the [code here] is above, the original recorded macro did this:
Range("W3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("W3:W210000")
Range("W3:W210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("X3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("X3:X210000")
Range("X3:X210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("Y3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("Y3:Y210000")
Range("Y3:Y210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("Z3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("Z3:Z210000")
Range("Z3:Z210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("AA3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("AA3:AA210000")
Range("AA3:AA210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
and kept going for lots and lots of columns..........
I want to write a loop so that my macro does not become too long. I want the loop to repeat the code as you can see above, but indexing the columns. Here is my feeble attempt, which doesn't work:
For n = 26 To 364
Cells(3, n).Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range(Cells(3, n), Cells(200010, n))
Range(Cells(3, n), Cells(200010, n)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next n
Any suggestions would be very much appreciated. It seems like it should be simple enough, but I have very, very little experience of VBA.
Cheers,
Amy
I pretty much have no idea what I'm doing in Excel macros, but have been having a go at editing a recorded macro all day without success.
My macro needs 2 loops. The first works fine, selecting a series of workbooks to apply the rest of the macro to. For the second I need a loop which repeats the action for each of 300 odd columns. I first tried to type out the action for each column and the macro became too long to function! Very bad coding practice I know.
Here's the first loop that works fine:
For m = 2 To 42
f$ = Format(m) & ".xlsx"
ChDrive "N:"
ChDir "N:\My Documents\INDICATOR MACROS 666\stage 2 - requirements filtered 666"
Workbooks.Open Filename:=f$
Range("A2:Y2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ChDir "N:\My Documents\INDICATOR MACROS 666"
Workbooks.Open Filename:="N:\My Documents\INDICATOR MACROS 666\Resource met assessment 666.xlsx"
Range("A3").Select
ActiveSheet.Paste
...............[code here]
Windows(f$).Activate
Range("W2").Select
ActiveSheet.Paste
Range("W1").Select
ActiveCell.FormulaR1C1 = "max"
Range("X1").Select
ActiveCell.FormulaR1C1 = "sum"
ActiveWorkbook.Save
ActiveWindow.Close
ActiveWindow.Close False
Next m
End Sub
Where the [code here] is above, the original recorded macro did this:
Range("W3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("W3:W210000")
Range("W3:W210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("X3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("X3:X210000")
Range("X3:X210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("Y3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("Y3:Y210000")
Range("Y3:Y210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("Z3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("Z3:Z210000")
Range("Z3:Z210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("AA3").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("AA3:AA210000")
Range("AA3:AA210000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
and kept going for lots and lots of columns..........
I want to write a loop so that my macro does not become too long. I want the loop to repeat the code as you can see above, but indexing the columns. Here is my feeble attempt, which doesn't work:
For n = 26 To 364
Cells(3, n).Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range(Cells(3, n), Cells(200010, n))
Range(Cells(3, n), Cells(200010, n)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next n
Any suggestions would be very much appreciated. It seems like it should be simple enough, but I have very, very little experience of VBA.
Cheers,
Amy