Array Problem

menschmaschine

New Member
Joined
Dec 21, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,

First time posting here (I think... was on an Excel forum years ago, but don't remember which). Recently got back into VBA and ran into a problem. Was hoping someone could help.

Two sheets in a workbook. Sheet 1 contains a column with cells containing the generic reference "[Unit]" in various cells (non-contiguous). Sheet 2 contains names of Units in a contiguous list in one column.

I need to copy the names of the Units from Sheet 2 and paste them in the cells that contain "[Unit]" on Sheet 1.

Here is the code I have and it is hanging up after the first one, i.e., it places the name of the first unit from Sheet 2 to the first occurrence of "[Unit]" on Sheet 1, but then gives a Subscript Out of Range error.

Help is greatly appreciated!
Excel Formula:
Sub UnitName()

Dim URng As Range, ULRow As Long, UCel As Range, u As Long
Dim RefRng As Range, RLRow As Long
Dim indx As Long, ary()

Sheets("Sheet 1").Select
ULRow = Cells(Rows.Count, "B").End(xlUp).Row
For u = 1 To ULRow
    If Cells(u, "B").value = "[Unit]" Then
        If URng Is Nothing Then
            Set URng = Cells(u, "B")
            Else
                Set URng = Union(URng, Cells(u, "B"))
        End If
    End If
Next u

RLRow = Sheets("Sheet 2").Range("AE" & Rows.Count).End(xlUp).Row
Set RefRng = Sheets("Sheets 2").Range("AE8:AE" & RLRow)

ary = RefRng

indx = 1
For Each UCel In URng
    UCel.value = ary(1, indx)
    indx = indx + 1
Next UCel

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
My guess is that's because in one place you have Sheet 2 and in the other, Sheets 2.

Cells(u, "B").value << you can do this? I thought cell reference syntax was letterNumber whereas you have B (for the column?) coming second.
 
Upvote 0
Welcome to the Forum (or welcome back ;) )
(@Micron - good spot didn't see that one.)

@menschmaschine, In addition to that, you don't mention which row is highlighted when you get the error,
You are loading a range of 1 column (AE) into an array (ary) but in the loop you are incrementing the Column index of the ary, which is going to error out as soon as you change it to 2.
Rich (BB code):
    UCel.value = ary(1, indx)
    indx = indx + 1

' should be
    UCel.value = ary(indx, 1)
    indx = indx + 1
 
Upvote 0
Solution
Thanks to both for your replies! The sheet name issue is a non-issue... they are actually different names (and correctly typed)... I renamed them for this thread and incorrectly typed one. But good catch, Macron!

Alex, I suspect you are correct. The row highlighted for the error is UCel.value = ary(indx, 1). The workbook in question is on my computer at work, but I will try this first thing tomorrow and let you know!
 
Upvote 0
Wow. Totally worked. Changed it to (indx, 1) and bam, there it was. Thanks a lot, Alex. I spent days trying to figure this out.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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