An easy one for you

ektorakos

New Member
Joined
Nov 16, 2016
Messages
9
This should be an easy one for you.
Why do i get error 1004 method range of object global failed in this one??
it seems pretty strait forward
What am i missing?


Sub test()

Dim xrows As Integer
Dim x As Integer
Dim y As Variant

xrows = (Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Count) - 4

For x = 4 To xrows
y = Left(Range(Cells(x, 1)), 2)

Next x

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

JeanKim

Board Regular
Joined
May 31, 2016
Messages
183
when error occurs msgbox variable Cells(x, 1) or x in immediate windown to see what is causing the error
 
Upvote 0

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,801
If you insist on using Range(...), the following would work:
Code:
y = Left(Range(Cells(x, 1), Cells(x, 1)), 2)

However, it's much easier to use just
Code:
y = Left(Cells(x, 1), 2)
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,955
Office Version
  1. 365
Platform
  1. Windows
Another thing to keep in mind.
The newer versions of Excel allow for over 1 million rows. Integer values can only go up to 32767.
If you have may have more rows of data than that, these variable declarations will not be sufficient.
Code:
[COLOR=#333333]Dim xrows As Integer[/COLOR]
[COLOR=#333333]Dim x As Integer[/COLOR]
You will need to use Long, i.e.
Code:
[COLOR=#333333]Dim xrows As Long[/COLOR]
[COLOR=#333333]Dim x As Long[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,191,056
Messages
5,984,397
Members
439,884
Latest member
BrownEyedGirl

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
Top