Getting a type mismatch error - vba

I am getting a run-time error 13 'Type Mismatch' on this chunk of code and I cannot figure out why. I have not used the Mid() function before and that is the line that throws the error. Beginner here, any and all help appreciated.
I am wanting that if line to check to see if the third character in a string equals 4. If you know of a better way I am open to that as well.
For k = 2 To NRow
If Mid(SummarySheet.Range("B2:B" & k), 3, 3) = 4 Then
SummarySheet.Range("B" & k & ":D" & k).Cut
SummarySheet.Range("R" & k & ":T" & k).PasteSpecial xlPasteValues
End If
Next

I believe this is what you are trying to do:
You will need to iterate backwards through your data.
Mid only allows one cell at a time. And the third criterion is the length so it should be 1.
You can set the values then remove the data from the set.
For k = Nrow To 2 Step -1
If Mid(SummarySheet.Range("B" & k), 3, 1) = "4" Then
SummarySheet.Range("R" & k & ":T" & k).Value = SummarySheet.Range("B" & k & ":D" & k).Value
SummarySheet.Range("B" & k & ":D" & k).Delete xlshiftUp
End If
Next
Now if you only want to clear the values and keep a blank row in the data then use this:
For k = Nrow To 2 Step -1
If Mid(SummarySheet.Range("B" & k), 3, 1) = "4" Then
SummarySheet.Range("R" & k & ":T" & k).Value = SummarySheet.Range("B" & k & ":D" & k).Value
SummarySheet.Range("B" & k & ":D" & k).Clear
End If
Next

Mid() is used to get a substring of a single text value. Since you are trying this on an array, it will not work. I'm not completely sure this is what you want, but this should not throw an error:
If Mid(SummarySheet.Range("B" & k).Value, 3, 1) = "4" Then
Notice the reference is for a single cell, and the "4" is quoted to show that it's a string. I changed your last argument to 1 because you are looking for a single character.

Related

I can't range the cells interval

I created a code so that each time a record was inserted into a line the program would detect the last line with values (assigned to the variable "k") and select a specific interval for the graph to update, but an error occurs, could someone help me ?
VBA Code:
Public Sub Chart_Update()
Dim k As Long
k = Cells(Rows.Count, "B").End(xlUp).Row
Range ("B2:B" & k & ",E2:E" & k & ",I2:K" & k & ",J2:J" & k & "K2:K & k")
'Error there!!
----------------------
ActiveChart.SetSourceData Source:=Range( _
"AC_Offset_Registers!"$B$2:$B$" & k ,AC_Offset_Registers!"$E$2:$E$" & k ,AC_Offset_Registers!"$I$2:$I$" & k ,AC_Offset_Registers!"$J$2:$J$" & k,AC_Offset_Registers!"$K$2:$K$" & k" _
)
-----------------
End Sub
I know the variable "k" is in a comment but I don't know where I should put the quotes so the syntax is correct
This
Range("B2:B&k,E2:E&k,I2:I&k,J2:J&k,K2:K&k")
is no valid syntax and needs to be changed to
Range("B2:B" & k & ",E2:E" & k & ",I2:I" & k & ",J2:J" & k & ",K2:K" & k)
or shorter
Range("B2:B" & k & ",E2:E" & k & ",I2:K" & k)
The same technique needs to be applied to your other range.
Finally you might benefit from reading
How to avoid using Select in Excel VBA.

Concatenate two ranges without looping

I want to concatenate two ranges into one WIHTOUT using a loop.
Below is the code. The lines with comment's is basically the solution I want to avoid.
With ws_AUoM
lCountEntriesInAUoMFile = .Cells(Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
' For lLoopCounterAUoM = 2 To lCountEntriesInAUoMFile
'
' .Cells(lLoopCounterAUoM, "O").Value = .Cells(lLoopCounterAUoM, "B").Value & .Cells(lLoopCounterAUoM, "F").Value
'
' Next lLoopCounterAUoM
End With
This line:
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
returns the error "Type Mismatch". I have double checked the sizes and location of each range. Yet it does not work. What am I missing here?
You can do this:
Dim r As Long
With ws_AUoM
r = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & r).Value = .Evaluate("B2:B" & r & " & F2:F" & r)
End With
Evaluate knows you're giving it an array formula, and will return the resulting array, which you can assign directly to the sheet.

Excel VBA code adding blank rows

