Wrong number of arguments or invalid property assignment on Evaluate function - excel

I'm sure this is something obvious but I haven't gotten it yet. Here is my code:
Sub Grossbypercent()
Dim GrossWeight As Double
Dim i As Long
Dim Val As Long
Dim div(2 To 250) As Variant
Dim x As Long
GrossWeight = Application.WorksheetFunction.Sum(Range("H2:H250"))
For x = 2 To 250
Worksheets("Sheet2").Range("B2:B250") = Application.WorksheetFunction.SumIf(Range("Sheet2!A$2:A$250"), Cells(x, 9), Range("H$2:H$250"))
Next x
For i = 2 To 250
Val = Worksheets("Sheet2").Cells(i, 2)
div(i) = Evaluate(Val & "/" & GrossWeight)
Worksheets("Sheet2").Cells(i, 3) = div(i) * Range("O2").Value
On Error Resume Next
Next i
End Sub
I get the titled error on the Evaluate line, before which the code works fine and outputs as requested. I'm going to warrant a guess that I'm declaring something incorrectly, but I'm not sure what, any guidance would be appreciated.

Related

Finding input data - Run time error 13 - Type mismatch

I'm trying to build a macro, which should find input data in an excel row. Everything works fine when the input data is somewhere available in the excel rows. But when the the input data is not available in the rows, I get a run time error 13 - Type mismatch. I don't understand why this is happening.
My input data, which I declare as a Variant, is something like:
040234-A
0463796
8745-00
These will be found, but if I change the input data to for example 040234- or 04637 I'll get the error.
Does anyone have an idea on how to solve this? In the following you'll find my code. Here "Gescanntes Material" is the important input data. I get the error message at:
MaterialDatenbankZeile = Application.Match(GescanntesMaterial, Range(MaterialienRange), 0)
Thanks!
Sub Traceability()
Worksheets("Materialien").Activate
Dim GescanntesEndgeraet As Variant
Dim GescanntesMaterial As Variant
Dim ZeileEndgeraetStart As Integer
Dim ZeileEndgeraetEnd As Integer
Dim MaterialDatenbankZeile As Integer
Dim MaterialDatenbank As Variant
GescanntesEndgeraet = Worksheets("Uebersicht").Cells(7, "C").Value
GescanntesMaterial = Worksheets("Uebersicht").Cells(11, "C").Value
ZeileEndgeraetStart = Application.WorksheetFunction.Match(GescanntesEndgeraet, Range("A:A"), 0)
ZeileEndgeraetEnd = Application.WorksheetFunction.Match(GescanntesEndgeraet, Range("A:A"), 0) + Application.WorksheetFunction.CountIf(Range("A:A"), GescanntesEndgeraet) - 1
Dim MaterialienRange As String
Let MaterialienRange = "B" & ZeileEndgeraetStart & ":" & "B" & ZeileEndgeraetEnd
MaterialDatenbankZeile = Application.Match(GescanntesMaterial, Range(MaterialienRange), 0)
If IsError(MaterialDatenbankZeile) Then
Let MaterialDatenbank = "A"
Else
MaterialDatenbankZeile = MaterialDatenbankZeile + ZeileEndgeraetStart - 1
MaterialDatenbank = Worksheets("Materialien").Cells(MaterialDatenbankZeile, "B").Value
End If
If MaterialDatenbank = GescanntesMaterial Then
Worksheets("Uebersicht").Cells(7, "F").Value = "true"
Else
Worksheets("Uebersicht").Cells(7, "F").Value = "false"
End If
Worksheets("Uebersicht").Activate
End Sub
Change
Dim MaterialDatenbankZeile As Integer
to
Dim MaterialDatenbankZeile As Variant
otherwise you cannot capture the error value when the Application.Match fails.
FYI pretty much no-one uses Let in VBA - you can omit that.

Type mismatch using split VBA

I have the following code and it gives me the error Type mismatch for the line of code «Split_dt_2 = Split(Split_dt_1, ",")». I'm not able to run through the code with F8 because it gives me the error right away so i can't give the exact value of «Split_dt_1» but it's always a date which has that form : [11/1/2019,12/1/2019].
My goal would be to obtain :
y_Dest = 2019 and m_Des = 11
Sub import_Redeem_Spread()
Workbooks.Open "C:\Users\106400\OneDrive\Documents\FTT\CDOPT_AB.xlsm"
Dim wksSource As Worksheet, wksDest As Worksheet
Set wksSource = Workbooks("CDOPT_AB.xlsm").Sheets(2)
Set wksDest = ThisWorkbook.Sheets(2)
Dim Split_dt_1() As String
Dim Split_dt_2() As String
Dim Split_dt_3() As String
Dim Split_dt_4() As String
nbRows = wksSource.Cells(Rows.Count, 1).End(xlUp).Row
nbDates = wksDest.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To nbRows
If wksSource.Cells(i, 16) = "CPG Taux Fixe" Then
For m = 7 To nbDates
Split_dt_1 = wksDest.Cells(m, 2)
Split_dt_2 = Split(Split_dt_1, ",")
Split_dt_3 = Split_dt_2(0)
Split_dt_4 = Split(Split_dt_3, "[")
y_Dest = Right(Split_dt_4(1), 4)
m_Dest = Left(Split_dt_4(1), 2)
y_source = Left(Cells(I, 3), 4)
m_Source = Right(Cells(I, 3), 2)
If y_Dest = m_Dest & y_Source = m_Source Then
For n = 4 To 15
wksDest.Cells(m, n) = wksSource.Cells(i, n)
Next n
End If
Next m
End If
Next i
End Sub
I tried «Dim Split_dt_2() As Variant» but it does noes solve the problem
and I tried
Split_dt_1 = wksDest.Cells(m, 2).value
Split_dt_2 = Split(Split_dt_1, ",")
and it still doesn't work
Thanks in advance!
Use a Variant when using Split to create the array instead of Diming it as a String array.
A Variant will take on the properties of an Array when the function you are using returns an Array.
Dim Split_dt_1 As Variant
Split_dt_1 = Split(wksDest.Cells(m, 2), ",")
I would ditch assigning the Arrays and all the intermediate steps altogether:
y_Dest = Year(Split(Split(wksDest.Cells(m, 2), ",")(0), "[")(0))
There are times when having those intermediate steps helps, but IMO, this isn't one of them.

Mismatch error in VBA - problems with columns

