VBA Macro passing value to another worksheet

yirga

New Member
Joined
Apr 3, 2015
Messages
25
Please help me to correct the code. the programs run and gives the correct value but it over write the previous values of other cells. please see at the bottom the input. thanks.

Sub SumItems_V4XX()
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, 1) <> "house1" Then
j = j + 1
o(j, 4) = a(i, 2)
o(j, 6) = a(i, 4)
o(j, 8) = a(i, 6)


ElseIf a(i, 1) = "house1" And a(i + 1, 1) = "house2" Then
j = j + 1
o(j, 4) = a(i, 2) + a(i + 1, 2)
o(j, 6) = a(i, 3) + a(i + 1, 4)
o(j, 8) = a(i, 4) + a(i + 1, 6)
i = i + 1
Else
j = j + 1
o(j, 4) = a(i, 2)
o(j, 6) = a(i, 4)
o(j, 8) = a(i, 6)
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 - input
A B CDEFGH
arangecars888888
arangebuses768934
arangehouse1899089
arangehouse2459145
arangecars349255
arangebuses339345
arangehouse1789478
arangehouse2459545
arangecars559655
arangebuses5978
arangehouse1559855
arangehouse2559955
arangecars3410022
arangebuses1210111
arangehouse15510255
arangehouse25510355

<colgroup><col><col><col><col><col><col span="3"></colgroup><tbody>
</tbody>


Sheet2 - the result should be expected as the following - the code above should process sheet1 C, E, G to pass the value to Sheet2 D, F, H with out affecting or clearing other columns. I appreciate you for sharing your precious time and knowledge with me. thanks.
ABCDEFGH
LocationPriceOld Bal123Old Bal244Old Bal367
LocationPriceOld Bal251Old Bal347Old Bal4116
LocationPriceOld Bal327Old Bal448Old Bal571
LocationPriceOld Bal457Old Bal550Old Bal6122
LocationPriceOld Bal530Old Bal651Old Bal774
LocationPriceOld Bal663Old Bal753Old Bal8128
LocationPriceOld Bal733Old Bal854Old Bal977
LocationPriceOld Bal834Old Bal955Old Bal1078

<colgroup><col width="64" span="8" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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