VB. If with multiple Or inside - vba

I want to put this If in a macro but it gives me an error all the time. I dont know if "Or" is used correctly or not.
Dim SMAT As String
SMAT = "blahblahblah"
(...)
If Cells(h + 2, 24) <> SMAT Or SMBE Or SMES Or SMFR Or SMGB Or SMGR Or SMRO1 Or SMRO2 Or SMRO3 Or SMDE Then
C(j) = Cells(h + 2, 5)

Use a Select Case block instead:
Select Case Cells(H + 2, 24).Value
Case SMAT, SMBE, SMES, SMFR, SMGB, SMGR, SMR01, SMR02, SMR03, SMDE
Case Else
c(j) = Cells(H + 2, 5).Value
End Select
Or another way using Evaluate(), just for variety*:
varConditions = Array(SMAT, SMBE, SMES, SMFR, SMGB, SMGR, SMR01, SMR02, SMR03, SMDE)
If Evaluate("ISERROR(MATCH(" & Cells(H + 2, 24).Value & ",{" & _
Join(varConditions, ",") & "},0))") Then
c(j) = Cells(H + 2, 5).Value
End If
* This Evaluate method will work when the array contains numbers - if you are using strings you would have to wrap each string in additional quotation marks

Here is the correction
Dim SMAT As String
SMAT = "blahblahblah"
'(...)
If Cells(H + 2, 24) <> SMAT Or _
Cells(H + 2, 24) <> SMBE Or _
Cells(H + 2, 24) <> SMES Or _
Cells(H + 2, 24) <> SMFR Or _
Cells(H + 2, 24) <> SMGB Or _
Cells(H + 2, 24) <> SMGR Or _
Cells(H + 2, 24) <> SMRO1 Or _
Cells(H + 2, 24) <> SMRO2 Or _
Cells(H + 2, 24) <> SMRO3 Or _
Cells(H + 2, 24) <> SMDE Then
c(j) = Cells(H + 2, 5)
End If
Or Operator (Visual Basic)
The error is because you are trying to "talk" to VBA like a person do, but the or does not take the parameter of another or. You need to tell in every parameter of each or to tell the complete logical test
firstCheck = a > b Or b > c
firstCheck = Logical_test Or Logical_test

Related

EXCEL VBA , Allow only if condition verifies

