Macro - Loop Question

mojo_123

New Member
Joined
Jul 8, 2013
Messages
5
Good Evening,

First off, I'd like to thank everyone for answering VB related questions. With your help I was able to familiarize myself somewhat with VB.
Since this is my first post, I'd like to apologize if this question has already been asked. I searched on Google as well as this forum's own search engine, but failed to come up with a solution.

I'm in a situation that requires filtering. But since i was unable to successfully execute a macro that selects the first cell and pastes formula, I tried another method.

What I'm trying to do is basically create a condition when met copies a cell (with Formulas or just the formula) and replaces two other cells in the same row.

So far this is what I came up with:

Dim rng As Range
Sheets("DAILY").Select
Range("X10").Select
Range(Range("X10"), Range("X100000").End(xlDown)).Select

For Each rng In Selection


If ActiveCell.Text = "True" Then
ActiveCell.FormulaR1C1 = "=$P8/$L8"
ActiveCell.FormulaR1C1 = "=$M8*(1-VLOOKUP($C8,$Q$1:$R$6,2,0))"
ActiveCell.End(xlDown).Activate

End If

Next rng


But i keep getting errors trying to execute the macro.

Can anyone help me or provide me with advise as to what would be an efficient way?

Thanks,
Mojo
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You might improve your chances of getting help if you provide some more information about what you want to accomplish and details about the layout of data on the sheet. Your code is placing a formula in the active cell if its value is "True" and then immediately overwriting that formula with a different formula - probably not what you intended.
 
Upvote 0
Thanks for the quick reply. You're right, thats not exactly what i intended.

So here's a summary of what I'm looking at.

ABCDEfegp%GHIJKLM
1
2#DIV/0!#N/AFALSE
3QtySalesFegpunitcostfegp%seasonField2DeptField3
44$116.00 #N/A#N/A#N/A#N/A4#N/AFALSE
51$6.94 ($0.13)7.077.07-0.0187317.07FALSE
640$960.00 #N/A#N/A#N/A#N/A40#N/AFALSE
7-2($78.00)($43.07)17.46286-34.92570.552234-2-34.9257FALSE
8
- If column M4=True then copy (cell E4 and F4 or "=$Q2/$M2" or "=$N2*(1-VLOOKUP($D4,$R$1:$S$7,2,0))" formula to column E4 and F4
- If False, move on to the next cell (ie, N5)

<colgroup><col width="64" span="14" style="width:48pt"> </colgroup><tbody>
</tbody>
i hope that helps.
 
Upvote 0
Try this on a copy of your sheet (untested) - not clear to me if the formula in column E is meant to be absolute or relative.
Code:
Sub mojo_123()
Dim lR As Long, c As Range
lR = Range("M" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Range("M4", "M" & lR)
    If c.Value = True Then
        c.Offset(, -8).Formula = "=$Q2/$M2"
        c.Offset(, -7).Formula = "=$N2*(1-VLOOKUP($D" & c.Row & ",$R$1:$S$7,2,0))"
    End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Hi Joe,

I modified the formula a bit to fit these cells:

6NPPQRSTUVWX
7
8#N/A#N/AFALSE
9Fegpcost/unitcostfegp%seasonField2DeptField3
10#N/A#N/A#N/A#N/A4#N/A#N/A
11($0.13)7.077.07-0.0187317.07FALSE

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

Dim lR As Long, c As Range
lR = Range("X" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Range("X10", "X" & lR)
If c.Value.Text = True Then
c.Offset(, -9).Formula = "=$p2/$L2"
c.Offset(, -8).Formula = "=$M2*(1-VLOOKUP($C" & c.Row & ",$P$1:$R$6,2,0))"
End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

But i keep getting stuck at " If c.Value.Text = True Then " --> Identifier under cursor is not recognized.

In regards to the formula in column E. Its supposed to be relative. So would it be efficient for me to copy and paste the formula or place it (like we have now).

Thanks again for your help.
 
Upvote 0
Change this:
If c.Value.Text = True Then
to this:
If c.Value = True Then

If you want the formula in E to be relative to the row then use this:
c.Offset(, -9).Formula = "=$p" & c.Row & "/$L" & c.Row
 
Upvote 0
It still keeps giving the same error message:

Run-Time Error '13':
Type Mismatch


Dim lR As Long, c As Range
lR = Range("X" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each c In Range("X10", "X" & lR)
If c.Value = True Then
c.Offset(, -9).Formula = "=$p2/$L2"
c.Offset(, -8).Formula = "=$M2*(1-VLOOKUP($C" & c.Row & ",$P$1:$R$6,2,0))"
End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
 
Upvote 0
I can't reproduce that error. There's nothing I can see that would cause a type mismatch error at that line - it runs fine for me. Check that line for typos or retype it.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,975
Members
449,200
Latest member
Jamil ahmed

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