I am new to VBA and I have written the below code that is supposed to compare dates in two columns, and take which ever date is greater and display it on a Worksheet called PPDCI. If there is no dates, or dates are equal then it will display that record on another worksheet called "Error" and "REVIEW PPD DATA".
The program seems to work fine for the first two IF conditions for variables PPD_1_Date and PPD_2_Date that output data to the PPDCI worksheet, however the results on the Error tab are not what I am expecting. It seems to be including blank rows (rows that I believe are on the PPDCI tab with data), rows that only contain the cell with "REVIEW PPD DATA" (ID, Name information missing), and rows that just have data in columns A - C (sourced from the "Data" worksheets columns F - H).
I tried changing the code (commented out below) to include a condition for if the two date fields are empty then GoTo EmptyRange, just prior to iterating the Next i. This producted a runtime error though, even though it works on several other functions in my module
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value =
Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value =
Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
j = j + 1
Else
'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And
IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
'GoTo EmptyRange
'Else
Worksheets("Error").Range("A" & k & ":C" & k).Value =
Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("Error").Range("F" & j).Value = "REVIEW PPD DATA"
k = k + 1
'End If
End If
End If
EmptyRange:
'k = k + 1
Next i
End Function
I would expect all the rows that qualify for the final Else statement to be grouped together and not missing any of the cells. Should I be incrementing k (k = k+1) somewhere else in the code? Any feedback is appreciated!

Summing Two Columns - Type mismatch Error

I am trying to sum two columns, but i keep getting an error message of type mismatch. Where my error is?
Sub SumCols()
Dim ws As Worksheet
Set ws = Sheets("Recon")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
Next i
End Sub
The below might be my issue, I checked for blank cells and non were found. But I can see blank cells.
Most likely one of the values you are trying to add together is not numeric, so check to see if they are numeric and non-blank before you try adding them.
For i = 2 To LastRow
If Len(Range("C" & i).Value) > 0 And Len(Range("D" & i).Value) > 0 Then
If IsNumeric(Range("C" & i).Value) And IsNumeric(Range("D" & i).Value) Then
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
End If
End If
Next i
Also, you might be better off just using a formula:
Range("E" & i).Formula = "=C" & i & "+D" & i

VBA -- variable in .formula

is there a more elegant (simpler) way to put a variable in .formula? I don't want to use .formulaR1C1
I have this code:
Range("C8").Select
Selection.End(xlDown).Select
PosR = ActiveCell.Row
KonR = PosR - 2
Range("N" & PosR).Select
aAddress = Range("$N$9").Address & ":" & Range("$N$" & KonR).Address
ActiveCell.Formula = "=SUM(" & aAddress & ")"
Obviously I want to put =SUM($N$9:$N$101) (101 is the last cell minus 2) into that cell and this code does the job. But I just want to be sure that this is the easiest way to do this.
The easiest way is to skip all that selecting and those variables
PosR = Range("C8").End(xlDown).Row
Range("N" & PosR).Formula = "=SUM($N$9:$N$" & PosR - 2 & ")"
Edit: to be more explicit, the easiest way is to use FormulaR1C1 but you said you didn't want to, so...
You can use the code below (without using Select and ActiveCell:
PosR = Range("C8").End(xlDown).Row
KonR = PosR - 2
Range("N" & PosR).Formula = "=SUM(" & Range("$N$9").Address & ":" & Range("$N$" & KonR).Address & ")"
Or, the much simplier version:
Range("N" & PosR).Formula = "=SUM($N$9:$N$" & KonR & ")"
Well you should be trying to avoid using Select in VBA. You've made the actual inclusion of a variable in the .Formula about a simple as it gets, but your whole code could be simplified:
PosR = Range("C8").End(xlDown).Row
Range("N" & PosR).Formula = "=SUM($N$9:$N$" & PosR - 2 & ")"
Really you should be fully qualifying your ranges too, like so
With ThisWorkbook.Sheets("Sheet1")
PosR = .Range("C8").End(xlDown).Row
.Range("N" & PosR).Formula = "=SUM($N$9:$N$" & PosR - 2 & ")"
End With
And if you have blank cells in column C then your use of xlDown will fail to find the last cell. You may want to look at ways of finding the last cell in VBA or simply use
' Again, preferably fully qualified
Range("C" & Rows.Count).End(xlUp).Row
Range("$N$9").Address gives exactly "$N$9".
Range("N9").Address gives the same. Thus, it is a bit overwork. Check out the first two debug.print in the sample below.
Thus, once you calculate the last row and assign value to it lngLast, it is possible to get the formula like this:
"=SUM(N9:N" & lngLast & ")"
Option Explicit
Public Sub TestMe()
Dim strA As String
Dim lngLast As Long
strA = Range("$N$9").Address
Debug.Print strA = "$N$9"
strA = Range("N9").Address
Debug.Print strA = "$N$9"
lngLast = Range("N" & Rows.Count).End(xlUp).Row - 2
ActiveCell.Formula = "=SUM(N9:N" & lngLast & ")"
End Sub
Good morning, everyone :)