Hello Group,
I need help. when this  code runs , The result goes to Sheet2 as intended. 
however it overwrite A, B, C E, G. of Sheet2 columns. you help to stop 
overwriting .I Appreciate your help

Sub SheetA()
Dim w1 As Worksheet, w2 As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
a = w1.Range("A1:H" & w1.Range("A" & Rows.Count).End(xlUp).Row)
ReDim o(1 To UBound(a, 1), 1 To 8)
For i = LBound(a, 1) To UBound(a, 1)
  If a(i, 2) <> "Red apple" Then
    j = j + 1
    o(j, 4) = a(i, 3)
    o(j, 6) = a(i, 5)
    o(j, 8) = a(i, 7)
    
 
  ElseIf a(i, 2) = "Red apple" And a(i + 1, 2) = "Green apple" Then
    j = j + 1
        o(j, 4) = a(i, 3) + a(i + 1, 3)
        o(j, 6) = a(i, 5) + a(i + 1, 5)
        o(j, 8) = a(i, 7) + a(i + 1, 7)
    i = i + 1
  Else
    j = j + 1
    o(j, 4) = a(i, 3)
    o(j, 6) = a(i, 5)
    o(j, 8) = a(i, 7)
  End If
Next i
With w2
  .Columns(1).ClearContents
  .Range("A1").Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).AutoFit
  .Activate
End With
Application.ScreenUpdating = True

End Sub


Sheet1
 A B C D E F G H  arange Test10 23 44 67  arange house1 25 46 69  arange 
house2 26 47 70  arange Test4 27 48 71  arange house1 28 49 72  arange 
house2 29 50 73  arange Test9 30 51 74  arange house1 31 52 75  arange 
house2 32 53 76  arange Test12 33 54 77  arange Test13 34 55 78


    Sheet2
   A B C D E F G  doNotOverite doNotOverite doNotOverite   doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite  doNotOverite doNotOverite doNotOverite doNotOverite 
doNotOverite 

-- 
Are you =EXP(E:RT) or =NOT(EXP(E:RT)) in Excel? And do you wanna be? It’s 
=TIME(2,DO:IT,N:OW) ! Join official Facebook page of this forum @ 
https://www.facebook.com/discussexcel

FORUM RULES

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.
2) Don't post a question in the thread of another member.
3) Don't post questions regarding breaking or bypassing any security measure.
4) Acknowledge the responses you receive, good or bad.
5) Jobs posting is not allowed.
6) Sharing copyrighted material and their links is not allowed.

NOTE  : Don't ever post confidential data in a workbook. Forum owners and 
members are not responsible for any loss.
--- 
You received this message because you are subscribed to the Google Groups "MS 
EXCEL AND VBA MACROS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to excel-macros+unsubscr...@googlegroups.com.
To post to this group, send email to excel-macros@googlegroups.com.
Visit this group at http://groups.google.com/group/excel-macros.
For more options, visit https://groups.google.com/d/optout.

Reply via email to