I need a condition that sees if an item has stock in X wharehouse(CC) , if there's any stock than the user can move if there's no stock than the item can't be moved.
Right now i have a condition than only allows only to move from 1 to max stock.
last = Application.ThisWorkbook.Worksheets("Registos").Range("A65536").End(xlUp).Row
For i = 1 To last
REFERENCIA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 8)
ENTRADAeSAIDA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 12)
CC = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 6)
Down bellow is my condition that's suppose to check for the check in and if there's no check in can't be any checkout my problema is that the first time i run CommandButton it doesn't works so there will be a checkout without having any check in , and after the first time i run CommandButton it works but not properly as it also doesn't let me move nothing even if i have it on stock
If registos.ComboBox1 = "SAÍDA" Then
If REFERENCIA <> registos.TextBox1 And CC = registos.Label11.Caption Then
MsgBox "Não existe em stock!"
GoTo fim
Else: GoTo salto_1
End If
End If
End here the condition
salto_1:
If REFERENCIA = registos.TextBox1 And registos.ComboBox1 = "SAÍDA" Then
Worksheets("registos").Select
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=8, Criteria1:= _
registos.TextBox1.Text 'Filtrar referência
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=6, Criteria1:= _
registos.Label11.Caption 'Filtrar CC
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
"ENTRADA" 'Filtrar Entrada
'Somar quantidades de peças de Entrada
xty100 = ThisWorkbook.Worksheets("calculos").Range("A1")
ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
"SAÍDA" 'Filtrar Saída
'Somar quantidade de peças de saída
xty101 = ThisWorkbook.Worksheets("calculos").Range("A1")
sumfinal = xty100 - xty101 'Calculo do Stock
ThisWorkbook.Worksheets("calculos").Range("A20") = Format(registos.TextBox4, "#")
xtybx4 = ThisWorkbook.Worksheets("calculos").Range("A20")
If xtybx4 <= sumfinal And xtybx4 > 0 Then
GoTo salto_2
Else
MsgBox "Não foi possível concluir o movimento!Stock " & sumfinal & ""
GoTo fim
End If
End If
Next i
salto_2:
If registos.TextBox3 = "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 <> "" And registos.ComboBox5 <> "" Then
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 1) = Now() 'data
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 5) = registos.Label20.Caption 'ano fiscal
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 6) = registos.Label11.Caption 'cc
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 7) = a1logiin.TextBox1 'operario
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 8) = registos.TextBox1 'referencia formata
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 9) = registos.TextBox2 'ordem
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 11) = Format(registos.TextBox4, "#") 'quantidade
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 12) = registos.ComboBox1 'ENTRADA/SAIDA
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 13) = registos.ComboBox5 ' ESTADO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 14) = Format(registos.ComboBox3, "#") 'CODIGO DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 16) = registos.ComboBox6 'ORIGEM DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 17) = registos.TextBox5 'OBSERVAÇÕES
MsgBox "Dados introduzidos com sucesso!"
'GoTo fim
GoTo fim2
End If
MsgBox "Insira todos os dados"
GoTo fim
fim2: If registos.ComboBox1 <> "SAÍDA" Then
GoTo fim
End If
If registos.ComboBox1 = "SAÍDA" And registos.TextBox3 = "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 <> "" And registos.ComboBox5 <> "" And registos.ComboBox2 <> "" Then
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 1) = Now() 'data
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 5) = registos.Label20.Caption 'ThisWorkbook.Worksheets("anofiscal").Range("A1") 'ano fiscal
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 6) = registos.ComboBox2 'cc
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 7) = a1logiin.TextBox1 'operario
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 8) = registos.TextBox1 'referencia formata
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 9) = registos.TextBox2 'ordem
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 11) = Format(registos.TextBox4, "#") 'quantidade
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 12) = "ENTRADA" 'Define a saída de um como a entrada de outro
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 13) = registos.ComboBox5 ' ESTADO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 14) = Format(registos.ComboBox3, "#") 'CODIGO DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 16) = registos.ComboBox6 'ORIGEM DEFEITO
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 17) = registos.TextBox5 'OBSERVAÇÕES
Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 18) = registos.ComboBox7 'detalhe produção
GoTo fim
End If
If contador = 1 Then
MsgBox "Foi efetuado o registo de " & contador & " referência!", vbInformation
' Else
' MsgBox "Foi efetuado o registo de " & contador & " referências!", vbInformation
End If
contador = 0
GoTo fim:
fim:
End Sub

trying to join 3 statements together in a select case

