Can someone help me make this code more professional? I'm trying my best to find something similar, but I wasn't successful.
I want to avoid the repeat of the code for every single value and also I can't find a solution to avoid select. I only get it run when I repeat the code and count rng
, rng1
, rng2
etc.
Sub a()
Dim cell As Range, i As Integer, wks As Worksheet
Dim rng As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Set Bereich = Sheets("Tabelle1").Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).Row)
For Each cell In Bereich
If cell.Value = "EURUSD" Then
If Not rng Is Nothing Then
Set rng = Union(rng, Rows(cell.Row))
Else
Set rng = Rows(cell.Row)
End If
End If
Next cell
rng.EntireRow.Select
With Selection
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Sheets(ActiveSheet.Name).Name = "EURUSD"
rng.EntireRow.Copy Worksheets("EURUSD").Cells(5, 1)
End With
'--------------------------------------------------------------------------------
Sheets("Tabelle1").Select
For Each cell In Bereich
If cell.Value = "GBPUSD" Then
If Not rng1 Is Nothing Then
Set rng1 = Union(rng1, Rows(cell.Row))
Else
Set rng1 = Rows(cell.Row)
End If
End If
Next cell
rng1.EntireRow.Select
With Selection
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Sheets(ActiveSheet.Name).Name = "GBPUSD"
rng1.EntireRow.Copy Worksheets("GBPUSD").Cells(5, 1)
End With
'--------------------------------------------------------------------------------
Sheets("Tabelle1").Select
For Each cell In Bereich
If cell.Value = "AUDUSD" Then
If Not rng2 Is Nothing Then
Set rng2 = Union(rng2, Rows(cell.Row))
Else
Set rng2 = Rows(cell.Row)
End If
End If
Next cell
rng1.EntireRow.Select
With Selection
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Sheets(ActiveSheet.Name).Name = "AUDUSD"
rng2.EntireRow.Copy Worksheets("AUDUSD").Cells(5, 1)
End With
'--------------------------------------------------------------------------------
Sheets("Tabelle1").Select
For Each cell In Bereich
If cell.Value = "NZDUSD" Then
If Not rng3 Is Nothing Then
Set rng3 = Union(rng3, Rows(cell.Row))
Else
Set rng3 = Rows(cell.Row)
End If
End If
Next cell
rng1.EntireRow.Select
With Selection
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Sheets(ActiveSheet.Name).Name = "NZDUSD"
rng3.EntireRow.Copy Worksheets("NZDUSD").Cells(5, 1)
End With
End Sub