Solved

VBA - Loop through cells and multiply based on certain cells

Posted on 2016-10-21
13
29 Views
Last Modified: 2016-10-21
Need VBA to do the below calculations. The range however can be 100s of columns and 100s of row.                                     
So, I need to be able to loop through.                                     
                                    
  (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.


I found the below code; however, I don't know what to do to get it to loop through the ranges and apply the formula based on row and column.

Sub LoopRange()

    Dim rCell As Range
    Dim rRng As Range

   
    ActiveSheet.Range("C16:C25").Select

    For Each rCell In Selection
       If Not IsError(rCell) Then rCell = rCell * "don't now what to do here"
    Next rCell

End Sub
0
Comment
Question by:MATO0618
  • 8
  • 5
13 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41854491
Not quite sure what you need.  Your 'formulas' all point to the same row pars, even though they are on many rows.
0
 

Author Comment

by:MATO0618
ID: 41854503
I see the mistake I made.  It shouldn't have been the same pars.

 (A$1*A3)       (B$1*B3)       (C$1*C3)       etc.
 (A$1*A4)       (B$1*B4)       (C$1*C4)       etc.
 (A$1*A5)       (B$1*B5)       (C$1*C5)       etc.
 (A$1*A6)       (B$1*B6)       (C$1*C6)       etc.
 (A$1*A7)       (B$1*B7)       (C$1*C7)       etc.
 (A$1*A8)       (B$1*B8)       (C$1*C8)       etc.
 (A$1*A9)       (B$1*B9)       (C$1*C9)       etc.
 (A$1*A10)       (B$1*B10)       (C$1*C10)       etc.
 (A$1*A11)       (B$1*B11)       (C$1*C11)       etc.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41854592
Do you want the formulas or just the values?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41854602
This would populate the A16:K25 cells.
activesheet.range("A16").formula="=A$1*A3"
activesheet.range("A16:A25").filldown
activesheet.range("A16:K25").fillright

Open in new window


It would be easy enough to convert them from formulas to values.
activesheet.range("A16:K25").value = activesheet.range("A16:K25").value

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 41854604
I don't really see the need for looping
0
 

Author Comment

by:MATO0618
ID: 41854739
i didn't explain it clearly. So, hopefully this try at an explanation is better.

I've attached a worksheet where you can see original and what I eventually need to happen.

NOTE: The range of course would be much larger than this example.

I created a crude macro, but this would be very cumbersome if I had to do it that way.
Multiple-with-VBA-across-Range.xlsm
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Expert Comment

by:aikimark
ID: 41854768
Try this:
1. Select the first/top row of cells
2. Ctrl+C
3. Select all the cells below the top row
4. Paste Special (Values, Multiply)

Let me know if that gives you the values you want.
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 41854770
If that works, this is the automated equivalent.
Sub Q_28978063()
    Dim rng As Range
    Set rng = ActiveSheet.Range("C1:H1")
    rng.Copy
    Set rng = rng.Offset(1)
    Set rng = ActiveSheet.Range(rng, rng.End(xlDown))
    rng.PasteSpecial xlPasteValues, xlPasteSpecialOperationMultiply
End Sub

Open in new window

0
 

Author Comment

by:MATO0618
ID: 41854780
I tried the automated equivalent and it works partially. But, the difference is that the first row values are absolute values.

So the output is not correct in this solution.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41854782
the first row values are absolute values
What does that mean?

Was your posted workbook a representative example of the data?
0
 

Author Comment

by:MATO0618
ID: 41854786
Yes it was a representative example of the data.

All values are multiplied by the first row. In the last solution, the past copy moves it down, and thereby the next row would multiply by the second row and so forth.

Range("C2").Value = Evaluate("C2*$C$1")
Range("D2").Value = Evaluate("D2*$D$1")
Range("E2").Value = Evaluate("E2*$E$1")
Range("F2").Value = Evaluate("F2*$F$1")
Range("G2").Value = Evaluate("G2*$G$1")
Range("H2").Value = Evaluate("H2*$H$1")

Range("C3").Value = Evaluate("C3*$C$1")
Range("D3").Value = Evaluate("D3*$D$1")
Range("E3").Value = Evaluate("E3*$E$1")
Range("F3").Value = Evaluate("F3*$F$1")
Range("G3").Value = Evaluate("G3*$G$1")
Range("H3").Value = Evaluate("H3*$H$1")
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41854788
when I tested the code, it gave the same values as you posted in your posted workbook.
0
 

Author Closing Comment

by:MATO0618
ID: 41854791
Yeah...... you're correct. I had inadvertently wasn't using apples to apples when I tested on the original data.

Thanks for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now