Excell VBA For Each Loop and Add Array cell adress - arrays

Can u help me please.
I have a one problem.
My VBA Code
Sub ercan()
Dim ws As Worksheet
Set ws = Worksheets("Sayfa1")
For Each gunadi In ws.Range("D1:AH1")
If gunadi.Text = "CUMARTESİ" Then 'CUMARTESİ DAY NAME = Saturday
gunadi.Interior.Color = RGB(146, 208, 80)
gunadi.Offset(1, 0).Interior.Color = RGB(146, 208, 80)
ElseIf gunadi.Text = "PAZAR" Then 'PAZAR DAY NAME = Sunday
gunadi.Interior.Color = RGB(0, 176, 80)
gunadi.Offset(1, 0).Interior.Color = RGB(0, 176, 80)
Else
gunadi.Interior.ColorIndex = 0
gunadi.Offset(1, 0).Interior.ColorIndex = 0
End If
Next
For Each haftaici In ws.Range("D4:AH4")
If haftaici.Offset(-3, 0).Text = "PAZAR" Then
MsgBox haftaici.Address(False, False)
addr = haftaici.Address(False, False)
End If
'afterrrrr do
ws.Range("AK4").Value = "=" & addr
Next
End Sub
enter image description here
I want select range -3 == pazar (Sunday) add array cell address and after used this string.
H4 O4 V4 AC4 <= msgbox . I need add array foreach loop
I WANT =H4+O4+V4+AC4 THIS FORMAT DEBUG
I want to collect Sundays in "AK4" cell but value type =cell+cell+cell
how can i do i need your help?

Try this: you only need one loop.
Sub ercan()
Dim ws As Worksheet, c As Range, addr, sep
Set ws = Worksheets("Sayfa1")
For Each c In ws.Range("D1:AH1").Cells
Select Case c.Text
Case "CUMARTESI" 'Sat
c.Interior.Color = RGB(146, 208, 80)
c.Offset(1, 0).Interior.Color = RGB(146, 208, 80)
Case "PAZAR" 'Sun
c.Interior.Color = RGB(0, 176, 80)
c.Offset(1, 0).Interior.Color = RGB(0, 176, 80)
addr = addr & sep & c.Offset(3).Address(False, False) 'address from row 4
sep = "," 'add comma after first sunday
Case Else
c.Interior.ColorIndex = 0
c.Offset(1, 0).Interior.ColorIndex = 0
End Select
Next
If Len(addr) > 0 Then 'any Sundays?
ws.Range("AK4").Formula = "=SUM(" & addr & ")"
End If
End Sub

Related

VBA how to devide from a single range to regular ranges with intervals between each range

I appreciate you in advance.
I wrote this code below.
And I want change the single range from Set rng = Application.Intersect(Target, Me.Range("M31:AM53"))
to 42 regular ranges with intervals between each ranges.
42 ranges should be like this,
Sub WriteNumber_v4()
Dim rng As Range
Dim i, j As Integer
For i = 1 To 6
For j = 1 To 7
Set rng = Range("M31:O33").Offset((i - 1) * 4, (j - 1) * 4) 'change 3 to 4
rng.Value = 1
Next j
Next i
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim trlRed As Long, aaaPhoneBlue As Long, bbbGreen As Long, cccGrey As Long, dddPurple As Long
Dim rng As Range, cell As Range
trlRed = RGB(230, 37, 30)
aaaPhoneBlue = RGB(126, 199, 216)
bbbGreen = RGB(61, 220, 132)
cccGrey = RGB(162, 170, 173)
dddPurple = RGB(165, 154, 202)
'firstLvValFor = Array("TRIAL", "BASIC", "NOVICE", "INTERMEDIATE", "ADVANCED")
secondLvValFor = Array("aaa", "bbb", "ccc", "ddd")
thirdLvValFor_01 = Array("Beginner", "Text", "camera")
thirLvValFor_02 = Array("Security", "SomeSnsApps_01", "SomeSnsApps_02")
Set rng = Application.Intersect(Target, Me.Range("M31:AM53"))
If Not rng Is Nothing Then 'only loop though any cells in M31:AM53
For Each cell In rng.Cells
If cell.Value = "Session" And cell.Offset(0, -2).Value = "TRIAL" Then
cell.Offset(0, -2).Resize(1, 3).Interior.Color = trlRed
ElseIf IsError(Application.Match(cell.Value, thirdLvValFor_01, 0)) = False And cell.Offset(0, -1).Value = "aaa" And cell.Offset(0, -2).Value <> "TRIAL" Then
cell.Offset(0, -2).Resize(1, 3).Interior.Color = aaaPhoneBlue
ElseIf cell.Value = "aaa" And IsError(Application.Match(cell.Offset(0, 1).Value, thirdLvValFor_01, 0)) = False And cell.Offset(0, -1).Value <> "TRIAL" Then
cell.Offset(0, -1).Resize(1, 3).Interior.Color = aaaPhoneBlue
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
Next cell
End If
End Sub
I simply put "Sub WriteNumber_v4()" into the code, however
some loop error has been occured. The error was "Next without For" thing.
Any advice would be appreciated.