I am trying to join these 3 statements together. I can get two to work but not the third.
Check is looking to see if the 5th character = a,b,c,d.
Check0 is looking to see if characterS 2-9 are all numbers.
Check3 is looking to see if the first 3 characters are numbers.
Desired effect
Before > After
Check 0 e01730101.pdf > S-173-0101
Check3 173d00510.pdf > S-173-D005
Check e173d0061.pdf > S-173-D006
Right now Check0 is not working. When I run the code it seems to skip my Case 13 Check0 statement. Anyway i can write this so the 3 checks are not conflicting each other?
Option Explicit
Sub Convert()
Application.ScreenUpdating = False
Dim rng As Range, aCell As Range
Dim val As String, Check, Check0, Check3
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set rng = Range("A2:A" & LastRow)
For Each aCell In rng.Cells
Select Case Len(aCell)
'Case 12 left out
Case 13
Check = Mid(aCell, 5, Len(aCell) - 12)
If Check = "a" Or Check = "b" Or Check = "c" Or Check = "d" Then 'Existing Standard
val = "S-" & Left(aCell, Len(aCell) - 13) & Mid(aCell, 2, Len(aCell) - 10) & "-" & Mid(aCell, 5, Len(aCell) - 9)
Check0 = IsNumeric(Mid(aCell, 2, Len(aCell) - 5)) '|||PROBLEM|||
ElseIf Check0 = True Then 'Existing Three Line Diagrams
val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
End If
Check3 = IsNumeric(Left(aCell, 3))
If Check3 = True Then 'Standard after page 9
val = "S-" & Mid(aCell, 1, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
End If
Check = ""
Check0 = ""
Check3 = ""
'Case 14 left out
Case Else 'All other pages
val = "_Mod " & Left(aCell, Len(aCell) - 4)
End Select
val = UCase(val)
val = val & " " & aCell.Offset(, 2) & aCell.Offset(, 3)
aCell.Offset(, 1).Value = val
Next
Application.ScreenUpdating = True
End Sub
The issue is that your Check0 variable is not initialised when you check it. It would be best to first do all your three checks and only then perform the If...ElseIf....
Secondly, this expression is using the wrong parts of the input string:
val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
I don't really understand why you use Len(aCell) - something, since it is known that the length is 13. Certainly Left(aCell, Len(aCell) - 13) is quite useless, as it will be the empty string.
Here is a correction with some other optimisations (pos is a Long):
Case 13
Check = InStr("abcd", Mid(aCell, 5, 1))
Check0 = IsNumeric(Mid(aCell, 2, 8))
Check3 = IsNumeric(Left(aCell, 3))
pos = IIf(Check3, 1, _
IIf(Check, 2, _
IIf(Check0, 3, 0)))
val = IIf(pos, "S-" & Mid(aCell, pos, 3) & "-" & Mid(aCell, pos+3, 4), "??")

Object doesn't support this property or method

I am essentially trying to match a series of cells from 2 worksheets and obtained values from a corresponding column.
When I run this code, I get an error:
object doesn't support this property or method
My Code
Sub Macro2()
Dim rowcount As Integer
Dim target As Variant
rowcount = Range("E2", Range("E2").End(xlDown)).Count
For i = 1 To rowcount + 1
target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0)
If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _
ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then
ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10)
End If
Next i
End Sub
If you want to find the Row count, you need to use the syntax:
rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count , also it's better to use Long than Integer.
Also, you need to trap a possible error when unable to find a successful match with the Application.Match function, do it by using If Not IsError(target) Then.
Note: try to avoid using ActiveSheet, instead use fully qualified worksheet, by using Worksheets("YourSheetName") in your code.
Code
Sub Macro2()
Dim rowcount As Long
Dim target As Variant
rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count
For i = 1 To rowcount + 1
target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0)
If Not IsError(target) Then ' successful Match
If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _
ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then
ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10)
End If
Else
MsgBox "Unable to find a Match !"
End If
Next i
End Sub
target is not a Property or Method of a Worksheets object.
I believe you need to change Worksheets(14).target.Offset(0, 10) to Worksheets(14).Range("A5").Offset(target, 10).
You should also be consistent in your coding. In your code you have things like ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) ..., where you specifically qualify Cells(i + 1, 6) to be on ActiveSheet but allow Cells(i + 1, 5) to default to being on the ActiveSheet. Although it works, it will get very confusing if you ever need to reread your code later on.

Sum multiple columns using vlookup in vba

