Change to code offsets

SUSAN BAXTER

New Member
Joined
Apr 1, 2019
Messages
47
Hi


I have code that I would like to make a small change to but need some help with. I only need the first cell in all the ranges in all the “arr1” in my code below to be increased by one row , so the 1st change is the current range is Range("D22:I36") change to Range("E22:I36"). I can do this on my own but I’m having trouble with the changes needed in the offsets (believe that’s the correct terminology) in the code. There is a lot going on in that code and I’m no expert like you are.
No changes are needed in arr2.
Thanks so much for any help.



Code:
Sub PlaceNumbers()

Dim c As Range, rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
Dim last1 As Long, last2 As Long, rtar As Long, xtar As Long
 
Application.ScreenUpdating = False

With ActiveSheet
    'create arrays
    arr1 = Array(.Range("D22:I36"), .Range("J22:O36"), .Range("P22:U36"), .Range("V22:AA36"), .Range("AB22:AG36") _
        , .Range("AH22:AM36"), .Range("AN22:AS36"), .Range("AT22:AY36"), .Range("AZ22:BE36"), .Range("BF22:BK36"))
    arr2 = Array(.Range("BN22:BQ36"), .Range("BR22:BU36"), .Range("BV22:BY36"), .Range("BZ22:CC36"), .Range("CD22:CG36") _
        , .Range("CH22:CK36"), .Range("CL22:CO36"), .Range("CP22:CS36"), .Range("CT22:CW36"), .Range("CY22:DA36"))                                                                '
    'loop through arrays
    For i = LBound(arr1) To UBound(arr1)
        Set rng1 = arr1(i)
        Set rng3 = arr2(i)                                                          '
        last1 = .Cells(.Rows.Count, ColLetter(rng1.Columns(1).Column)).End(xlUp).Row
        last2 = .Cells(.Rows.Count, ColLetter(rng3.Columns(1).Column)).End(xlUp).Row
 
        For Each c In rng1.Offset(1, 2).Resize(, 1)
            If c <> "" Then
                rtar = Evaluate("=MATCH(" & ColLetter(rng1.Columns(2).Column) & rng1.Row & "&" & ColLetter(rng1.Columns(3).Column) & rng1.Row & "," & ColLetter(rng3.Columns(1).Column) & "1:" & ColLetter(rng3.Columns(1).Column) & last2 & "&" & ColLetter(rng3.Columns(3).Column) & "1:" & ColLetter(rng3.Columns(3).Column) & last2 & ",0)")
                xtar = Application.Match(c.Offset(0, -2), Range(ColLetter(rng3.Columns(1).Column) & rtar & ":" & ColLetter(rng3.Columns(1).Column) & last2), 0)
                With Application.WorksheetFunction
                    c.Offset(0, 1) = .Index(Range(ColLetter(rng3.Columns(2).Column) & rtar & ":" & ColLetter(rng3.Columns(2).Column) & last2), xtar)
                    c.Offset(0, 2) = .Index(Range(ColLetter(rng3.Columns(3).Column) & rtar & ":" & ColLetter(rng3.Columns(3).Column) & last2), xtar)
                    c.Offset(0, 3) = .Index(Range(ColLetter(rng3.Columns(4).Column) & rtar & ":" & ColLetter(rng3.Columns(4).Column) & last2), xtar)
                End With
            End If
        Next c
    Next
End With

Application.ScreenUpdating = True
 
End Sub
 
Function ColLetter(Collet As Integer) As String

ColLetter = Split(Cells(1, Collet).Address, "$")(1)

End Function 

 
  [SIZE=3]<style type="text/css">pre { direction: ltr; color: rgb(0, 0, 0); text-align: left; }pre.western { font-family: "Liberation Mono", serif; }pre.cjk { font-family: "WenQuanYi Micro Hei Mono"; }pre.ctl { font-family: "Liberation Mono"; }p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 115%; text-align: left; }p.western { font-family: "Liberation Serif", serif; font-size: 12pt; }p.cjk { font-family: "WenQuanYi Micro Hei"; font-size: 12pt; }p.ctl { font-family: "Lohit Devanagari"; font-size: 12pt; }a:link { }</style>

[/SIZE]

  <style type="text/css">p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 115%; text-align: left; }p.western { font-family: "Liberation Serif", serif; font-size: 12pt; }p.cjk { font-family: "WenQuanYi Micro Hei"; font-size: 12pt; }p.ctl { font-family: "Lohit Devanagari"; font-size: 12pt; }a:link { }</style>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello Susan:

So let me see if I understand what you want to do...Your first Array (arr1), you want all the first rows to increase by one row so range D22:I36 goes to E22:I36 and J22:O36 goes to K22:O36 and so on? So you will be losing a row in each array range in arr1. Is that correct? If that is correct, I think this should work...


