Looping dose not work in Excel

amy0299

New Member
Joined
Dec 14, 2013
Messages
3
Hi, I am new to this forum and also this is my first Excel VBA. I have multiple work sheets in the same workbook with the same columns but different number of rows. I want to add 5 more columns to each worksheet and also populate those columns.
The script is adding columns and populating cells correctly ONLY on the first worksheet. The looping is not working. Below is the script. Any Suggestion / correction is greatly appreciated. Thanks in advance for all your help!

Sub add_cell()


Dim wrk As Workbook
Dim WS As Worksheet


Set wrk = ActiveWorkbook


For Each WS In wrk.Worksheets
If WS.Index = wrk.Worksheets.Count Then
Exit For
End If

Columns("A:E").Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Value = "Date"
Range("A2").Value = Format(Now(), "mm-dd-yyyy")

Range("B1").Value = "Month"
Range("B2").Value = Format(Now(), "MMM")

Range("C1").Value = "Tab"
Range("C2").Value = ActiveSheet.Name

Range("D1").Value = "Name"
Range("D2").FormulaR1C1 = "=CONCATENATE(RC[2],"" "",RC[3])"

Range("E1").Value = "Type"
Range("E2").FormulaR1C1 = "=IF(LEFT(RC[-1],3)=""syn"",""Sync"", ""Non Sync"")"

Range("A2:E2").Select
Selection.Copy

With WS
.Range("A2:E" & .Cells(Rows.Count, "F").End(xlUp).Row).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range("A2:E" & .Cells(Rows.Count, "F").End(xlUp).Row).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End With


Exit For
Next WS



End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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