I'm trying to sum multiple columns using vlookup within vba. I can do it as an Excel formula in this manner
{=SUM(VLOOKUP(LookupValue,LookupRange,{FirstColumnNo,2ndColumnNo,...,LastColumnNo},0))}
Within VBA, I don't seem to have any luck using that as I am also using a For loop with this formula. You can sum a lot of these lookups but I'm looking for something shorter and elegant
My current code
For i = 1 To ProdCurrentQtr
''''''''Gross Values
ThisWorkbook.Sheets("Central").Cells(i + 8, 1) = ThisWorkbook.Sheets("Central").Cells(i + 8, 2) & "_" & ThisWorkbook.Sheets("Central").Cells(i + 8, 3)
ThisWorkbook.Sheets("Central").Cells(i + 8, 5) = Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 6, 0) _
+ Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 7, 0) _
+ Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 8, 0) _
+ Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 9, 0) _
+ Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 10, 0) _
+ Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, 12, 0)
Next i
But what I'm looking for is something along the lines of
ThisWorkbook.Sheets("Central").Cells(i + 8, 5).FormulaArray = Application.Sum(Application.VLookup(ThisWorkbook.Sheets("Central").Cells(i + 8, 1), LookupRangeCentral, [6,7,8,9,10,12], 0))
Any help appreciated
The following formula does what you want. Just an option:
=SUMPRODUCT(INDEX((ISNUMBER(MATCH(A11:A16,A2:A6&"_"&B2:B6,0)))*(ISNUMBER(SEARCH("," & COLUMN(B11:G16)& ",",",2,4,6,7,")))*B11:G16,))
The ",2,4,6,7," are the actual column numbers, not the relative.
This is summing the values in the above columns where the names are found in A2:A6&"_"&B2:B6.
With ThisWorkbook.Sheets("Central").Cells(8,1)
For i = 1 To ProdCurrentQtr
.Offset(i) = .Offset(i, 1) & "_" & .Offset(i, 2)
Set found = LookupRangeCentral.Find(What:=.Offset(i), LookAt:=xlWhole, LookIn:=xlValues)
If Not found Is Nothing Then .Offset(I, 4) = Application.WorksheetFunction.Sum(found.Offset(,5).Resize(,7)) - found.Offset(,10)
Next i
End With
The array formula approach would be the following
With ThisWorkbook.Sheets("Central").Cells(9,1)
.Resize(ProdCurrentQtr).FormulaR1C1 = "=RC[1] & "" _ "" & RC[2]"
.Offset(,4).FormulaArray = "=SUM(VLOOKUP(" & .Address(False, False) & "," & LookupRangeCentral.Address(,,,True) & ",{6,7,8,9,10,12},0))"
.Offset(,4).AutoFill Destination:=.Offset(,4).Resize(ProdCurrentQtr)
End With

Run-time error '1004': Application-defined or object-defined error with repeated use of "range" object

