Substraction of cell General type value

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Hi all,

I have two worksheets in different workbook, and when some conditions are satisfied, I will substract the value of Range("J" & cell.Row) in desSheet by the value of Range("J" & cell.Row) in Pr worksheet.
The variable "cell" indicates a selected cell.
The full version of code is as below:
Code:
With desSheet
        For Each cell In Range("F4", Range("F" & Rows.count).End(xlUp))
            For i = 1 To 22
                If cell.Value = Trans_Part(i) Then
                    'check whether quantity is the same
                    If .Range("J" & cell.Row).Value = Pr.Range("I" & i).Value Then
                        'delete the entire row
                        .Range("A" & cell.Row & ":N" & cell.Row).Delete
                    Else
                        'substract current quantity by the amount this row just submitted
                        .Range("J" & cell.Row).Value = CInt(.Range("J" & cell.Row).Value) - CInt(Pr.Range("I" & i).Value)
                    End If
                End If
            Next i
        Next cell
    End With

Now I receive an error message "Type mismatch" over the line:
Code:
.Range("J" & cell.Row).Value = CInt(.Range("J" & cell.Row).Value) - CInt(Pr.Range("I" & i).Value)

Could you pls help me to find out the bug and solve this problem?

Thank you very much!

yours sincerely,
lolo^^
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You are missing some dot qualifiers here:

Rich (BB code):
For Each cell In .Range("F4", .Range("F" & .Rows.count).End(xlUp))

And where have you set Pr?
 
Upvote 0
I don't think this is the problem, but you ought to avoid using words like cell for variable names - it's too close to a reserved word and can make code harder to read.

Just wondering, is cell 'cell' on sheet desSheet as well? From the code it would be whatever the currently active sheet is.
 
Upvote 0
Thank you so much Andrew,

I have added the dot. Then I tried to run my code, in the first two times, this function has no runtime error, but it didn't substract the value successfully. Then I tried the third time, the same "type mismatch" error prompt out.
Why is that?

Best regards,
lolo

You are missing some dot qualifiers here:

Rich (BB code):
For Each cell In .Range("F4", .Range("F" & .Rows.count).End(xlUp))

And where have you set Pr?
 
Upvote 0
Hi Weaver,
:stickouttounge:Thank you. I will take note of avoiding using some confusing variable name in future.
Actually I indeed lost some dot there. The cell "cell" should be in the desSheet.

Best regards,
lolo
I don't think this is the problem, but you ought to avoid using words like cell for variable names - it's too close to a reserved word and can make code harder to read.

Just wondering, is cell 'cell' on sheet desSheet as well? From the code it would be whatever the currently active sheet is.
 
Upvote 0
So, let's dissect it:

1. Red code refers to active sheet.
Code:
With desSheet
        For Each cell In [B][COLOR="Red"]Range("F4", Range("F" & Rows.count)[/COLOR][/B].End(xlUp))
        ........
        Next cell
    End With

2. This code refers to desSheet.
Code:
.Range("J" & cell.Row).Value = ...
....
.Range("A" & cell.Row & ":N" & cell.Row).Delete
...
.Range("J" & cell.Row).Value = CInt(.Range("J" & cell.Row).Value)

3. This code refers to some sheet.
Code:
Pr.Range("I" & i).Value

So, are you sure that you refer to correct sheets?
 
Upvote 0
Re: Subtraction of cell General type value

Hi Sektor,
Actually just like what Andrew said, I need to add dots for
Rich (BB code):
 For Each cell In .Range("F4", .Range("F" & .Rows.count).End(xlUp))

The full version now should be:
Rich (BB code):
With desSheet
        For Each c In .Range("F4", .Range("F" & .Rows.count).End(xlUp))
            For i = 1 To 22
                If c.Value = Trans_Part(i) Then
                    'check whether quantity is the same
                    If .Range("J" & c.Row).Value = Pr.Range("I" & i).Value Then
                        'delete the entire row
                        .Range("A" & c.Row & ":N" & c.Row).Delete
                    Else
                        'subtract current quantity by the amount this row just submitted
                        .Range("J" & c.Row).Value = CInt(.Range("J" & c.Row).Value) - CInt(Pr.Range("I" & i).Value)
                    End If
                End If
            Next i
        Next c
    End With
There's still same error...

Best regards,
lolo
So, let's dissect it:

1. Red code refers to active sheet.
Code:
With desSheet
        For Each cell In [B][COLOR=red]Range("F4", Range("F" & Rows.count)[/COLOR][/B].End(xlUp))
        ........
        Next cell
    End With

2. This code refers to desSheet.
Code:
.Range("J" & cell.Row).Value = ...
....
.Range("A" & cell.Row & ":N" & cell.Row).Delete
...
.Range("J" & cell.Row).Value = CInt(.Range("J" & cell.Row).Value)

3. This code refers to some sheet.
Code:
Pr.Range("I" & i).Value

So, are you sure that you refer to correct sheets?
 
Last edited:
Upvote 0
Then you try to convert non-numeric data into Integer here:

Code:
...CInt(.[COLOR="Red"]Range("J" & cell.Row)[/COLOR].Value) - CInt([COLOR="red"]Pr.Range("I" & i)[/COLOR].Value)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top