shwetankbhardwaj
New Member
- Joined
- Apr 26, 2017
- Messages
- 20
Hello there,
Hope you are creating magic in excel every day.
Below are the codes I have got, which creates a new worksheet (consolidate) and get data into the sheet from other worksheets.
It only fetches filtered rows and all the columns.
What I need:-
I also want to add columns header in the 'consolidate' sheet. Which currently has no data. I want A1, B1, C1 and D1 as a column names.
As of now it is fetching all the columns from all the worksheets but I only need columns A, B, C and F from all worksheets.
Can someone please help me add codes (only for two tasks mentioned above) with the coding pasted above?
Thanks in advance!
Shwetank Bhardwaj
Hope you are creating magic in excel every day.
Below are the codes I have got, which creates a new worksheet (consolidate) and get data into the sheet from other worksheets.
It only fetches filtered rows and all the columns.
Code:
Sub copyfrmworksheet()
Dim wrk As Workbook
Dim sht As Worksheet
Dim trg As Worksheet
Dim cons_data As Worksheet
Dim rng As Range
Dim lastnonblank As Long
Dim wslastnonblank As Long
Dim colcount As Integer
Set wrk = ActiveWorkbook
'Set cons_data = ActiveWorkbook.Sheets("Consolidate_Data")
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
trg.Name = "Consolidate"
Set sht = wrk.Worksheets(1)
colcount = sht.Cells(1, 255).End(xlToLeft).Column
For Each sht In wrk.Worksheets
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colcount))
lastnonblank = trg.Range("A:A").End(xlUp).Rows + 1
sht.Activate
rng.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
trg.Activate
trg.Range("A65536").End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).PasteSpecial xlPasteValues
Next sht
trg.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
I also want to add columns header in the 'consolidate' sheet. Which currently has no data. I want A1, B1, C1 and D1 as a column names.
As of now it is fetching all the columns from all the worksheets but I only need columns A, B, C and F from all worksheets.
Can someone please help me add codes (only for two tasks mentioned above) with the coding pasted above?
Thanks in advance!
Shwetank Bhardwaj
Last edited by a moderator: