I have a VBA program that needs to copy and paste values depending on what their sheet names are.
The sheet names will be extracted on a specific column (Bin column) and will be the name basis of the added worksheets. Is there a way that I can copy the values and paste them according to their Bin values?
For example, my bin values are QWE, RTY, UIO
where they are duplicated on the original sheet that's why I created a list. Then after creating the list, Sub CreateSheets()
will create worksheet QWE
, worksheet RTY
and worksheet UIO
. My problem is how can I paste the values depending on the worksheet names and set number.
sample data sheet
This is the code where I extract and create a list (removing the duplicate values) of the bins
Sub BIN_Values_List()
Dim rSelection As Range
Dim ws As Worksheet
Range("F3:F" & Range("F" & Rows.Count).End(xlUp).Row).Select
Set rSelection = Selection
Set ws = Worksheets.Add
ws.Name = "BIN LIST"
rSelection.Copy
With ws.Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteValues
End With
ws.UsedRange.RemoveDuplicates Columns:=Array(1), Header:=xlGuess
On Error Resume Next
ws.UsedRange.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
On Error GoTo 0
ws.Columns("A").AutoFit
Application.CutCopyMode = False
End Sub
After creating the list, this code will create and rename the sheets based on the bin list together with the result template.
Sub CreateSheets()
lastcell = ThisWorkbook.Worksheets("BIN LIST").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastcell
With ThisWorkbook
newname = ThisWorkbook.Worksheets("BIN LIST").Cells(i, 1).Value
.Sheets.Add after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = newname
Sheets("RESULT TEMPLATE").Range("A1:E205").Copy
ActiveSheet.Paste
End With
Next
ThisWorkbook.Worksheets("SHEET1").Activate
ThisWorkbook.Worksheets("SHEET1").Cells(1, 1).Select
End Sub