Thread: mismatch error
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Barb Reinhardt Barb Reinhardt is offline
external usenet poster
 
Posts: 3,355
Default mismatch error

Look at your range definitions. You never change the columns in them.

FYI, the convention for most posters in this newsgroup is to top post. The
thread gets very confusing when some top post and some bottom post.



" wrote:

On Jul 24, 9:16 am, Barb Reinhardt
wrote:
Try this

Sub testt()
Dim rang1 As Range
Dim rang2 As Range
Dim m As Integer
Dim lRow As Long
Dim lCol As Long

For lCol = 7 To 7
For m = 0 To 60 Step 4
'Not sure what you want to do with columns in ranges
Set rang1 = Range(Cells(2 + m, 3), Cells(4 + m, 4))
Set rang2 = Range(Cells(6 + m, 3), Cells(8 + m, 4))
If WorksheetFunction.Sum(rang1) = WorksheetFunction.Sum(rang2) Then
For lRow = 2 To 4
'Changed 7 to lCol
Cells(lRow + m, lCol) = Cells(lRow + m, lCol) + Cells(lRow +
4 + m, lCol)
Next lRow
End If
Next
Next lCol
End Sub



" wrote:
On Jul 23, 4:14 pm, Barb Reinhardt
wrote:
Ignore my previous response.


Sub test()
Dim rang1 As Range
Dim rang2 As Range


Set rang1 = Range("C18:C20")
Set rang2 = Range("C22:C24")
If WorksheetFunction.Sum(rang1) = WorksheetFunction.Sum(rang2) Then
Cells(18, 7) = Cells(18, 7) + Cells(22, 7)
Cells(19, 7) = Cells(19, 7) + Cells(23, 7)
Cells(20, 7) = Cells(20, 7) + Cells(24, 7)
End If
End Sub


" wrote:
I am currently trying to look at C18:C20 and compare that to C22:C24.
And if they are the same then do C18+C22, C19+C23 and C20+C24. But it
keeps erroring saying mismatch. I've tried to reduce the range(to only
2 rows as shown in the code below) but the only time it will work is
if I just do one cell at a time but I need the macro to look at the
group.


Sub test()
Dim rang1 As Range
Dim rang2 As Range


Set rang1 = Range(Cells(18, 3), Cells(19, 3))
Set rang2 = Range(Cells(22, 3), Cells(23, 3))
If rang1 = rang2 Then Cells(18, 7) = (Cells(18, 7) + Cells(22, 7))
If rang1 = rang2 Then Cells(19, 7) = (Cells(19, 7) + Cells(23, 7))
If rang1 = rang2 Then Cells(20, 7) = Cells(20, 7) + Cells(24, 7)
End
End Sub- Hide quoted text -


- Show quoted text -


So Thank you very much it did work. However, usually after I get the
basic code I can create the loop with no problem but I'm having
difficulty this time. This is what I have so far.


Sub testt()
Dim rang1 As Range
Dim rang2 As Range
Dim m As Integer
For m = 0 To 60 Step 4
Set rang1 = Range(Cells(2 + m, 3), Cells(4 + m, 4))
Set rang2 = Range(Cells(6 + m, 3), Cells(8 + m, 4))
If WorksheetFunction.Sum(rang1) = WorksheetFunction.Sum(rang2) Then
Cells(2 + m, 7) = Cells(2 + m, 7) + Cells(6 + m, 7)
Cells(3 + m, 7) = Cells(3 + m, 7) + Cells(7 + m, 7)
Cells(4 + m, 7) = Cells(4 + m, 7) + Cells(8 + m, 7)


End If
Next
End Sub


I also want the column part to be looping also but I can't get the row
part. What happens is sometimes they add sometime they don't but I
haven't figured out why some do and some don't.- Hide quoted text -


- Show quoted text -


Sub testft()
Dim rang1 As Range
Dim rang2 As Range
Dim m As Integer
Dim lRow As Long
Dim lCol As Long


For lCol = 7 To 70 Step 7
For m = 0 To 60 Step 4
'Not sure what you want to do with columns in ranges
Set rang1 = Range(Cells(2 + m, 3), Cells(4 + m, 4))
Set rang2 = Range(Cells(6 + m, 3), Cells(8 + m, 4))
If WorksheetFunction.Sum(rang1) = WorksheetFunction.Sum(rang2)
Then
For lRow = 2 To 4
'Changed 7 to lCol
Cells(lRow + m, lCol) = Cells(lRow + m, lCol) +
Cells(lRow + 4 + m, lCol)
Next lRow
End If
Next
Next lCol
End Sub
this is with what I want with the columns but it still does the same
thing. Basically acts as if the condition part (rang1=rang2) is not
even there.