Code:
Sub PlaceNumbers()

Dim c As Range, rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
Dim last1 As Long, last2 As Long, rtar As Long, xtar As Long
 
Application.ScreenUpdating = False


With ActiveSheet
    'create arrays
    arr1 = Array(.Range("E22:I36"), .Range("K22:O36"), .Range("Q22:U36"), .Range("W22:AA36"), .Range("AC22:AG36") _
        , .Range("AI22:AM36"), .Range("AO22:AS36"), .Range("AU22:AY36"), .Range("BA22:BE36"), .Range("BG22:BK36"))
    arr2 = Array(.Range("BN22:BQ36"), .Range("BR22:BU36"), .Range("BV22:BY36"), .Range("BZ22:CC36"), .Range("CD22:CG36") _
        , .Range("CH22:CK36"), .Range("CL22:CO36"), .Range("CP22:CS36"), .Range("CT22:CW36"), .Range("CY22:DA36"))                                                                '
    'loop through arrays
    For i = LBound(arr1) To UBound(arr1)
        Set rng1 = arr1(i)
        Set rng3 = arr2(i)                                                          '
        last1 = .Cells(.Rows.Count, ColLetter(rng1.Columns(1).Column)).End(xlUp).Row
        last2 = .Cells(.Rows.Count, ColLetter(rng3.Columns(1).Column)).End(xlUp).Row
 
        For Each c In rng1.Offset(1, 2).Resize(, 1)
            If c <> "" Then
                rtar = Evaluate("=MATCH(" & ColLetter(rng1.Columns(2).Column) & rng1.Row & "&" & ColLetter(rng1.Columns(3).Column) & rng1.Row & "," & ColLetter(rng3.Columns(1).Column) & "1:" & ColLetter(rng3.Columns(1).Column) & last2 & "&" & ColLetter(rng3.Columns(3).Column) & "1:" & ColLetter(rng3.Columns(3).Column) & last2 & ",0)")
                xtar = Application.Match(c.Offset(0, -2), Range(ColLetter(rng3.Columns(1).Column) & rtar & ":" & ColLetter(rng3.Columns(1).Column) & last2), 0)
                With Application.WorksheetFunction
                    c.Offset(0, 1) = .Index(Range(ColLetter(rng3.Columns(2).Column) & rtar & ":" & ColLetter(rng3.Columns(2).Column) & last2), xtar)
                    c.Offset(0, 2) = .Index(Range(ColLetter(rng3.Columns(3).Column) & rtar & ":" & ColLetter(rng3.Columns(3).Column) & last2), xtar)
                    c.Offset(0, 3) = .Index(Range(ColLetter(rng3.Columns(4).Column) & rtar & ":" & ColLetter(rng3.Columns(4).Column) & last2), xtar)
                End With
            End If
        Next c
    Next
End With


Application.ScreenUpdating = True
 
End Sub
 
Function ColLetter(Collet As Integer) As String


ColLetter = Split(Cells(1, Collet).Address, "$")(1)


End Function
 
Upvote 0
disregard. not relevant.
 
Last edited:
Upvote 0
Hi

My apologies, I should have just listed the changes up front.

arr2 – does not need to be changed.

The range changes to arr1 are as follows:
arr1 = Array(.Range("E22:I36"), .Range("J22:N36"), .Range("O22:S36"), .Range("T22:X36"), .Range("Y22:AC36") _
, .Range("AD22:AH36"), .Range("AI22:AM36"), .Range("AN22:AR36"), .Range("AS22:AW36"), .Range("AX22:BB36"))


<style type="text/css">pre { direction: ltr; color: rgb(0, 0, 0); text-align: left; }pre.western { font-family: "Liberation Mono", serif; }pre.cjk { font-family: "WenQuanYi Micro Hei Mono"; }pre.ctl { font-family: "Liberation Mono"; }p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 115%; text-align: left; }p.western { font-family: "Liberation Serif", serif; font-size: 12pt; }p.cjk { font-family: "WenQuanYi Micro Hei"; font-size: 12pt; }p.ctl { font-family: "Lohit Devanagari"; font-size: 12pt; }a:link { }</style>
 
Last edited:
Upvote 0
The only Offset that I think would be affected is this one.

Code:
For Each c In rng1.Offset(1, 2).Resize(, 1)
Since you modified all of your array ranges to start one column to the right, then if you change the 2 to a 1 in the For statement it should then reference the same range that it did before.
Code:
For Each c In rng1.Offset(1, 1).Resize(, 1)

If not, post back with the results and what they should have been, then we can make any necessary adjustments. It would be much better if we could see the workbook in a link to a share server.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,790
Members
448,994
Latest member
rohitsomani

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