The code currently works as it should and seems to work dynamically as I need it to but I am pretty new to Excel so I think there is probably a better way to do some of the things I have set up. I also would like to make the code a bit prettier.
'*******************************************************************************************************************************
'*******************This finds, copies, and moves the data to the correct sheets then formats them correctly********************
'*******************************************************************************************************************************
Sub Copy_Function_Data()
Dim Target As String
Dim X As Long
Dim Y As Long
Dim Target_2 As String
Dim Last_Row3 As Long
Dim Last_Column3 As Long
Dim Total_Count As Long
Dim Title_Column As Long
Dim Country_Column As String
Dim Row_Limit1, Row_Limit2 As Long
Dim Column_Limit1 As Long
Dim Row_Limit3 As Long
Dim Current_Sheet As String
Dim r As Range
Dim r2 As Range
Application.StatusBar = "Moving Data..."
Y = ActiveWorkbook.Worksheets("Calculations").Range("B3", Worksheets("Calculations").Range("B3").End(xlDown)).Rows.Count
For X = 1 To Y
Sheets("Calculations").Select
Target = Range("B2").Offset(X, 0)
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = "TempData"
Worksheets("TempData").Range("A1").Value = Target
ActiveSheet.Range("A1").Select
Selection.Replace What:="Open Position - ", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Target_2 = Worksheets("TempData").Range("A1")
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = (Target_2)
Sheets("Raw Data").Select
Selection.AutoFilter
ActiveSheet.Range("$B$1:$Z$10000").AutoFilter Field:=5, Criteria1:= _
(Target)
Range("B1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets(Target_2).Select
Range("B50").Select
ActiveSheet.Paste
Total_Count = ActiveWorkbook.Worksheets(Target_2).Range("B50", Worksheets(Target_2).Range("B50").End(xlDown)).Rows.Count
Last_Row3 = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Last_Column3 = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Title_Column = Cells.Find(What:="Title", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Column
Country_Column = Cells.Find(What:="Country", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Column
ActiveWorkbook.Worksheets(Target_2).Range("R51", Worksheets(Target_2).Range("R51").End(xlDown)).Rows.Copy
Range("X50").Select
ActiveSheet.Paste
ActiveSheet.Range("$X$50:$X$10000").RemoveDuplicates Columns:=1, Header:= _
xlNo
ActiveWorkbook.Worksheets(Target_2).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(Target_2).Sort.SortFields.Add Key:=Range("X50") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Target_2).Sort
.SetRange Range("X50:X10000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Row_Limit1 = Worksheets(Target_2).Range("X:X").Cells.SpecialCells(xlCellTypeConstants).Count
If Row_Limit1 > 1 Then
ActiveWorkbook.Worksheets(Target_2).Range("X50", Worksheets(Target_2).Range("X50").End(xlDown)).Rows.Copy
Range("B3").Select
ActiveSheet.Paste
Else
ActiveWorkbook.Worksheets(Target_2).Range("X50", Worksheets(Target_2).Range("X50")).Copy
Range("B3").Select
ActiveSheet.Paste
End If
ActiveWorkbook.Worksheets(Target_2).Range("V51", Worksheets(Target_2).Range("V51").End(xlDown)).Rows.Copy
Range("Y50").Select
ActiveSheet.Paste
ActiveSheet.Range("$Y$50:$Y$10000").RemoveDuplicates Columns:=1, Header:= _
xlNo
ActiveWorkbook.Worksheets(Target_2).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(Target_2).Sort.SortFields.Add Key:=Range("Y20") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Target_2).Sort
.SetRange Range("Y50:Y10000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Row_Limit2 = Worksheets(Target_2).Range("Y:Y").Cells.SpecialCells(xlCellTypeConstants).Count
If Row_Limit2 > 1 Then
ActiveWorkbook.Worksheets(Target_2).Range("Y50", Worksheets(Target_2).Range("Y50").End(xlDown)).Rows.Copy
ActiveSheet.Cells(2, 3).PasteSpecial Transpose:=True
Else
ActiveWorkbook.Worksheets(Target_2).Range("Y50", Worksheets(Target_2).Range("Y50")).Copy
Range("C2").Select
ActiveSheet.Cells(2, 3).PasteSpecial Transpose:=True
End If
Application.DisplayAlerts = False
Sheets("TempData").Delete
Application.DisplayAlerts = True
Cells.Find(What:="Title", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Name = "Range1"
Set r = Range("Range1")
Cells.Find(What:="Country", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Name = "Range2"
Set r2 = Range("Range2")
Row_Limit1 = Worksheets(Target_2).Range("X:X").Cells.SpecialCells(xlCellTypeConstants).Count
Column_Limit1 = Worksheets(Target_2).Range("Y:Y").Cells.SpecialCells(xlCellTypeConstants).Count
With Worksheets(Target_2)
.Range(.Cells(3, 3), .Cells(Row_Limit1 + 2, Column_Limit1 + 2)).FormulaR1C1 = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
.Range(.Cells(Row_Limit1 + 3, 3), .Cells(Row_Limit1 + 3, Column_Limit1 + 2)).FormulaR1C1 = "=SUM(R3C:R[-1]C)"
End With
Next X
Application.StatusBar = False
End Sub