Creating a range based on values present in column.

efijal

New Member
Joined
Dec 3, 2018
Messages
1
Hello, I am trying to insert formulas into cells with VBA by using the following formula:

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Range(Range(startcell), Range(endcell)).Offset(0, 1).FormulaR1C1 = "=2+2"</code>

The range I want the formula to be inserted into are only the rows in which there is a value present in Column B. These values are in format QF1, QF2, QF3....
So sometimes there will be 10 rows and sometimes there will be 100. So Lets say there are QF values in rows 10 to 80, so I want the script to insert different formulas (=2+2 used as example) in column D,E,F,G, but only within that range.

I've been trying to use something like the script below to find and save that range, and then the command above to insert it in the column nearby :

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Dim startcell As Range
Dim endcell As Range
Range0 = "B1:B90"

Dim rng1 As Range, cell1 As Range
Set rng1 = Range(Range0)

For Each cell1 In rng1
    If InStr(1, cell1, "QF") > 0 And started = False Then
        
        Set startcell = cell1
         started = True
    End If
        
    If InStr(1, cell1, "QF") > 0 And started = True Then
        Set endcell = cell1
        
    End If
Next cell1



Range(Range(startcell), Range(endcell)).Offset(0, 1).FormulaR1C1 = "=2+2"

</code>

This script is however not working and the formulas are not inserted into cells. I think the startcell and endcell values are not saved properly. Is someone able to help?

Kind Regards
Emil
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try
Code:
Range(startcell, endcell).Offset(0, 1).FormulaR1C1 = "=2+2"
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...on-values-in-selected-column.html#post5019283

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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