radsok8199
New Member
- Joined
- Dec 4, 2020
- Messages
- 24
- Office Version
- 2016
- Platform
- Windows
- MacOS
- Mobile
Dear VVA Masters
I have below code to find columns based on headers and paste them into new created sheet.
First rows are merged. A1:B1, And C1 : to the end of values.
At first this code will copy entire column A and B and paste into new sheet. I t works fine
Second it should find columns with headers from Array and paste into new worksheet right after columns A & B. It does find columns and paste them into new worksheet keeping all formats, however it does paste copied columns into new sheet but different range. It paste into A1 overwriting previously pasted here columns. Not sure why it does not work ?
I also tried as below
Below screen from sheets
I have below code to find columns based on headers and paste them into new created sheet.
First rows are merged. A1:B1, And C1 : to the end of values.
At first this code will copy entire column A and B and paste into new sheet. I t works fine
Second it should find columns with headers from Array and paste into new worksheet right after columns A & B. It does find columns and paste them into new worksheet keeping all formats, however it does paste copied columns into new sheet but different range. It paste into A1 overwriting previously pasted here columns. Not sure why it does not work ?
VBA Code:
Sub GetColumnsByHeader()
Application.ScreenUpdating = False
Dim newSht As Worksheet, ws As Worksheet, Hdrs As Variant, i As Long, Fnd As Range, lcolS As Long, wb As Workbook, rng As Range
Dim lr As Long, lrnewSht As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("SM mode")
lcolS = ws.Cells(10, 3).End(xlToRight).Column
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set newSht = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
newSht.Name = "Admin 1_6"
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(lr, lcolS))
ws.Range(ws.Cells(1, "A"), ws.Cells(lr, "B")).Copy
With newSht.Range("A1")
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End With
'lrnewSht = newSht.Cells(10, 1).End(xlToRight).Column
Hdrs = Array("Admin 1", "Admin 2", "Admin 3", "Admin 4", "Admin 5", "Admin 6")
With ws.Range(ws.Cells(2, 3), ws.Cells(2, lcolS))
For i = LBound(Hdrs) To UBound(Hdrs)
Set Fnd = .Find(Hdrs(i), lookat:=xlWhole)
If Not Fnd Is Nothing Then
Intersect(Fnd.EntireColumn, rng).Copy
newSht.Cells(1, 2+ i).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
newSht.Cells(1, 2+ i).PasteSpecial Paste:=xlPasteColumnWidths
newSht.Cells(1, 2+ i).PasteSpecial Paste:=xlPasteFormats
End If
Next i
Application.CutCopyMode = False
End With
Application.ScreenUpdating = True
End Sub
I also tried as below
VBA Code:
Sub GetColumnsByHeader()
Application.ScreenUpdating = False
Dim newSht As Worksheet, ws As Worksheet, Hdrs As Variant, i As Long, Fnd As Range, lcolS As Long, wb As Workbook, rng As Range
Dim lr As Long, lrnewSht As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("SM mode")
lcolS = ws.Cells(10, 3).End(xlToRight).Column
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set newSht = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
newSht.Name = "Admin 1_6"
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(lr, lcolS))
ws.Range(ws.Cells(1, "A"), ws.Cells(lr, "B")).Copy
With newSht.Range("A1")
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End With
lrnewSht = newSht.Cells(10, 1).End(xlToRight).Column
Hdrs = Array("Admin 1", "Admin 2", "Admin 3", "Admin 4", "Admin 5", "Admin 6")
With ws.Range(ws.Cells(2, 3), ws.Cells(2, lcolS))
For i = LBound(Hdrs) To UBound(Hdrs)
Set Fnd = .Find(Hdrs(i), lookat:=xlWhole)
If Not Fnd Is Nothing Then
Intersect(Fnd.EntireColumn, rng).Copy
newSht.Cells(1, lrnewSht + i).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
newSht.Cells(1, lrnewSht + i).PasteSpecial Paste:=xlPasteColumnWidths
newSht.Cells(1, lrnewSht + i).PasteSpecial Paste:=xlPasteFormats
End If
Next i
Application.CutCopyMode = False
End With
Application.ScreenUpdating = True
End Sub
Below screen from sheets
Last edited: