I want to start second for from next current first for counter I run this code and this error prevent to run code.
ERROR : type mismatch
This code should show shortest distance cells as when find next cell(short distance) this cell should remove from search
also I want to return the address(ROW NUMBER) of next cell(shortest distance)
`Sub distance()
Dim j, i As Integer, ws As Worksheet
Set ws = Worksheets("activesheet")
For i = 2 To 87
For j = i+1 To 87
If j <> i Then
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i,
8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 +
((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j,
9).Value)) ^ 2)
Next j
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
Next i
End Sub`
I have reformatted your code so I could read it, it would not compile because you were missing an End If (as indicated by a comment in the formatted code below). The code runs fine on a test workbook I have just created. However of course I don't know what source data you have.
Sub distance()
Dim j, i As Integer, ws As Worksheet
Set ws = Worksheets("activesheet")
For i = 2 To 87
For j = i + 1 To 87
If j <> i Then
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i, 8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 + ((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j, 9).Value)) ^ 2)
End If 'this was missing
Next j
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
Next i
End Sub
Given that this runs okay, I recommend checking the data being used from the worksheet on this line
Worksheets("activesheet").Cells(j, 11).Value = Sqr(((Worksheets("activesheet").Cells(i, 8).Value) - (Worksheets("activesheet").Cells(j, 8).Value)) ^ 2 + ((Worksheets("activesheet").Cells(i, 9).Value) - (Worksheets("activesheet").Cells(j, 9).Value)) ^ 2)
and this line
ws.Range("l" & i) = Application.WorksheetFunction.Small(ws.Range("k2:k87"), 1)
and make sure you are getting data of the correct type. Do you have any words or letters or non numeric data of any kind being used?
To demonstrate the problem, below I am trying to divide the variable A (a string) by the variable B (an integer) and get a type mismatch error as a result.
Sub test()
Dim a As String
a = "test"
Dim b As Integer
b = 1
Debug.Print a / b
End Sub
Related
I am trying to print out "OK" value if the statements same value with "NumberPallete" but my code doesn't work right.
I have two conditions to compare from one cell value ["54# / 221"]. The first condition value for "SeriesNumber" is [88] and then the Second condition value for "NumberPallete" is [221#]. I am using looping for "SeriesNumber" and "NumberPallete" to find the value because I have long data in the table sheet.
and then from the different sheets using looping too, I am starting with the First condition checks "SeriesNumber" value if the value is right, then check the second condition from "NumberPallete" value, in this condition, I want a print out "ok" value but "ok" value doesn't print out.
I am sorry, my English is poor. I'm trying my best to explain. Please help me.
Dim NumberPallete As String
Dim SeriesNumber As String
Dim I As Long
Dim j As Long
Dim z As Long
i = Cells(Rows.Count, 15).End(xlUp).Row
For j = 6 To i
'Cells(j, 20).Value = Right(Cells(j, 15).Value, Len(Cells(j, 15)) - InStr(1, Cells(j, 15).Value, "/"))
SeriesNumber = Right(Cells(j, 15).Value, Len(Cells(j, 15)) - InStr(1, Cells(j, 15).Value, "/"))
'Cells(j, 21).Value = Left(Cells(j, 15).Value, Len(Cells(j, 15)) - InStr(1, Cells(j, 15).Value, "/"))
NumberPallete = Left(Cells(j, 15).Value, Len(Cells(j, 15)) - InStr(1, Cells(j, 15).Value, "/"))
If SeriesNumber = 221 Then
For z = 4 To 250
If Worksheets("AAA").Cells(z, 2).Value = NumberPallete Then
Worksheets("AAA").Cells(z, 6).Value = "OK"
End If
Next z
Else
MsgBox ("Not OK")
End If
Next j
I may not have fully understood what you are trying to do but the code below is doing something and, hopefully, it can be fixed to do what you want.
Sub FindPalletNumber()
' 062
' you can find code to enter 2 values with input boxes at this link:-
' https://stackoverflow.com/questions/62651211/vba-excel-search-in-excel-specific-word-and-delete-all-rows-who-does-not-have-t
Dim Snum As Integer ' serial number
Dim Pnum As Integer ' pallet number
Dim Txt As String ' message text
Snum = 221 ' number only
Pnum = 54 ' no # sign, no brackets
If MarkOK(Snum, Pnum) Then
Txt = "Found and marked."
Else
Txt = "No match found."
End If
MsgBox Txt, vbInformation, "Action report"
End Sub
Private Function MarkOK(Snum As Integer, _
Pnum As Integer) As Boolean
' 062
' return True if found and marked
Const Pallet As Long = 0 ' element of array Nums
Const Serial As Long = 1 ' element of array Nums
Dim Nums() As String ' split cell pattern "54# / 221"
Dim Done As Boolean ' True if found
Dim R As Long ' loop counter: Row in ActiveSheet
Dim R2 As Long ' loop counter: Row in Ws("AAA")
For R = 6 To Cells(Rows.Count, 15).End(xlUp).Row
Nums = Split(Cells(R, 15).Value, "/")
' Nums(Pallet) = "54# ", Nums(Serial) = " 221"
If Val(Nums(Serial)) = Snum Then
With Worksheets("AAA")
For R2 = 4 To .Cells(.Rows.Count, 2).End(xlUp).Row
If .Cells(R2, 2).Value = Trim(Nums(Pallet)) Then
.Cells(R2, 6).Value = "OK"
Done = True
Exit For
End If
Next R2
End With
End If
If Done Then Exit For ' stop search if found
Next R
MarkOK = Done
End Function
In the first procedure the Pallet and Serial numbers should be set (Pnum and Snum). Then, when you run that procedure, it will call the other one which reports back whether a match was found or not. I have added a link where you can find code to get the two values from Input boxes, if that is what you need.
The function looks for the serial number in the ActiveSheet. If found, it looks for the pallet number in Sheet("AAA"). This is confusing because it looks for the pallet number found in the ActiveSheet, not the pallet number specified in the search. The pallet number in the search specs ends up not being used at all. Perhaps it's not needed.
Anyway, when the pallet is found the row is marked and the search terminates. If the pallet number isn't found the loop in the ActiveSheet is continued to look for another instance of the serial number. Note that the code is not enabled to find multiple pallets with the same serial number.
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
I'm trying to unhide a range of rows. This is to be done within a loop, so I'm using variables; here's the code:
For i = 2 To lastrow
If Workbooks("Discrepancies1").Worksheets(1).Cells(i, 8) = "USRFLG02=T" Then
a = Workbooks("Discrepancies1").Worksheets(1).Cells(i, 46).Value
b = Application.WorksheetFunction.CountIf(c, a)
Workbooks("Discrepancies1").Worksheets(1).Rows("i: i + b - 1").Hidden = False
End If
Next
End If
However, running this gives me a type mismatch error on the last line of code. I've checked all the variables, and they are what they should be. It seems like VBA doesn't like the colon (denoting range) in conjunction with variables. I can run this without variables, or without the range, but I can't have both. Suggestions?
With Workbooks("Discrepancies1").Worksheets(1)
For i = 2 To lastrow
If .Cells(i, 8) = "USRFLG02=T" Then
a = .Cells(i, 46).Value
b = Application.CountIf(c, a)
.Rows(i & ":" & i + b - 1).Hidden = False
End If
Next
End With
Can anyone help me? (I am new in VBA and i also got help to create this macro)
I created a macro for a file and first it was working fine, but today I've been opening and restarting the file and macro hundreds of times and I'm always getting the following error: Excel VBA Run-time error '13' Type mismatch
I didn't change anything in the macro and don't know why am I getting the error. Furthermore it takes ages to update the macro every time I put it running (the macro has to run about 700 rows).
The error is in the between ** **.
VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim j As Integer
Dim z(8) As Integer
Set ws = ThisWorkbook.ActiveSheet
For i = 6 To ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 3
If Not ws.Rows(i).Hidden = True Then
For j = 0 To 8
If Not ws.Cells(i, j + 5) = "" Then
** z(j) = z(j) + ws.Cells(i, j + 5) **
End If
Next j
End If
Next i
Application.EnableEvents = False
For j = 0 To 8
ws.Cells(ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 2, j + 5) = z(j)
Next j
Application.EnableEvents = True
End Sub
ws.Cells(i, j + 5) at a particular point does not contain a value that can be added to z(j). It's probably blank or doesn't contain a number.
One fix would be to write
On Error Resume Next 'switch off error handling
z(j) = z(j) + ws.Cells(i, j + 5)
On Error Goto 0 'the idiomatic way of switching the default error handling back on
Effectively then, your macro will not attempt to add a non-numeric value to z(j).
But solving the problem this way is a bit like using a sledgehammer to crack a nut: perhaps it's worth investigating the contents of the cell that's causing the error, and programming around that more elegantly.
Also, I question why this needs to be in VBA in the first place. Really you're doing something that excel can do in normal calculation. (Consider SUMIF and SUMIFS) Failing that, it would be quicker if you used a VBA Function rather than responding to change events.
In addition to what #Bathsheba mentioned, you might also want to consider declaring variables before using them to avoid problems. I wasn't able to run your code because Excel wouldn't know how to handle Z() and thought of it as a UDF. Try the following and let me know if this solves the problem:
Option Base 0
Option Explicit
Sub tmpTest()
Dim ws As Worksheet
Dim i As Long, j As Integer
Dim z(0 To 8) As Double
Set ws = ThisWorkbook.ActiveSheet
For i = 6 To ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 3
If Not ws.Rows(i).Hidden = True Then
For j = 0 To 8
If Not ws.Cells(i, j + 5) = "" Then
z(j) = z(j) + IIf(VarType(ws.Cells(i, j + 5).Value) = vbError, 0, ws.Cells(i, j + 5).Value)
End If
Next j
End If
Next i
Application.EnableEvents = False
For j = 0 To 8
ws.Cells(ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 2, j + 5) = z(j)
Next j
Application.EnableEvents = True
End Sub
I have an excel spreadsheet. In a column of the spreadsheet I have a list of codes (numbers).These codes (numbers) are sorted from highest to lowest values.(some of these codes has been repeated. For example I have three consecutive line with code 1001200).I want to insert new rows between each codes (in case of having repeated codes i just need one new row (for example i Just need one new row for 1001200 not 3 rows) .
I have written the following code but it does not work.
Sub addspace()
Dim space_1(5000), Space_2(5000)
For n = 1 To 5000
Debug.Print space_1(n) = Worksheets("sheet3").Cells(1 + n, 1).Value
Debug.Print Space_2(n) = Worksheets("sheet3").Cells(2 + n, 1).Value
Next
For n = 1 To 5000
If space_1(n) <> Space_2(n) Then
Range("space_1(n)").EntireRow.Insert
End If
Next
End Sub
How can I fix it? (From the code you can see that I am so beginner :)))
Cheers
To insert one empty row between each unique value try this:
Option Explicit
Public Sub addspace()
Dim i As Long
Application.ScreenUpdating = False
With Worksheets("sheet3")
For i = 5000 To 2 Step -1
While .Range("A" & i - 1) = .Range("A" & i)
i = i - 1
Wend
.Rows(i).Insert Shift:=xlDown
Next
End With
Application.ScreenUpdating = True
End Sub
It starts from the end row and moves up, skipping duplicates
The Range("space_1(n)") is invalid. Arg of range object should be a column name like "A1", you can use Range("A" & n).EntireRow.Insert in your code. But I recommend my code.
Please try,
Sub addspace()
Dim n As Integer
For n = 1 To 5000
If Worksheets("sheet3").Cells(n, 1).Value <> Worksheets("sheet3").Cells(n + 1, 1).Value Then
Worksheets("sheet3").Cells(n + 1, 1).EntireRow.Insert
n = n + 1
End If
Next
End Sub