Hide rows based on length of longest column vba

I have a set of 7 non-consecutive columns to cycle through and I want to hide all the rows after the end of the longest column (cells that contain no value).
For Each Y In Array(4, 10, 16, 22, 28, 34, 40)
For X = 16 To 65
If wks.Cells(X, Y).Value = "" Then
wks.Cells(X, Y).EntireRow.Hidden = True
Else: wks.Cells(X, Y).EntireRow.Hidden = False
End If
Next X
Next Y
This would cover it except for the fact that the columns are different lengths and by the time the macro is finished, some rows that contain data have been hidden. I know what I have isn't quite correct, I'm just not sure how to tweak it.
Loop the columns and use Find to find the last with a value.
Sub test()
Dim wks As Worksheet
Set wks = ActiveSheet
wks.Rows("16:65").Hidden = False
Dim y
Dim j As Range
Dim currentmax As Long
For Each y In Array(4, 10, 16, 22, 28, 34, 40)
Set j = wks.Range(wks.Cells(16, y), wks.Cells(65, y)).Find("*", wks.Cells(16, y), , , xlByRows, xlPrevious)
If Not j Is Nothing Then
If j.Row > currentmax Then currentmax = j.Row
End If
Next y
If currentmax > 16 Then
wks.Rows(currentmax + 1 & ":65").Hidden = True
End If
End Sub
Sub TestMe()
Dim wks As Worksheet
Set wks = Worksheets(1)
Dim myCol As Variant
Dim myRow As Long
Dim recordRow As Long : recordRow = 65
For Each myCol In Array(4, 10, 16, 22, 28, 34, 40)
For myRow = 16 To 65
If wks.Cells(myRow, myCol).Value = "" Then
If recordRow < myRow Then recordRow = myRow
Exit For
End If
Next myRow
Next myCol
If myRow < 65 Then wks.Rows("65:" & myRow).EntireRow.Hidden = True
End Sub
Introduce a variable recordRow, which keeps the lowest row of the columns;
Once the variable makes the check, the first for loop exits;
At the end you hide the EntireRow, starting from 16 and ending with the recordRow;
If myRow < 16 is needed to make sure that myRow has some values assigned;

IsInArray not returning True when it should

I am using the IsInArray function to check if a cell address (Row,Column) exists within an array. For some reason, even though the array contains the value it doesn't match. For example if my array is like this:
18, 812, 84, 34, 412, 87, 74
And OldRow & OldCol gets to 34, the function IsInArray doesn't match it and execute Else.
Below is a sample of the code I am using to try and achieve this:
Set c = .Find(1, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
cellAddress = c.Address
OldRow = Range(cellAddress).Row
OldCol = Range(cellAddress).Column
If IsInArray("OldRow & OldCol", mappedcells) = False Then
oldmappingrow = Application.Match(OldRow, Worksheets(1).Range("r3:r16"), 0)
If Not IsError(oldmappingrow) Then
OldRowMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingrow).Offset(, 1).Value
End If
oldmappingcol = Application.Match(OldCol, Worksheets(1).Range("r3:r16"), 0)
If Not IsError(oldmappingcol) Then
OldColMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingcol).Offset(, 1).Value
End If
If OldCol > OldRow Then
NewCol = WorksheetFunction.Max(OldRowMapped, OldColMapped)
NewRow = WorksheetFunction.Min(OldRowMapped, OldColMapped)
Else
NewRow = WorksheetFunction.Max(OldRowMapped, OldColMapped)
NewCol = WorksheetFunction.Min(OldRowMapped, OldColMapped)
End If
.Cells(NewRow, NewCol) = .Cells(OldRow, OldCol).Value
.Cells(OldRow, OldCol).Value = "0"
ReDim Preserve mappedcells(UBound(mappedcells) + 1) 'Add next array element
mappedcells(UBound(mappedcells)) = NewRow & NewCol 'Assign the array element
Set c = .FindNext(c)
Debug.Print (OldRow & OldCol & " moved to " & NewRow & NewCol)
Else
Set c = .FindNext(c)
End If
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
And the IsInArray function I am using:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Sorry if the code is messy and could be written cleaner, I am totally new to vba and programming as a whole. Any help would be much appreciated!
"3" & "4" <> 34 but "3" & "4" = "34" and Int("3" & "4") = 34. Try it as,
If not IsInArray(clng(OldRow & OldCol), mappedcells) Then
As noted in the comment from Scott Craner below, go through your code and make sure you are comparing numbers to numbers or text to text and not numbers to text-that-looks-like-a-number.

