forked from AllenMattson/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastThings.vb
160 lines (127 loc) · 5.34 KB
/
LastThings.vb
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
Option Explicit
Option Private Module
'locate last column
'locate last row
'last things
Function lastCol(wsName As String, Optional rowToCheck As Long = 1) As Long
Dim ws As Worksheet
Set ws = Worksheets(wsName)
lastCol = ws.Cells(rowToCheck, ws.Columns.Count).End(xlToLeft).Column
End Function
Function lastRow(wsName As String, Optional columnToCheck As Long = 1) As Long
Dim ws As Worksheet
Set ws = Worksheets(wsName)
lastRow = ws.Cells(ws.Rows.Count, columnToCheck).End(xlUp).Row
End Function
Public Function LastUsedColumn() As Long
Dim rLastCell As Range
Set rLastCell = ActiveSheet.Cells.Find(What:="*", _
After:=ActiveSheet.Cells(1, 1), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
LastUsedColumn = rLastCell.Column
End Function
Public Function LastUsedRow() As Long
Dim rLastCell As Range
Set rLastCell = ActiveSheet.Cells.Find(What:="*", _
After:=ActiveSheet.Cells(1, 1), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
LastUsedRow = rLastCell.Row
End Function
'---------------------------------------------------------------------------------------
' Procedure : fnLngLocateValueRow
' Author : v.doynov
' Date : 16.03.2017
' Purpose : blnLookUpToBottom is more powerful than lngMoreValuesFound and makes it useless.
'---------------------------------------------------------------------------------------
'
Public Function fnLngLocateValueRow(ByVal strTarget As String, _
ByRef wksTarget As Worksheet, _
Optional lngCol As Long = 1, _
Optional lngMoreValuesFound As Long = 1, _
Optional blnLookForPart = False, _
Optional blnLookUpToBottom = True) As Long
Dim lngValuesFound As Long
Dim rngLocal As Range
Dim rngMyCell As Range
fnLngLocateValueRow = -999
lngValuesFound = lngMoreValuesFound
Set rngLocal = wksTarget.Range(wksTarget.Cells(1, lngCol), wksTarget.Cells(Rows.Count, lngCol))
For Each rngMyCell In rngLocal
If blnLookForPart Then
If strTarget = Left(rngMyCell, Len(strTarget)) Then
If lngValuesFound = 1 Then
fnLngLocateValueRow = rngMyCell.row
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
Else
If strTarget = Trim(rngMyCell) Then
If lngValuesFound = 1 Then
fnLngLocateValueRow = rngMyCell.row
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
End If
Next rngMyCell
End Function
'---------------------------------------------------------------------------------------
' Procedure : fnLngLocateValueCol
' Author : v.doynov
' Date : 16.03.2017
' Purpose : blnLookUpToBottom is more powerful than lngMoreValuesFound and makes it useless.
'---------------------------------------------------------------------------------------
'
Public Function fnLngLocateValueCol(ByVal strTarget As String, _
ByRef wksTarget As Worksheet, _
Optional lngRow As Long = 1, _
Optional lngMoreValuesFound As Long = 1, _
Optional blnLookForPart = False, _
Optional blnLookUpToBottom = True) As Long
Dim lngValuesFound As Long
Dim rngLocal As Range
Dim rngMyCell As Range
fnLngLocateValueCol = -999
lngValuesFound = lngMoreValuesFound
Set rngLocal = wksTarget.Range(wksTarget.Cells(lngRow, 1), wksTarget.Cells(lngRow, Columns.Count))
For Each rngMyCell In rngLocal
If blnLookForPart Then
If strTarget = Left(rngMyCell, Len(strTarget)) Then
If lngValuesFound = 1 Then
fnLngLocateValueCol = rngMyCell.Column
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
Else
If strTarget = Trim(rngMyCell) Then
If lngValuesFound = 1 Then
fnLngLocateValueCol = rngMyCell.Column
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
End If
Next rngMyCell
End Function
'LastRow Last Row Formula
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)
'LastColumn Last Column Formula
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(1:1))),COLUMN(1:1)),0)
'Last Row Value of Column A
=LOOKUP(2,1/(NOT(ISBLANK(A:A))),A:A)
'Last Column Value of the first row
=LOOKUP(2,1/(NOT(ISBLANK(1:1))),1:1)