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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
when error occurs msgbox variable Cells(x, 1) or x in immediate windown to see what is causing the error
 
Upvote 0
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
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,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

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