VBA Manually entered array - Zoidberg

I have VBA code that formats a range of cells, reads in a text file, converts the text in each cell to a color and in effect draws a picture of Zoidberg.
The problem I am having is the text file... I want to include the contents of the text file in the VBA as a manually entered array. That way I do not have to have the text file. This is strictly a memory burner.
The text file is the same thing as an array (in theory), so I should be able to define the array and do all the same things I am doing with the text file... but I'm missing something how do set up the array...
Sub Why_Not_Zoidberg()
' Insert Why Not Zoidberg?
'
'MAKE STUFF HAPPEN IN THE BACKGROUND
Application.ScreenUpdating = False
ActiveWindow.DisplayGridlines = False
'DEFINE VARIABLES
Dim Rng1 As Range
Dim i As Long
Dim j As Long
Dim nName As Name
'SET RANGE
Set Rng1 = Range("A1:BN48")
'CLEAR OUT ANY EXISTING JUNK
With Rng1
.Interior.ColorIndex = 0
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
ActiveSheet.Range("A1").Select
'IMPORT TEXT FILE
'With ActiveSheet.QueryTables.Add(Connection:= _
' "TEXT;C:\Windows\Zoidberg.txt", Destination:=Rng1 _
' )
' .Name = "Sample"
' .FieldNames = True
' .RowNumbers = False
' .FillAdjacentFormulas = False
' .PreserveFormatting = True
' .RefreshOnFileOpen = False
' .RefreshStyle = xlInsertDeleteCells
' .SavePassword = False
' .SaveData = True
' .AdjustColumnWidth = True
' .RefreshPeriod = 0
' .TextFilePromptOnRefresh = False
' .TextFilePlatform = 437
' .TextFileStartRow = 1
' .TextFileParseType = xlDelimited
' .TextFileTextQualifier = xlTextQualifierDoubleQuote
' .TextFileConsecutiveDelimiter = False
' .TextFileTabDelimiter = True
' .TextFileSemicolonDelimiter = False
' .TextFileCommaDelimiter = True
' .TextFileSpaceDelimiter = False
' .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
' .TextFileTrailingMinusNumbers = False
' .Refresh BackgroundQuery:=False
'End With
' ADD COLORS
For i = 1 To Rng1.Rows.Count
For j = 1 To Rng1.Columns.Count
If Rng1.Cells(i, j).Value = 3 Then
Rng1.Cells(i, j).Interior.Color = RGB(253, 3, 74)
End If
If Rng1.Cells(i, j).Value = -4142 Then
Rng1.Cells(i, j).Interior.Color = RGB(255, 255, 255)
End If
If Rng1.Cells(i, j).Value = 1 Then
Rng1.Cells(i, j).Interior.Color = RGB(1, 1, 1)
End If
If Rng1.Cells(i, j).Value = 14 Then
Rng1.Cells(i, j).Interior.Color = RGB(0, 153, 153)
End If
Next j
Next i
'CLEAR OUT TEXT FILE STUFF
Rng1.Select
With Rng1
.ClearContents
End With
Rng1.Select
'SET CELL SIZE
Selection.ColumnWidth = 2
Selection.RowHeight = 14.25
ActiveSheet.Range("A1").Select
'BREAK LINK TO EXTERNAL TEXT FILE
For Each nName In ActiveWorkbook.Names
If Left(nName.Name, 12) <> "" Then nName.Delete
Next nName
Application.ScreenUpdating = True
End Sub
Zoidberg.txt
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,3,3,3,3,3,3,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,1,3,3,3,3,3,3,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,1,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,1,3,3,3,3,3,3,3,1,1,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,1,3,3,1,1,1,1,1,1,-4142,-4142,1,1,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,1,3,1,1,1,-4142,-4142,1,1,1,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,1,1,-4142,1,1,-4142,-4142,1,3,3,1,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,1,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,1,1,1,1,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,1,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,1,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,1,3,3,1,3,3,1,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,1,3,3,3,1,3,1,3,3,1,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,1,1,1,3,3,1,3,3,1,3,3,1,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,1,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,1,1,1,3,3,1,3,3,1,3,3,1,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,1,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,1,3,3,3,1,3,1,3,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,1,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,1,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,3,3,3,3,3,3,3,3,3,3,3,1,3,3,1,3,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,1,-4142,-4142,1,3,3,3,3,3,3,3,1,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,1,3,3,3,3,3,3,3,3,3,3,3,1,3,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,1,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,1,1,1,3,3,3,3,3,3,3,3,3,3,1,3,3,3,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,1,14,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,14,1,-4142,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,1,14,14,14,14,14,14,14,14,14,14,14,1,14,14,14,14,1,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,14,14,14,14,14,14,14,14,14,14,1,14,14,14,14,1,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,14,14,14,14,14,14,14,14,14,1,14,14,14,14,1,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,14,14,14,14,14,14,14,14,14,1,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,1,1,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,1,1,1,1,1,1,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,1,-4142,1,1,3,3,3,3,3,3,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,-4142,-4142,-4142,1,3,3,3,3,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,1,1,1,1,1,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,1,-4142,1,1,1,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,1,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,1,1,1,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,1,1,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,3,3,3,3,3,3,3,3,3,3,3,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142,-4142
Every Excel worksheet is like a big 2d array. Depending on how you want to distribute the code, I'd store it in a worksheet. I copied and pasted your txt file into Excel - Paste Special - Text. Then I did Data - Text to Columns, delimited on Comma. Then I changed the code to this
Sub Why_Not_Zoidberg()
Dim Rng1 As Range
Dim i As Long, j As Long, k As Long
Dim nName As Name
ActiveWindow.DisplayGridlines = False
Set Rng1 = Range("A1:BN48")
With Rng1
.NumberFormat = ";;;" 'hide the values in the cells
.ColumnWidth = 2
.RowHeight = 14.25
.Interior.Color = RGB(255, 255, 255) 'white
End With
For i = 1 To Rng1.Rows.Count
For j = 1 To Rng1.Columns.Count
Select Case Rng1.Cells(i, j).Value
Case 3
Rng1.Cells(i, j).Interior.Color = RGB(253, 3, 74)
Case -4142
Rng1.Cells(i, j).Interior.Color = RGB(255, 255, 255)
Case 1
Rng1.Cells(i, j).Interior.Color = RGB(1, 1, 1)
Case 14
Rng1.Cells(i, j).Interior.Color = RGB(0, 153, 153)
End Select
For k = 1 To 100000: Next k 'adds a delay to animate
Next j
Next i
Rng1.Cells(1).Select
End Sub
I would manually enter all the ZoidDots... my text file... once this populates the cells, then run the other code we made up.
Sub ZoidArray()
Dim Inx As Long
Dim i As Long
Dim j As Long
Set Rng1 = Range("A1:BN48")
Dim ZoidDots() As Variant
ZoidDots = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
For i = 1 To Rng1.Rows.Count
For j = 1 To Rng1.Columns.Count
ActiveSheet.Cells(i, j).Select
With Selection
.Value = ZoidDots(j - 1)
End With
'For k = 1 To 1000000: Next k 'adds a delay to animate
Next j
Next i
End Sub

