Sub GenerateDataToCSV()
Dim ws As Worksheet
Dim i As Long
Dim StudentCSVPath As String
Dim FacultyCSVPath As String
' File paths for CSV files
StudentCSVPath = ThisWorkbook.Path & "\StudentData.csv"
FacultyCSVPath = ThisWorkbook.Path & "\FacultyData.csv"
Application.ScreenUpdating = False
' Generate StudentData worksheet
Set ws = Worksheets.Add
ws.Name = "StudentData"
With ws
.Cells(1, 1) = "StudentID"
.Cells(1, 2) = "IsImproved"
.Cells(1, 3) = "GPA"
.Cells(1, 4) = "Retained"
.Cells(1, 5) = "SatisfactionScore"
.Cells(1, 6) = "AdminCost"
.Cells(1, 7) = "ServicesUsed"
End With
For i = 2 To 1001
With ws
.Cells(i, 1) = i - 1
.Cells(i, 2) = IIf(Rnd() < 0.5, "Yes", "No")
If .Cells(i, 2) = "Yes" Then
.Cells(i, 3) = Round(2.8 + Rnd() * 0.8, 2)
.Cells(i, 4) = IIf(Rnd() < 0.85, "Yes", "No")
.Cells(i, 5) = Round(3.7 + Rnd() * 1, 1)
.Cells(i, 6) = Round(7500 + Rnd() * 1000, 0)
.Cells(i, 7) = Int(3 + Rnd() * 4)
Else
.Cells(i, 3) = Round(2.4 + Rnd() * 0.8, 2)
.Cells(i, 4) = IIf(Rnd() < 0.75, "Yes", "No")
.Cells(i, 5) = Round(3 + Rnd() * 1, 1)
.Cells(i, 6) = Round(9500 + Rnd() * 1000, 0)
.Cells(i, 7) = Int(1 + Rnd() * 3)
End If
End With
Next i
' Export StudentData to CSV
ws.Copy
ActiveWorkbook.SaveAs Filename:=StudentCSVPath, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
' Generate FacultyData worksheet
Set ws = Worksheets.Add
ws.Name = "FacultyData"
With ws
.Cells(1, 1) = "FacultyID"
.Cells(1, 2) = "IsImproved"
.Cells(1, 3) = "Publications"
.Cells(1, 4) = "Citations"
.Cells(1, 5) = "GrantFunding"
End With
For i = 2 To 101
With ws
.Cells(i, 1) = i - 1
.Cells(i, 2) = IIf(Rnd() < 0.5, "Yes", "No")
If .Cells(i, 2) = "Yes" Then
.Cells(i, 3) = Int(2 + Rnd() * 2)
.Cells(i, 4) = Int(.Cells(i, 3).Value * (10 + Rnd() * 10))
.Cells(i, 5) = Round(600000 + Rnd() * 300000, -3)
Else
.Cells(i, 3) = Int(1 + Rnd() * 2)
.Cells(i, 4) = Int(.Cells(i, 3).Value * (5 + Rnd() * 10))
.Cells(i, 5) = Round(400000 + Rnd() * 200000, -3)
End If
End With
Next i
' Export FacultyData to CSV
ws.Copy
ActiveWorkbook.SaveAs Filename:=FacultyCSVPath, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "Data generation and export to CSV complete! Files are saved at: " & vbCrLf & _
StudentCSVPath & vbCrLf & FacultyCSVPath
End Sub
Workbooks.Addto create another workbook. How would I activate now to make sure im importing to it ... is something likeNewWorkbook.Sheets(1).Activate?