Excel to copy column headings to new sheet

mwillerton

New Member
Joined
Jul 18, 2011
Messages
15
Hi all
I have used some VBA code in an excel spreadsheet 2016.
It looks at a column in a spreadsheet and copies all rows in that sheet where the word in the column matches (Mon) and creates and names a new sheet.
I have then repeated this for each day (Tue, Wed, Thur, Fri etc.)

There are two issues i am having:

1. Copy the column headings to appear in the newly created sheet from the original sheet
2. get the code to pull out all the MON, check a different column for a value (1, 2, 3, 4, 5, 6, reg, reg2) and place those in individual named sheets (MON1, MON2, MONreg etc.)

My code so far is below:
I am not an expert at this VBA

Thanks in advance
Matt


Sub MON()
Const strTest = "Mon"
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim rngCells As Range
Dim rngFind As Range

Set wsSource = ActiveSheet

NoRows = wsSource.Range("A65536").End(xlUp).Row
DestNoRows = 2
Set wsDest = ActiveWorkbook.Worksheets.Add
wsDest.Name = "MON"

For I = 1 To NoRows

Set rngCells = wsSource.Range("O" & I & ":F" & I)

If Not (rngCells.Find(strTest) Is Nothing) Then
rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)

DestNoRows = DestNoRows + 1
End If
Next I
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
For the first part, assuming the headers are in row 1 on the source sheet.
VBA Code:
Set wsDest = ActiveWorkbook.Worksheets.Add
wsDest.Name = "MON" 

wsSource.Rows(1).Copy wsDest.Rows(1)
 
Upvote 0
You mean in this range of columns: "F:O" the word "MON" may exist, for example if the word "MON" exists in column "F" then create the sheet "MON1". If "MON" exists in column "G" create sheet "MON2" and so on?
 
Upvote 0
Matt

Which column do you want to check for 1,2,3 etc.?
 
Upvote 0
You mean in this range of columns: "F:O" the word "MON" may exist, for example if the word "MON" exists in column "F" then create the sheet "MON1". If "MON" exists in column "G" create sheet "MON2" and so on?
If the word MON exists in column O AND if 1 exists in column F it creates a new sheet called MON1 and copies all rows containing the MON AND the 1.
Then i will just edit it to create another for MON2 and so on
1579100976825.png

1579101003085.png
 
Upvote 0
For the first part, assuming the headers are in row 1 on the source sheet.
VBA Code:
Set wsDest = ActiveWorkbook.Worksheets.Add
wsDest.Name = "MON"

wsSource.Rows(1).Copy wsDest.Rows(1)
Brilliant, that works. Many thanks. :)
 
Upvote 0
This is better, sorry didn't read all the rules. Now just the MON1 issue.
Thanks in advance again

VBA Code:
Sub MON()
Const strTest = "Mon"
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim rngCells As Range
Dim rngFind As Range

Set wsSource = ActiveSheet

NoRows = wsSource.Range("A65536").End(xlUp).Row
DestNoRows = 2
Set wsDest = ActiveWorkbook.Worksheets.Add
wsDest.Name = "MON"
wsSource.Rows(1).Copy wsDest.Rows(1)

For I = 1 To NoRows

Set rngCells = wsSource.Range("O" & I & ":F" & I)

If Not (rngCells.Find(strTest) Is Nothing) Then
rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)

DestNoRows = DestNoRows + 1
End If
Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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