Loops are not working in my code

amitkrb

New Member
Joined
Jul 31, 2012
Messages
6
Hi friends,
I am a beginner to VBA. I wrote a code to basically compare two sheets of data by their brand name in column A. When they match, I want to subtract the corresponding values of sheet 1 and sheet 2 for same brand. I am using this code.
Please help , it is giving a Next without For error

The code is:-
Code:
Option Explicit

Global Length1 As Integer, Length2 As Integer, i As Integer, j As Integer, k As Integer, Diff As Long








Public Sub mainQC()


i = 1
j = 1
k = 1


Length1 = Workbooks("Book1").Sheets("sheet1").Range("strtpoint1").End(xlDown).Row


Length2 = Workbooks("Book1").Sheets("sheet2").Range("strtpoint2").End(xlDown).Row
For i = 1 To Length1


For j = 1 To Length2


If Trim(StrConv(Workbooks("Book1").Sheets("sheet1").Range("A" & i).Value, vbProperCase)) = _
Trim(StrConv(Workbooks("Book1").Sheets("sheet2").Range("A" & j).Value, vbProperCase)) _
Then




Workbooks("Book1").Sheets("sheet3").Range("A" & k).Value _
 = Workbooks("Book1").Sheets("sheet1").Range("A" & i).Offset(0, k).Value - _
Workbooks("Book1").Sheets("sheet2").Range("A" & j).Offset(0, (k + 1)).Value


Exit For


Next i




End If
















End Sub

Any help would be appreciated .. Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Maybe

Code:
Global Length1 As Integer, Length2 As Integer, i As Integer, j As Integer, k As Integer, Diff As Long
Public Sub mainQC()
i = 1
j = 1
k = 1
Length1 = Workbooks("Book1").Sheets("sheet1").Range("strtpoint1").End(xlDown).Row
Length2 = Workbooks("Book1").Sheets("sheet2").Range("strtpoint2").End(xlDown).Row
For i = 1 To Length1
    For j = 1 To Length2
        If Trim(StrConv(Workbooks("Book1").Sheets("sheet1").Range("A" & i).Value, vbProperCase)) = _
            Trim(StrConv(Workbooks("Book1").Sheets("sheet2").Range("A" & j).Value, vbProperCase)) _
            Then
            Workbooks("Book1").Sheets("sheet3").Range("A" & k).Value _
             = Workbooks("Book1").Sheets("sheet1").Range("A" & i).Offset(0, k).Value - _
            Workbooks("Book1").Sheets("sheet2").Range("A" & j).Offset(0, (k + 1)).Value
            Exit For
        End If
    Next j
Next i
End Sub
 
Upvote 0
Hi,
What I was trying to do is this:
Sheet 1:-
ABC121511
DEF232142
JKL134533

<tbody>
</tbody>



Sheet 2:
DEF1433111
ABC123312
JKL113412

<tbody>
</tbody>



I was basically trying to take sheet 1 as reference and subtract corresponding values of ABC, DEF, JKL in sheet 1 and 2 and paste the difference in sheet 3. For example for ABC, it would be
ABC 0 12 -30

The nth column of Sheet 1 is substracted from the (n+1)th column of sheet 2.

Please help!!
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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