I'm trying to make an automated templated with VBA and this code seems to run fine when I enter in a low number of "pages", but when I enter in something such as the following into the prompts it gives me a run-time error 1004: 14 pages: 41, 26, 19, 28, 26, 28, 17, 26, 21, 19, 19, 10, 23, 28.
Public TitleSize As Integer
Public MostValves() As Integer
Public TotalValves As Integer
Public TitleBlockSize As Integer
Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
Sub ManualValve()
'On Error GoTo ErrHandler
'On Error Resume Next
Worksheets(1).Activate
ActiveSheet.Name = "Valve List"
ActiveSheet.Cells.Clear
PnIDPage = InputBox("How many pages are on your P&ID?")
'Values for Number of Pages: 14
Dim i As Integer
TotalValves = 0
ReDim MostValves(PnIDPage)
For i = 0 To PnIDPage - 1
ValveCount = InputBox("How many valves are on page " & i + 1 & " ?")
'Values for valves: 41, 26, 19, 28, 26, 28, 17, 26, 21, 19, 19, 10, 23, 28
If IsNumeric(ValveCount) Then
MostValves(i) = ValveCount
TotalValves = TotalValves + ValveCount
Else
MsgBox ("You did not enter a valid number")
'GoTo ErrHandler
End If
Next i
Dim Title As Variant
Response = MsgBox(prompt:="Do you want to use the default titleblock? (Count, Valve, Module, Note)", Buttons:=vbYesNo)
If Response = vbYes Then
Title = Array("Count", "Valve", "Module", "Note")
TitleSize = UBound(Title, 1) - LBound(Title, 1) + 1
Else
Title = Array("Count", "Valve", "Module")
TitleSize1 = UBound(Title, 1) - LBound(Title, 1) + 1
XtraCol = InputBox("How many extra columns would you like to add?")
ReDim Preserve Title(XtraCol + TitleSize1 - 1)
TitleSize = UBound(Title, 1) - LBound(Title, 1) + 1
For i = TitleSize1 + 1 To TitleSize
XtraTitle = InputBox("Extra Title " & i & "?")
Title(i - 1) = XtraTitle
Next i
End If
Dim TitleBlock As Variant
TitleBlock = Array("Project Number", "Project Name", "By", "Rev", "Date")
TitleBlockSize = UBound(TitleBlock, 1) - LBound(TitleBlock, 1) + 1
Range(ConvertToLetter(1) & "1:" & ConvertToLetter(1) & TitleBlockSize) = Application.Transpose(TitleBlock)
Dim Maximum As Integer
Dim ValveNum() As Integer
Dim TempSize As Integer
TempSize = 1
Maximum = WorksheetFunction.Max(MostValves) + 1
For i = 0 To PnIDPage - 1
Do Until MostValves(i) <> 0
i = i + 1
Loop
ReDim ValveNum(MostValves(i))
For j = 0 To MostValves(i)
ValveNum(j) = j + 1
Next j
MsgBox TempSize
If i Mod 2 = 0 Then
Worksheets(1).Range(ConvertToLetter(TempSize) & TitleBlockSize + 2 & ":" & ConvertToLetter(TempSize + TitleSize - 1) & Maximum + TitleBlockSize).Interior.ColorIndex = 42
Else
'This is where I encounter the run-time error
Worksheets(1).Range(ConvertToLetter(TempSize) & TitleBlockSize + 2 & ":" & ConvertToLetter(TempSize + TitleSize - 1) & Maximum + TitleBlockSize).Interior.ColorIndex = 43
End If
Worksheets(1).Range(ConvertToLetter(TempSize) & TitleBlockSize + 2 & ":" & ConvertToLetter(TempSize) & CStr(MostValves(i) + 1 + TitleBlockSize)). _
Resize(MostValves(i), 1) = Application.Transpose(ValveNum)
Worksheets(1).Range(ConvertToLetter(TempSize + 2) & TitleBlockSize + 2 & ":" & ConvertToLetter(TempSize + 2) & CStr(MostValves(i) + 1 + TitleBlockSize)) = "00" & CStr(i + 1)
Worksheets(1).Range(ConvertToLetter(TempSize) & TitleBlockSize + 1 & ":" & ConvertToLetter(TempSize + TitleSize - 1) & TitleBlockSize + 1) = Title
TempSize = TempSize + TitleSize
Worksheets(1).Range(ConvertToLetter(TempSize - 1) & TitleBlockSize + 1 & ":" & ConvertToLetter(TempSize - 1) & Maximum + TitleBlockSize). _
Borders(xlEdgeRight).Weight = xlMedium
Next i
Cells(1, 4) = "Total Valve Count"
Cells(1, 5) = TotalValves
Range("A1:" & ConvertToLetter(TempSize) & Maximum + TitleBlockSize).HorizontalAlignment = xlCenter
Range("A1:A" & TitleBlockSize).HorizontalAlignment = xlLeft
Columns("A:" & ConvertToLetter(TempSize)).AutoFit
Range("A1:" & ConvertToLetter(TempSize) & TitleBlockSize + 1).Font.Bold = True
Range("A" & TitleBlockSize + 1 & ":" & ConvertToLetter(TempSize - 1) & TitleBlockSize + 1).Interior.ColorIndex = 1
Range("A" & TitleBlockSize + 1 & ":" & ConvertToLetter(TempSize - 1) & TitleBlockSize + 1).Font.Color = vbWhite
Range("A" & Maximum + TitleBlockSize & ":" & ConvertToLetter(TempSize - 1) & Maximum + TitleBlockSize). _
Borders(xlEdgeBottom).Weight = xlMedium
'ErrHandler:
'MsgBox "An error has occurred. The macro will end."
End Sub
The problem does not depend on your Valve, but on your ConvertToLetter function. In fact, at some point the error occurs because the function returns an invalid range letter:
input: iCol = 53
return: "A["
Clearly, when you try to call the Range("A[2"), this raises the exception.
The code inside your function is not solid because converts the number into letter with:
ConvertToLetter = Chr(iAlpha + 64)
The Chr() function returns the value associated to the index from the characters collection, which is a unique chars list and cannot be used as you try to do there.
I would just replace your ConvertToLetter function with a more solid one, such as the following:
Function ConvertToLetter(iCol As Integer) As String
Dim vArr
vArr = Split(Cells(1, iCol).Address(True, False), "$")
ConvertToLetter = vArr(0)
End Function
...which has been kindly provided by brettdj in one of his precious answers (don't forget to give him an upvote for this piece of gold ;).
P.s. note that this explain also why a low number would not raise the exception: as long as the number is small, your function doesn't need to append a second letter to the output so it remains consistent. But as soon as it has to do that, CRASH ;)
Use the above function, it's way safer because it just retrieves the Range address from the Cells object. Your code will work fine once you will replace your old function with the new one above.