-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVbaCode
230 lines (185 loc) · 6.17 KB
/
VbaCode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Public longText As String
Sub CLeanANDSort()
'
' CLeanANDSort Macro
' Removed NULL and sorts by Year,PSC,HIghSchool,LastName,FirstName
'
Dim colName As String, MyCol As Long, logText As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = True
colName = "BannerID"
Cells.Select
' Sort
DisplayStatus (" Sorting...")
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
Call SortByName("Year")
Call SortByName("PSC")
Call SortByName("HighSchool")
Call SortByName("LastName")
Call SortByName("FirstName")
'apply Sorting now
Call SortByName("", 1)
DisplayStatus ("Done, ")
'Remove Duplicates
DisplayStatus ("Cleaning Dups...")
MyCol = Application.Match(colName, Rows(1), 0)
Range(Cells.Address).RemoveDuplicates Columns:=MyCol, Header:=xlYes
DisplayStatus ("Done")
'Replace NULLS
DisplayStatus (" , Cleaning NULLS...")
Selection.Replace What:="NULL", Replacement:="", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
DisplayStatus ("Done")
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub DisplayStatus(status As String)
longText = longText + status
Application.StatusBar = longText
End Sub
Sub SortByName(colName As String, Optional SortNow As Integer = 0)
'Checking when to apply sorting
If (colName = "") And (SortNow = 1) Then
With ActiveWorkbook.ActiveSheet.Sort
.SetRange ActiveCell.Range(Cells.Address)
.Header = xlYes
.MatchCase = True
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Exit Sub
End If
' Add keys for SORTING the columns
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:= _
ActiveWorkbook.ActiveSheet.Range("1:1").Find(What:= _
colName, MatchCase:=True), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End Sub
Sub ApplyTexas()
'
' ApplyTexas Macro
'
'
ActiveCell.Rows("1:2").EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveSheet.Cells.Select
ActiveSheet.Range("$A$1:$AN$15714").RemoveDuplicates Columns:=Array(1, 2, 11), _
Header:=xlYes
ActiveSheet.Range("$A$1:$AN$15714").RemoveDuplicates Columns:=Array(1, 2, 9), _
Header:=xlYes
ActiveSheet.Range("$A$1:$AN$15714").RemoveDuplicates Columns:=Array(1, 2, 3), _
Header:=xlYes
End Sub
Sub BigCopy()
'
' BigCopy Macro
'
'
Dim ws As Worksheet
Dim lRow As Long, lCol As Long
Dim rng As Range
Dim i As Long
i = 1
Dim count As Long
'~~> Set this to the relevant worksheet
Set ws = Sheets(1)
Application.StatusBar = ""
With ws
'~~> Get the last row and last column
lRow = .Range("A" & .Rows.count).End(xlUp).Row
lCol = .Cells(1, .Columns.count).End(xlToLeft).Column
Application.StatusBar = "Copying " & lRow & " rows..."
'loop
Application.ScreenUpdating = False
While i <= (lRow + 1000)
Set rng = .Range(.Cells(i, 1), .Cells(i + 1000, lCol))
With rng
ActiveWindow.ActivateNext
rng.Copy ActiveSheet.Cells(i, 1)
ActiveWindow.ActivateNext
End With
i = i + 1000
Wend
'~~> Set the range
End With
Application.ScreenUpdating = True
Application.StatusBar = "Copied " & lRow & " rows!!"
End Sub
Sub sheetToSqlSingleColumn()
'
' sheetToSqlSingleColumn Macro
'
'
Dim colNum As Integer
Dim lastRow As Long
'Windows("201531 Financial Aid Data.xlsx").Activate
Range("A3").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.ClearContents
ActiveWindow.ActivateNext
'Windows("2015 Applicants Financial Aid Data Report_20150827_085425.csv"). _
Activate
Range("A1").Select
ActiveSheet.Cells.RemoveDuplicates Columns:=2, Header:= _
xlYes
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
'Windows("201531 Financial Aid Data.xlsx").Activate
ActiveWindow.ActivateNext
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.SpecialCells(xlLastCell).Select
colNum = Application.Match("*SQL*", Range("1:1"), 0)
'Range(Selection, ActiveCell.Offset(columnOffset:=-2)).Select
lastRow = ActiveSheet.UsedRange.Rows.count
Range(Cells(2, colNum), Cells(lastRow, colNum + 2)).Select
'Selection.Offset(rowOffset:=1).Select
Application.CutCopyMode = False
Selection.FillDown
Rows("2:2").Select
Selection.Delete Shift:=xlUp
ActiveCell.SpecialCells(xlLastCell).Select
Range(Cells(2, colNum), Cells(lastRow, colNum)).Select
Selection.Copy
End Sub
Sub sheetToSqlMultipleColumn()
'
' for more than one column Macro
'
'
Dim colNum As Integer
Dim lastRow As Long
'Windows("201531 Financial Aid Data.xlsx").Activate
Range("A3").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.ClearContents
ActiveWindow.ActivateNext
'Windows("2015 Applicants Financial Aid Data Report_20150827_085425.csv"). _
Activate
Range("A1").Select
ActiveSheet.Cells.RemoveDuplicates Columns:=2, Header:= _
xlYes
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
'Windows("201531 Financial Aid Data.xlsx").Activate
ActiveWindow.ActivateNext
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.SpecialCells(xlLastCell).Select
colNum = Application.Match("*SQL*", Range("1:1"), 0)
'Range(Selection, ActiveCell.Offset(columnOffset:=-2)).Select
lastRow = ActiveSheet.UsedRange.Rows.count
Range(Cells(2, colNum), Cells(lastRow, colNum + 2)).Select
'Selection.Offset(rowOffset:=1).Select
Application.CutCopyMode = False
Selection.FillDown
Rows("2:2").Select
Selection.Delete Shift:=xlUp
ActiveCell.SpecialCells(xlLastCell).Select
Range(Cells(2, colNum), Cells(lastRow, colNum)).Select
Selection.Copy
End Sub