I have a workbook where I want to find the differences of two sheets by looking at the company name and their corporate registration number and then type the differences on the third sheet.
I have tried the code in another workbook with only 143 rows, which works perfectly, but when I try it on the real workbook with 10,000 rows I get a "type mismatch error". Also if I use other columns than the CVR and Firm columns the code also works.
The CVR is numbers and Firms are strings (firm names). I get the
type mismatch error
on the line I marked **. Does somebody know why I get this error?
Sub ComCVR()
Dim CVR1()
Dim CVR2()
Dim Firm1()
Dim Firm2()
Dim n As Long, m As Long
Dim i As Double, j As Double
Dim intCurRow1 As Integer, intCurRow2 As Integer
Dim rng As Range, rng1 As Range
Set rng = ThisWorkbook.Sheets("Last month").Range("A11")
Set rng1 = ThisWorkbook.Sheets("Current month").Range("A11")
n = rng.CurrentRegion.Rows.Count
m = rng1.CurrentRegion.Rows.Count
ReDim CVR1(n)
ReDim Firm1(n)
ReDim CVR2(m)
ReDim Firm2(m)
ThisWorkbook.Sheets("CVR").Range("A1") = "Flyttet CVR"
ThisWorkbook.Sheets("CVR").Range("B1") = "Flyttet Firmanavn"
ThisWorkbook.Sheets("CVR").Range("A1:B1").Interior.ColorIndex = 3
ThisWorkbook.Sheets("CVR").Range("C1") = "Nye CVR"
ThisWorkbook.Sheets("CVR").Range("D1") = "Nye Firmanavn"
ThisWorkbook.Sheets("CVR").Range("C1:D1").Interior.ColorIndex = 4
ThisWorkbook.Sheets("CVR").Range("A1:D1").Font.Bold = True
' Inset data to arrays
For i = 0 To n
CVR1(i) = ThisWorkbook.Sheets("Last month").Cells(12 + i, 5)
Firm1(i) = ThisWorkbook.Sheets("Last month").Cells(12 + i, 4)
Next
For i = 0 To m
CVR2(i) = ThisWorkbook.Sheets("Current month").Cells(12 + i, 5)
Firm2(i) = ThisWorkbook.Sheets("Current month").Cells(12 + i, 4)
Next
intCurRow1 = 2
intCurRow2 = 2
'Old
For i = 0 To n
For j = 0 To m
If Firm1(i) = ThisWorkbook.Sheets("Current month").Cells(12 + j, 4) Then '** Error raised here
Exit For
End If
If j = m Then
ThisWorkbook.Sheets("CVR").Cells(intCurRow1, 1) = CVR1(i)
ThisWorkbook.Sheets("CVR").Cells(intCurRow1, 2) = Firm1(i)
intCurRow1 = intCurRow1 + 1
End If
Next j
Next i
'new
For i = 0 To m
For j = 0 To n
If Firm2(i) = ThisWorkbook.Sheets("Last month").Cells(12 + j, 4) Then
Exit For
End If
If j = n Then
ThisWorkbook.Sheets("CVR").Cells(intCurRow2, 3) = CVR2(i)
ThisWorkbook.Sheets("CVR").Cells(intCurRow2, 4) = Firm2(i)
intCurRow2 = intCurRow2 + 1
End If
Next j
Next i
Columns("A:B").Select
ActiveSheet.Range("$A:$B").RemoveDuplicates Columns:=1, Header:=xlNo
Application.DisplayAlerts = False
Columns("C:D").Select
ActiveSheet.Range("$C:$D").RemoveDuplicates Columns:=1, Header:=xlNo
Application.DisplayAlerts = False
End Sub
Whenever an error happens, the best way is to google it. This is what it says in the documentation of VBA for Type mismatch:
Cause: The variable or property isn't of the correct type. For example, a variable that requires an integer value can't accept a string value unless the whole string can be recognized as an integer.
In the case of the code, it happens, when an array is compared with excel cell. Now the trick - in order to see why it happens, see what is in these:
Debug.Print ThisWorkbook.Sheets("Last month").Cells(12 + i, 4)
Debug.Print Firm1(i)
and the after the error runs, take a look at the immediate window (Ctrl+G). It it quite possible, that there is an error in the excel cell, thus it cannot be compared. This is some easy way to avoid it, if this is the case:
Sub TestMe()
Dim myRange As Range
Set myRange = Worksheets(1).Cells(1, 1)
myRange.Formula = "=0/0"
If Not IsError(myRange) Then
Debug.Print CBool(myRange = 2)
Else
Debug.Print myRange.Address; " is error!"
End If
End Sub

run-time error 13 - type mismatch in if function within for loop (trying to match error against string)

I am trying to locate the first cell (row) stating "#N/A" in specific columns. I cannot work around a type mismatch error I get. I have googled and read a lot of similar stackflow questions and answers but still could not solve it.
The main things I have tried so far (besides various little changes):
used the immediate window and debug print to check outputs (the GetDates sub is working correctly)
converting the collection to an array where I can define a data type
using a while function instead of for (in this case I get it to attempt the while function but on the last iteration I get a type mismatch again)
here is the full code:
Dim EndofWeekDates As New Collection
Dim EndofRange As New Collection
Dim lCol As Long
Dim lRow As Long
Dim i As Long
Dim j As Long
Dim v As Long
Dim x As Long
Sub GetDates()
Set EndofWeekDates = Nothing
i = 4
j = 1
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
While j < lCol + 1
If Not IsEmpty(Cells(i, j).Value) And Not Cells(i, j).Value = "End" Then
EndofWeekDates.Add j
End If
j = j + 1
Wend
Call GetRange
End Sub
Sub GetRange()
Set EndofRange = Nothing
For x = EndofWeekDates.Count To 1 Step -1
lRow = Cells(Rows.Count, EndofWeekDates(x)).End(xlUp).Row
For v = 15 To lRow
If Cells(v, EndofWeekDates(x)).Value = "#N/A" Then
EndofRange.Add v
Exit For
End If
Next v
Next x
End Sub
I get the error in the following section on the IF line
For v = 15 To lRow
If Cells(v, EndofWeekDates(x)).Value = "#N/A" Then
EndofRange.Add v
Exit For
End If
Next v
The EndofWeekDates(x) should be constant during each 15 - lRow run while v changes. I have tried putting in the variable i (used earlier) instead of v and it works but only if i remains constant and is not changed in the for loop. As far as I can see the issue is with the v and not with EndofWeekDates(x). Furthermore, it seems that the issue only occurs when I do not use a constant but a changing number per for iteration. I tried to use the same while function as in GetDates but that did not solve it either.
Since v is declared as Long and I have also tried integer, I am stuck. Especially since the earlier used Cells.Value works with a Long which is increased in each iteration.
Your line
If Cells(v, EndofWeekDates(x)).Value = "#N/A" Then
is crashing because the cell does not contain the string "#N/A" but instead contains an error code, which Excel displays as #N/A.
A comparison of the error code to a string cannot be performed as there is no type conversion that allows the two sides of the comparison to be cast to a common data type - therefore it generates a "type mismatch" error.
The correct way to test for an #N/A error condition would be
If Application.IsNA(Cells(v, EndofWeekDates(x))) Then
This is a very peculiar error. I am not sure what is causing it but using Cells().Text instead of Cells().Value will work properly.
Try below
For v = 15 To lRow
If Cells(v, EndofWeekDates(x)).Text = "#N/A" Then
EndofRange.Add v
Exit For
End If
Next v
Alternatively,
For v = 15 To lRow
If Application.WorksheetFunction.IsNA(Cells(v, EndofWeekDates(x))) Then
EndofRange.Add v
Exit For
End If
Next v

