Macro Help

jkindhappy

Board Regular
Joined
Jul 14, 2008
Messages
112
Hi,

I am a starter in VBA. Below is apart of code in which I am getting error in the bolded and the under lined line. Can I have some suggestions how to rectify this.
Code:
With ws
    For i = 1 To Sheets.Count - 1
        .Cells(i, 1).Value = Sheets(i).Name
    Next i
End With
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To lr
    [COLOR=red][B][U]myArray(j) = ws.Cells(j, 1)[/U][/B][/COLOR]
Next j
For n = 1 To lr
    myArray(n).Range("A1:D4").Interior.ColorIndex = 6
 
Next n

Thank you.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Are you trying to assign the value from ws.Cells(j,1) to the array?
Then it needs to be ws.Cells(j,1).Value
 
Upvote 0
Hi snowblizz,

Yes, you are right.

I have rectified the code as
Code:
 myArray(j) = ws.Cells(j, 1).Value

Now I am getting run time error 13.
 
Upvote 0
Hi snowblizz,

Yes, you are right.

I have rectified the code as
Code:
 myArray(j) = ws.Cells(j, 1).Value
Now I am getting run time error 13.
Bummer. :( If computers only did what we wanted and not what we told them...
Is the error on the same line?

What kind of data does the array contain (or is supposed to contain)? Looking at the continuation it doesn't quite add up in my mind.
 
Upvote 0
As I told I am starter in VBA, what I am trying to do is: I am trying to add a new sheet and getting the names of all sheets in to it. Then I am trying to assign the names of sheets to an array and then trying to color the range A1:D4.
Code:
Option Base 1
Sub sheetName()
 
Dim ws As Worksheet
Dim myArray As Variant
 
Set ws = Worksheets.Add
ws.Name = "List"
ws.Move After:=Sheets(Sheets.Count)
With ws
    For i = 1 To Sheets.Count - 1
        .Cells(i, 1).Value = Sheets(i).Name
    Next i
End With
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To lr
    [B][U][COLOR=red]myArray(j) = ws.Cells(j, 1).Value[/COLOR][/U][/B]
Next j
For n = 1 To lr
    myArray(n).Range("A1:D4").Interior.ColorIndex = 6
 
Next n
End Sub

Yes, the error is on the same line.
Any Help on where I am going wrong.
 
Upvote 0
Well I'm an advance beginner myself (so I know enough to cause lots of problems for myself:p) and I was just recently struggling with arrays so I feel your pain.

I don't see you declaring the size of the array. This might be a source of your problems.

You can actually take a range and assign it directly into an array. Provided the dimension is the same.
This should work:
myArray = Range("A1:A20").Value

as long as myArray has 20 "spaces".

Are you trying to change the background colour of cells in all sheets of the workbook? Because there is already an array of that provided by Excel.
 
Upvote 0
Well I'm an advance beginner myself (so I know enough to cause lots of problems for myself:p) and I was just recently struggling with arrays so I feel your pain.

I don't see you declaring the size of the array. This might be a source of your problems.

You can actually take a range and assign it directly into an array. Provided the dimension is the same.
This should work:
myArray = Range("A1:A20").Value

as long as myArray has 20 "spaces".

Are you trying to change the background colour of cells in all sheets of the workbook? Because there is already an array of that provided by Excel.

Help from any one?
 
Upvote 0
It was indeed a problem with the size of the array.
Code:
Sub sheetName()
 
Dim ws As Worksheet
Dim myArray As Variant
 
Set ws = Worksheets.Add
ws.Name = "List"
ws.Move After:=Sheets(Sheets.Count)
ReDim myArray(Sheets.Count)
With ws
    For i = 1 To Sheets.Count - 1
        .Cells(i, 1).Value = Sheets(i).Name
        'myArray(i) = Sheets(i).Name 'if you want to add sheets names to an array directly use this line instead of above
    Next i
End With
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To lr
    myArray(j) = ws.Cells(j, 1).Value
Next j
For n = 1 To lr
        myArray(n).Range("A1:D4").Interior.ColorIndex = 6 'this line will fail as the array is not a worksheet object that can have ranges
 
Next n
End Sub
I've made some modifications to the code. Note that the last part will fail. If you are trying to change the colorindex on all worksheets then that can be done but you don't need to do it like this.
 
Upvote 0

Forum statistics

Threads
1,215,986
Messages
6,128,115
Members
449,423
Latest member
Mike_AL

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