VBA Macro to insert worksheet name and copy it down column

LuvVBA

New Member
Joined
Aug 6, 2013
Messages
1
Hi,
The following macro copies two columns from every sheet in the workbook and pastes them into column B and C of the sheet named Journal entry. I would like to add to column A of the Journal entry worksheet the worksheet name for which the columns are being copied. Once I have the worksheet name in column A, I would like to be able to copy that name down up.

In the end I´ll have on the Journal entry worksheet column A populated with a worksheet name on each row and col B and C with its corresponding data. Thank you.

Here is the code I have so far:
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count

' Find last empty Row in the Journal Entry worksheet
Dim LastRow As Long
LastRow = Worksheets("Journal Entry").UsedRange.Rows.Count

' Copy Columns G and H from Project tab
Worksheets(I).Range("G11:H150").Copy


' Paste values to Journal Entry tab
Worksheets("Journal Entry").Range("B" & LastRow + 1).PasteSpecial Paste:=xlValues,Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False


' Insert worksheet name in col A of Journal Entry tab
Columns(1).Insert

Cells(2) = Worksheets(I).Name

' Reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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