VBA multiply all cells in range with corresponding row value

milanso

New Member
Joined
May 17, 2017
Messages
4
Hi, I'm trying the following,
I have the below data which needs to be copied below this data and each cell if larger than 0 in columns b-e multiplied with the corresponding value in column a.
a
b
c
d
e
1.02
2373580715
0.35
10120
0
0.71
51000
1.216834810612
0.6770450101002
1.07372325300
650
1.0735032555
650

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

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello milanso,

You can try this formula

=IF(B2>0,B2*$A2,"smaller than 0")

You can change the text "smaller than 0" by whatever you want to happen if the value is smaller than 0.
 
Upvote 0
o well! my bad

Code:
Sub test()Dim r As Range


For Each r In Range("A1:E7")
    If r > 0 Then
        r.Offset(7, 0) = r * r.Offset(0, r.column() - 1)
    End If
Next


End Sub
 
Last edited:
Upvote 0
Hi,

I'm pretty new with VBA and for practise reasons I gave your question a try as well. I made a code that you can still use if you expand your Table.
Code:
Sub Test()
Range("A2").Select
     Range(Selection, Selection.End(xlDown)).Select
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
For MyRow = 2 To Range("A" & Rows.Count).End(xlUp).Row
For mycol = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
i = Range("A" & Rows.Count).End(xlUp).Row
If Cells(MyRow, mycol) > 0 Then
Cells(MyRow + i, mycol).Value = Cells(MyRow, mycol) * Cells(MyRow, 1)
Else: Cells(MyRow + i, mycol).Value = 0
End If
Next mycol
Next MyRow

End Sub

I've run it and it seems to work. Tips are Always welcome.

Gr.
 
Upvote 0
hum, i think that your code could have some modification. I will modify some part in red and put some comment.

Code:
Sub Test()
[COLOR=#ff0000]Range("A2").Select 'why select this?[/COLOR]
     [COLOR=#ff0000]Range(Selection, Selection.End(xlDown)).Select 'the way to select on a line than using selection after is ... slowing your code and moving your mouse[/COLOR]
    [COLOR=#ff0000]Range(Selection, Selection.End(xlDown))[/COLOR].Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False 'However, I don't understand why you are replacing "." by "," in french we use comma for decimal, but in Excel in english, it's a dot
        
For MyRow = 2 To Range("A" & Rows.Count).End(xlUp).Row
For mycol = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
i = Range("A" & Rows.Count).End(xlUp).Row
If Cells(MyRow, mycol) > 0 Then
Cells(MyRow + i, mycol) = Cells(MyRow, mycol) * Cells(MyRow, 1) 'I deleted .value because this attribute is the one used by default in VBA, no use to have it
Else 
Cells(MyRow + i, mycol) = 0 'You should but your else part on another line
End If
Next mycol
Next MyRow

End Sub
 
Last edited:
Upvote 0
Hi, thanks for responding!

I've got the dutch version from excel. Maybe that's why I have to change the "." to ",". But I've changed it now to:
Code:
   Range("A1", Selection.End(xlDown)).Select
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

Thanks for your constructive help!
If you have more tips for me they're very welcome!
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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