Increase range by 2

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
608
Office Version
  1. 365
Platform
  1. Windows
Hello,

I want to add this formula starting at B1 and paste it down 23 rows. However, I want DD1!B3 to increase to DD1!B5 for the second row, DD1!B7 to the Third and so on.

VBA Code:
Worksheets("Sheet1").Range("B1") = "=IFERROR(MID('DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1)-FIND(""000"","" "" &'DD1'!B3)+13),IFERROR(MID('DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1)-FIND(""100"","" "" &'DD1'!B3)+13),""""))"

Range("B1").Copy

Range("B2:B23).PasteSpecial Paste:=xlFormulas, operation:=xlNone, skipblanks _

:=False, Transpose:=False

Any directions or help is appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
One way.
VBA Code:
    Dim R As Range, FStr As String, I As Long, N As Long
    
    Set R = Worksheets("Sheet1").Range("B1")
    FStr = "=IFERROR(MID('DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1)-FIND(""000"","" "" &'DD1'!B3)+13),IFERROR(MID('DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1)-FIND(""100"","" "" &'DD1'!B3)+13),""""))"
    R.Formula = FStr
    
    N = 5
    For I = 1 To 22
        With R.Offset(I)
            .Formula = Replace(FStr, "B3", "B" & N)
            N = N + 2
        End With
    Next I
 
Upvote 0
Solution
One way.
VBA Code:
    Dim R As Range, FStr As String, I As Long, N As Long
   
    Set R = Worksheets("Sheet1").Range("B1")
    FStr = "=IFERROR(MID('DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1)-FIND(""000"","" "" &'DD1'!B3)+13),IFERROR(MID('DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1)-FIND(""100"","" "" &'DD1'!B3)+13),""""))"
    R.Formula = FStr
   
    N = 5
    For I = 1 To 22
        With R.Offset(I)
            .Formula = Replace(FStr, "B3", "B" & N)
            N = N + 2
        End With
    Next I
It works perfectly! Thank you so much.
 
Upvote 0
If I wanted to add another formula starting in F1 following the same logic, can I nest it with the For next loop or do I need to create another loop?

VBA Code:
Set R = Worksheets("Sheet1").Range("B1")
Set S = Worksheets("Sheet1").Range("F1")

FSts ="=TRIM(MID('DD1'!B3,FIND(""#"",SUBSTITUTE('DD1'!B3,"" "",""#"",LEN('DD1'!B3)-LEN(SUBSTITUTE('DD1'!B3,"" "",""""))-3)),FIND("" "",'DD1'!B3,FIND(""#"",SUBSTITUTE('DD1'!B3,"" "",""#"",LEN('DD1'!B3)-LEN(SUBSTITUTE('DD1'!B3,"" "",""""))-3))+1)-FIND(""#"",SUBSTITUTE('DD1'!B3,"" "",""#"",LEN('DD1'!B3)-LEN(SUBSTITUTE('DD1'!B3,"" "",""""))-3))))"

R.Formula = FStr

S.Formula = FSts

N = 5

For I = 1 To 22

With R.Offset(I)

.Formula = Replace(FStr, "B3", "B" & N)

.Formula = Replace(FSts, "B3", "B" & N)

N = N + 2

End With

Next I
 
Upvote 0
Maybe something like this?

With R.Offset(I)

.Formula = Replace(FStr, "B3", "B" & N)

.Offset(0, 4).Formula = Replace(FSts, "B3", "B" & N)

N = N + 2

End With
 
Upvote 0
Assuming you want the same formula using column B in both places, the above might work, but you would have to test it to be sure. Personally I think it would be cleaner to just add a 2nd loop. The extra CPU time is negligible.

VBA Code:
Dim R As Range, FStr As String, I As Long, N As Long
   
    'Col B
    Set R = Worksheets("Sheet1").Range("B1")
    FStr = "=IFERROR(MID('DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""000"","" ""&'DD1'!B3)-1)-FIND(""000"","" "" &'DD1'!B3)+13),IFERROR(MID('DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1,FIND("" "",'DD1'!B3,FIND(""100"","" ""&'DD1'!B3)-1)-FIND(""100"","" "" &'DD1'!B3)+13),""""))"
    R.Formula = FStr
   
    N = 5
    For I = 1 To 22
        With R.Offset(I)
            .Formula = Replace(FStr, "B3", "B" & N)
            N = N + 2
        End With
    Next I
   
    'Col F
    Set R = Worksheets("Sheet1").Range("F1")
    R.Formula = FStr
   
    N = 5
    For I = 1 To 22
        With R.Offset(I)
            .Formula = Replace(FStr, "B3", "B" & N)
            N = N + 2
        End With
    Next I
 
Upvote 0
I did a quick test using my version and it worked but I like your version better as it easier to debug.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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