VBA: replacing array elements

Edit: based on the comments, I'm providing more details on the code.
The idea of the code is:
There are strings stored in a range B6:E6 (e.g. B6 = "Actual Sales", C6 = "SOP11 (2015)", D6 = "SOP12 (2015)", E6 = "SOP10 (2015)").
I calculate the integer by using "Mid" function if the string is not "Actual Sales".
When that's done, the calculated integers are sorted using BubbleSort in array.
Afterwards, I would like to link the sorted integers (SOP_key_B6, SOP_key_C6, SOP_key_D6, SOP_key_E6) with the original string (cell_b6, cell_c6, cell_d6, cell_e6). In other words, there's a one-to-one correspondence between SOP_key_B6 and cell_b6, etc.)
I want to do the above, because I need to input to the range L30:O30 the sorted array with strings based on the sorted integers.
I hope this makes the idea clear as it's not very complicated, but the approach itself & code makes it a bit frustrating (probably because I'm still learning the VB coding).
Here's the code:
Sub Worksheet_Delta_Update()
'Variables
Dim wb As Workbook, ws_wk_dlt As Worksheet, ws_dash As Worksheet, cell_B6 As Variant, _
cell_C6 As Variant, cell_D6 As Variant, cell_E6 As Variant, SOP_key_B6 As Variant, _
SOP_key_C6 As Variant, SOP_key_D6 As Variant, SOP_key_E6 As Variant
'Referencing
Set wb = ThisWorkbook
Set ws_wk_dlt = wb.Worksheets("t")
Set ws_dash = wb.Worksheets("x")
'Values from pivot stored
cell_B6 = ws_wk_dlt.Range("B6").Value
cell_C6 = ws_wk_dlt.Range("C6").Value
cell_D6 = ws_wk_dlt.Range("D6").Value
cell_E6 = ws_wk_dlt.Range("E6").Value
'If len certain amount of characters then do option 1, or option 2
If cell_B6 <> "" Then
If Len(cell_B6) = 12 And cell_B6 <> "Actual Sales" Then
SOP_key_B6 = CInt(Mid(cell_B6, 4, 2)) + CInt(Mid(cell_B6, 8, 4))
ElseIf Len(cell_B6) = 11 And cell_B6 <> "Actual Sales" Then
SOP_key_B6 = CInt(Mid(cell_B6, 4, 2)) + CInt(Mid(cell_B6, 7, 4))
End If
End If
If cell_C6 <> "" Then
If Len(cell_C6) = 12 And cell_C6 <> "Actual Sales" Then
SOP_key_C6 = CInt(Mid(cell_C6, 4, 2)) + CInt(Mid(cell_C6, 8, 4))
ElseIf Len(cell_C6) = 11 And cell_C6 <> "Actual Sales" Then
SOP_key_C6 = CInt(Mid(cell_C6, 4, 2)) + CInt(Mid(cell_C6, 7, 4))
End If
End If
If cell_D6 <> "" Then
If Len(cell_D6) = 12 And cell_D6 <> "Actual Sales" Then
SOP_key_D6 = CInt(Mid(cell_D6, 4, 2)) + CInt(Mid(cell_D6, 8, 4))
ElseIf Len(cell_D6) = 11 And cell_D6 <> "Actual Sales" Then
SOP_key_D6 = CInt(Mid(cell_D6, 4, 2)) + CInt(Mid(cell_D6, 7, 4))
End If
End If
If cell_E6 <> "" Then
If Len(cell_E6) = 12 And cell_E6 <> "Actual Sales" Then
SOP_key_E6 = CInt(Mid(cell_E6, 4, 2)) + CInt(Mid(cell_E6, 8, 4))
ElseIf Len(cell_E6) = 11 And cell_E6 <> "Actual Sales" Then
SOP_key_E6 = CInt(Mid(cell_E6, 4, 2)) + CInt(Mid(cell_E6, 7, 4))
End If
End If
'Finding the Actual Sales and putting into L30
If cell_B6 = "Actual Sales" Then
ws_dash.Range("L31").Value = cell_B6
ElseIf cell_C6 = "Actual Sales" Then
ws_dash.Range("L31").Value = cell_C6
ElseIf cell_D6 = "Actual Sales" Then
ws_dash.Range("L31").Value = cell_D6
ElseIf cell_E6 = "Actual Sales" Then
ws_dash.Range("L31").Value = cell_E6
End If
'BubbleSort in Descending order
Dim ArrayToSort(0 To 4) As Variant
ArrayToSort(0) = SOP_key_B6
ArrayToSort(1) = SOP_key_C6
ArrayToSort(2) = SOP_key_D6
ArrayToSort(3) = SOP_key_E6
'Moving upwards because of -1
For j = UBound(ArrayToSort) - 1 To LBound(ArrayToSort) Step -1
'Starting at lowest
For i = LBound(ArrayToSort) To j
If ArrayToSort(i) > ArrayToSort(i + 1) Then
vTemp = ArrayToSort(i)
ArrayToSort(i) = ArrayToSort(i + 1)
ArrayToSort(i + 1) = vTemp
End If
Next i
Next j
'Put sorted array into the range
'But how to put the values linked to integers?
'E.g. SOP_key_B6 = cell_B6
ws_dash.Range("L30:O30").Value = ArrayToSort
End Sub
Most probably the solution is with replacing the array elements with the correct ones (i.e. SOP_key_B6 = cell_B6, etc.)?
Your code is bloated in places, for example:
Dim ArrayToSort(0 To 4) As Variant
ArrayToSort(0) = SOP_key_B6
ArrayToSort(1) = SOP_key_C6
ArrayToSort(2) = SOP_key_D6
ArrayToSort(3) = SOP_key_E6
can be replaced by
Dim ArrayToSort As Variant 'note lack of ()
ArrayToSort = Array(SOP_key_B6, SOP_key_C6, SOP_key_D6, SOP_key_E6)
As far as your question goes, it seems that you need to use a collection. Assuming that there is a one-to-one correspondence between the SOP-key_ values and the cell_ values (otherwise, calling them "keys" is misleading), you could do the following:
Dim C As New Collection
C.Add cell_B6, CStr(SOP_key_B6)
C.Add cell_C6, CStr(SOP_key_C6)
C.Add cell_D6, CStr(SOP_key_D6)
C.Add cell_E6, CStr(SOP_key_E6)
then, after sorting ArrayToSort, have a loop like:
For i = 0 to 3
Range("L30").Offset(0,i).Value = C(CStr(ArrayToSort(i)))
Next i
I think this is what you are looking for -- but the code seems on the convoluted side so it might not be a bad idea to streamline it a bit.
On Edit:
You are getting duplicate keys due to the way you are constructing the keys by adding note that SOP11(2015) differs from SOP10(2016) but 11+2015 = 10 + 2016 (both equal to 2026). Instead -- juxtapose: 112015 isn't 102016.
Furthermore, it makes sense to split the key creation into its own function (so you don't repeat essentially the same code 4 times:
Function ExtractKey(s As Variant) As Long
Dim v As Variant, n As Long
v = Trim(s)
If v Like "*(*)" Then
n = Len(v)
v = Mid(v, n - 7, 7)
v = Replace(v, "(", "")
ExtractKey = CLng(v)
Else
ExtractKey = 0
End If
End Function
Note that the return type is Long -- Integer variables overflow too easily to be useful in VBA.
Then -- something like this should work:
Sub Worksheet_Delta_Update()
Dim SourceRange As Range, TargetRange As Range
Dim i As Long, j As Long, minKey As Long, minAt As Long
Dim v As Variant
Dim C As New Collection
Set SourceRange = Worksheets("t").Range("B6:E6")
Set TargetRange = Worksheets("t").Range("L30:O30")
For i = 1 To 4
v = SourceRange.Cells(1, i).Value
C.Add Array(ExtractKey(v), v)
Next i
'transfer data
For i = 1 To 4
minAt = -1
For j = 1 To C.Count
If minAt = -1 Or C(j)(0) < minKey Then
minKey = C(j)(0)
minAt = j
End If
Next j
TargetRange.Cells(1, i).Value = C(minAt)(1)
C.Remove minAt
Next i
End Sub
On fixed the Type mismatch error with the following modificaton:
Function ExtractKey(s As Variant) As Long
Dim v As Variant, n As Long
v = Trim(s) 'remove spaces leave only spaces between words
If v Like "*(*)" Then 'if it's SOPXX (YYYY) then
n = Len(v) 'find number of the characters
If n = 11 Then
v = Mid(v, n - 7, 7) 'find the number of SOP + year in bracket
ElseIf n = 12 Then
v = Mid(v, n - 8, 8)
End If
v = Replace(v, "(", "") 'replace the brackets with nothing
v = Replace(v, " ", "")
ExtractKey = CLng(v) 'error WAS here
Else
ExtractKey = 0
End If
End Function
Edit:
Added another few lines
If n = 11 Then
v = Right(v, 4) + Left(v, 1)
ElseIf n = 12 Then
v = Right(v, 4) + Left(v, 2)
End If
The above switch year and number (e.g. SOP12 (2015) = 122015 and after switch 201512). This is because SOP12 (2014) was placed after SOP10 (2015) despite the fact it should go before as its dated year 2014. Now working like charm :)

Resources