Can't add values to an array

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to add values from a row to an array and just can't figure it out.

What I can't work out is how to add the values from column H onward to an array. It's probably very straightforward but I've tried all manner of solutions and just can't get it to work. The latest thing I have tried is producing an array but it has extra blank values added too.

This is what I've been playing with, (don't make fun at how rough and crude it is, I'm doing my best to tinker and learn!).

VBA Code:
LastRow = ThisWorkbook.Worksheets("COPYRESULTS").Range("A65536").End(xlUp).Row

For Each Cell In ThisWorkbook.Worksheets("COPYRESULTS").Range("A2:A" & LastRow)

r = Cell.Row

ColNo = Cells(r, Columns.Count).End(xlToLeft).Column
ColLe = Col_Letter(Cells(r, Columns.Count).End(xlToLeft).Column)

Dim myary(1 To 7) As String
Dim var As Integer
Dim myrang As Range, myC As Range
var = 1
Set myrang = Range("H3:" & ColLe & "3")
For Each myC In myrang
myary(var) = "• " & myC.Value
var = var + 1
Next myC
For var = LBound(myary) To UBound(myary)
Debug.Print myary(var)
Next var

Next Cell

Any advice is appreciated!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I'm not 100% clear on what range exactly you want stored in your array, but please give this a try & see if it comes close to what you are looking for:
VBA Code:
Option Explicit
Sub FillArray_1()
    Dim ws As Worksheet
    Set ws = Worksheets("COPYRESULTS")
    Dim LRow As Long, LCol As Long
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    Dim myary, n As Long, i As Long, c As Range
    n = ws.Range(ws.Cells(3, 8), ws.Cells(LRow, LCol)).Cells.Count  '<-- cells 3,8 = H3 your starting point?
    ReDim myary(1 To n) As Variant
    
    i = 1
    For Each c In ws.Range(ws.Cells(3, 8), ws.Cells(LRow, LCol))
        If c <> "" Then
            myary(i) = "• " & c.Value
            i = i + 1
        End If
    Next c
    ReDim Preserve myary(1 To i - 1)
    
    For i = LBound(myary) To UBound(myary)
        Debug.Print myary(i)
    Next i
End Sub
 
Upvote 0
Thanks very much, that's certainly progress!

Can it be tweaked please so that it adds each value (for each row) into 1 string with each value on a new line?

I want to store the result in column G of that row.

Thanks, appreciate your help!
 
Last edited:
Upvote 0
Can it be tweaked please so that it adds each value (for each row) into 1 string with each value on a new line?

I want to store the result in column G of that row.
I'm struggling to visualise this a bit - could you provide a before-and-after sample so I can get my head around it? Preferably using the XL2BB add in.
 
Upvote 0
Yeah sure - I'm on a train at the moment and don't have my laptop so I'll reply later - thanks!
 
Upvote 0
I've managed to borrow a colleagues laptop!

So this is the before;

All Gold Checks.xlsm
GHIJ
1Summary123
2Delay callingWarning not added
3No logNo stampDocumented clear
COPYRESULTS


and this is the after;

All Gold Checks.xlsm
GHIJ
1Summary123
2• Delay calling •Warning not addedDelay callingWarning not added
3• No log • No stamp • Documented clearNo logNo stampDocumented clear
COPYRESULTS


I have to say, I find arrays so confusing to understand so I'm grateful for your help!
 
Upvote 0
Please try this on a copy of your workbook:
VBA Code:
Option Explicit
Sub FillArray_2()
    Dim ws As Worksheet
    Set ws = Worksheets("COPYRESULTS")
    Dim LRow As Long, LCol As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    Dim myary, outary, i As Long, j As Long, s As String
   
    myary = ws.Range(ws.Cells(2, 8), ws.Cells(LRow, LCol))  '<-- *** assumes you're starting in H2 (2,8)
    ReDim outary(1 To UBound(myary, 1), 1 To 1)
    For i = 1 To UBound(myary, 1)
        For j = 1 To UBound(myary, 2)
            If myary(i, j) <> "" Then s = s & "• " & myary(i, j) & vbCrLf
        Next j
        s = Left(s, Len(s) - 1)
        outary(i, 1) = s: s = ""
    Next i
        ws.Range("G2").Resize(UBound(outary, 1), 1).Value = outary
        ws.Range("G2:G" & LRow).EntireRow.AutoFit
End Sub

Before:
sharky.xlsm
GHIJ
1Summary123
2Delay callingWarning not added
3No logNo stampDocumented clear
COPYRESULTS


After:
sharky.xlsm
GHIJ
1Summary123
2• Delay calling • Warning not added Delay callingWarning not added
3• No log • No stamp • Documented clear No logNo stampDocumented clear
4
COPYRESULTS
 
Upvote 1
Solution
Thanks!

I'm getting a run-time error 5, invalid procedure call or argument on this line;

VBA Code:
s = Left(s, Len(s) - 1)
 
Upvote 0
OK, as you can see, I don't get that when I run the code on the data sample you supplied. Is it possible to share your file via Google Drive, Dropbox or similar file sharing platform? That way, we'll both be looking at the same data.
 
Upvote 0
Yeah sure, I'll need to sanitise it first but certainly......I'll come back to you
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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