cells().value = ... application or object defined error

I want to create a summary table in a new sheet, and at the moment I'm just doing it very crudely. I will try a more elegant solution in the future.
Anyway, this is the code I have so far:
Sub createsummarytable()
Worksheets.Add().Name = "datasummary"
With Worksheets("datasummary")
Dim i As Long
Dim Startpoint As Long
Startpoint = -5
For i = 1 To 40
.Cells(Startpoint + (5 * i), 1).Value = "Block" & "i"
Next i
End With
End Sub
I am getting the error in the title on line: .Cells(Startpoint + (5 * i), 1).Value = "Block" & "i"
If anyone wants to make the code more elegant in addition to solving the error, that would be appreciated.
Off-by-one. There is no column/row 0 in Excel; -5 + (5 * 1) evaluates to 0:
.Cells(0, 1).Value = 42 'same error
You need to adjust by +1:
For i = 1 To 40
.Cells(Startpoint + (5 * i) + 1, 1).Value = "Block" & i
Next i
If your code works as intended, describe your working code in a new question on Code Review.
#Mat's Mug picked up on the off-by-one error, and #kpg987 got your use of "i" as a string-literal instead of a variable, but your code can be improved.
Here are some of the changes I made:
Scope the procedure to Private or Public
Use meaningful names
Use Constants to determine the starting row and number of each rows for each block. StartingPoint = -5 isn't useful, whereas START_ROW = 1 is quite clear about what it is and where it will actually start.
Adjust the formula to use the constants.
Refer to the target workbook explicitly, otherwise the active workbook will be used.
Set a reference to the added worksheet when using the add command.
Use the strongly typed reference with the With command, because With Worksheets("datasummary") will be late bound)
Use camelCasing for variable names
Result:
Private Sub createSummaryTable()
Const WORKSHEET_NAME As String = "datasummary"
Const START_ROW As Long = 1
Const BLOCK_COLUMN as Long = 1
Const BLOCK_SIZE As Long = 5
Const BLOCK_COUNT As Long = 40
Const BLOCK_PREFIX As String = "Block"
Dim dataSummary As Worksheet
Set dataSummary = ThisWorkbook.Worksheets.Add
With dataSummary
.Name = WORKSHEET_NAME
Dim blockCounter As Long
For blockCounter = 1 To BLOCK_COUNT
.Cells(START_ROW + (BLOCK_SIZE * (blockCounter - 1)), BLOCK_COLUMN).Value = BLOCK_PREFIX & blockCounter
Next blockCounter
End With
End Sub
a "no-loop" approach:
Private Sub createSummaryTable2()
Dim dataSummary As Worksheet
With ThisWorkbook.Worksheets.Add
.Name = "datasummary"
With .Range("A1").Resize((40 - 1) * 5 + 1)
.FormulaR1C1 = "=IF(MOD(ROW(),5)=1,""Block"" & int(ROW()/5)+1,"""")"
.Value = .Value
End With
End With
End Sub

Resources