Excel VBA Loop Question

pink705

New Member
Joined
May 10, 2011
Messages
2
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
I am trying to teach myself VBA for Excel, but I am having some issues with the correct syntax for the loop I am try to do, or figuring out if I should do a loop at all. I have created loops before that insert formulas or do calculations on the Active worksheet until the last cell, but this case is a bit different and I would appreciate any help that anyone can offer.<o:p></o:p>
<o:p> </o:p>
Basically, what I would like to do is insert a value from Worksheet2, column A into cell A4 in Worksheet1, copy Worksheet1 to a new book, copy paste the values, save the document with the value in A4 plus the date and close the copied document. I would like to do this for every cell that has a value in Worksheet2 from column A, and then stop.<o:p></o:p>
<o:p> </o:p>
Here is the code I have so far:<o:p></o:p>
<o:p> </o:p>
Sub Macro_Test()<o:p></o:p>
'<o:p></o:p>
' Macro_Test Macro<o:p></o:p>
'<o:p></o:p>
' Keyboard Shortcut: Ctrl+Shift+Q<o:p></o:p>
'<o:p></o:p>
Sheets("Sheet1").Select<o:p></o:p>
Range("A4").Select<o:p></o:p>
ActiveCell.FormulaR1C1 = "=Worksheet2!R[-2]C" (this is the part I need to loop)<o:p></o:p>
Range("A4").Select<o:p></o:p>
Sheets("Sheet1").Select<o:p></o:p>
Sheets("Sheet1").Copy<o:p></o:p>
Cells.Select<o:p></o:p>
Selection.Copy<o:p></o:p>
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _<o:p></o:p>
:=False, Transpose:=False<o:p></o:p>
Range("A4").Select<o:p></o:p>
Dim newFile As String, fName As String<o:p></o:p>
fName = Range("A4").Value<o:p></o:p>
newFile = fName & " " & Format$(Date, "mm-dd-yyyy")<o:p></o:p>
ChDir _<o:p></o:p>
"C:\Documents and Settings\pink705\Desktop"<o:p></o:p>
ActiveWorkbook.SaveAs Filename:=newFile<o:p></o:p>
ActiveWindow.Close<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
I have managed to code everything except for the loop part; I can’t seem to figure out how to make it reference "=Worksheet2!R[-2]C" and do all of the code, then "=Worksheet2!R[-1]C" and do all of the code, etc.<o:p></o:p>
<o:p> </o:p>
Any help anyone could offer would be great.<o:p></o:p>
<o:p></o:p>
<o:p>I am using Excel 2007, Windows XP </o:p>

Thanks.<o:p></o:p>
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top