Reputation: 105
I want to use vba code in order to choose each value in the table orderly and copy them into new sheet. As it is shown in the picture there is a table and in the column F we have 2 different values(it might be more than 2). What I need is when I run the macro it will select first value then will copy the table into new sheet (name of the sheet will be based on the value in the column F-for example 0.55) then will come back and select second value and do the same. we might have more than 6-7 values, so I don't know how to make a loop to do it automatically for all values. I need to add this process at the end of that code block. Becaue that codes doing something different and at the result I get that table.
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim x As Workbook
Dim y As Workbook
Dim q As Workbook
'## Open all workbooks first:
Set x = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\barkod.xlsx")
Set y = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\csv.csv")
Set q = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\campaign.xlsx")
'## Clear the workbook first:
Windows("csv.csv").Activate
y.Sheets("csv").Range("A:M").Clear
'## Insert the column in the barkod file:
Windows("barkod.xlsx").Activate
x.Sheets("barkod").Range("F1").EntireColumn.Insert
'## Insert the column header in the barkod file:
x.Sheets("barkod").Range("E1").Offset(0, 1).Value = "Discounts"
'## make the vlookup in barkod file:
With x.Sheets("barkod").Range("F2")
.FormulaR1C1 = "=VLOOKUP(RC[-1], [campaign.xlsx]Sheet1!C[-5]:C[-4], 2, 0)"
.AutoFill Destination:=.Resize(WorksheetFunction.CountA(.Offset(, -1).EntireColumn))
End With
'## deselect the #N/A:
Windows("barkod.xlsx").Activate
x.Sheets("barkod").Range(Sheets("barkod").Range("A1:F1"), Sheets("barkod").Range("A1:F1").End(xlDown)).Select
Selection.AutoFilter
ActiveSheet.Range("A:F").AutoFilter field:=6, Criteria1:="<>#N/A"
'Now, copy what you want from x:
x.Sheets("barkod").Range(Sheets("barkod").Range("A1:F1"), Sheets("barkod").Range("A1:F1").End(xlDown)).Copy
'Now, paste to y worksheet:
y.Sheets("csv").Range("A1").PasteSpecial
Upvotes: 0
Views: 441
Reputation: 23081
This procedure should do what you want so you could give it a meaningful name and call it at the end of your existing code. It creates a list of unique items in F using advanced filter and then loops through each using AutoFilter to create the new sheet.
Sub Macro2()
Dim r As Range, r2 As Range, ws As Worksheet
Application.DisplayAlerts = False
With Sheets("Sheet1") 'change to suit
Sheets.Add().Name = "Temp"
.Range("F1", .Range("F" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Temp").Range("A1"), Unique:=True
Set r2 = Sheets("Temp").Range("A2", Sheets("Temp").Range("A2").End(xlDown))
For Each r In r2
.Range("A1").CurrentRegion.AutoFilter field:=6, Criteria1:=r
Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
.AutoFilter.Range.Copy ws.Range("A1")
ws.Name = r
.Range("A1").CurrentRegion.AutoFilter field:=6
Next r
Sheets("Temp").Delete
.AutoFilterMode = False
End With
Application.DisplayAlerts = True
End Sub
Upvotes: 2