Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default VBA Simple question

I am received the error "1004" Method 'Range' of object '_Global' failed.

I find that this is almost always caused by a missing workbook or worksheet
reference: "Range(A1)" vs "Worksheets("Sheet1").Range(A1)".

Range, Cells, Columns, Rows, etc. are all properties of multiple objects.
When you don't specify a "parent" object for one of those, Excel can
*sometimes* guess what you mean. When it can't, you get a 1004 error (and
whether a piece of code does or does not cause an error can even seem to
change from day to day). Avoid making assumptions to avoid 1004's.

(This has been tested)

Dim wks as Worksheet
Dim node As Integer
Dim xrow As Integer

Set wks = ThisWorkbook.Worksheets("Sheet3")
node = 1
With wks
For xrow = 2 To 10000
If .Range("b" & xrow) < 0 Then
If .Range("b" & xrow) < .Range("b" & xrow - 1) Then
.Range("a" & xrow) = 33
End If
End If
Next xrow
End With

Notes: 1) Note the addition of a dot/period before each occurance of Range
within the "With...End With" stucture. This associates Range with wks and
should eliminate your 1004's
2) If performance is an issue, you might consider changing
".Range("b" & xrow)" to ".Cells(xrow,2).", etc. Concatenation (using &)
does add a bit of processing time. Probably not material here.
3) I assume node is doing something in code you didn't share
with us, 'cause it certainly ain't doing anything here <g

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Shane" wrote in message
...
I am received the error "1004" Method 'Range' of object '_Global' failed.

I tried both suggestions, neither changed this error.

"JE McGimpsey" wrote:

By looping xrow from 1 to 10000, the first iteration will error since
there is no Range("B0"). You might be able to use

For xrow = 2 to 10000

FYI, you don't gain anything by defining xrow as an Integer rather than
a Long (VBA uses longs internally), and if you ever use more than 32767
rows, you'll get an error.

In article ,
"Shane" wrote:

I apologize, the code should be shown as:

Public Sub assign()
Worksheets("Sheet3").Activate
Dim node As Integer
Dim xrow As Integer
node = 1
For xrow = 1 To 10000
If Range("b" & xrow).Value < 0 And range("b" & xrow).Value <
range("b" &
xrow - 1).Value Then
Range("a" & xrow).Value = 33
End If
Next
End Sub

I feel that the range("b" & xrow - 1).value is causing the problem.
How
should I write it?

"Shane" wrote:

I am looking to automate a cell comparison.

The code I have is as follows:

Public Sub assign()
Worksheets("Sheet3").Activate
Dim node As Integer
Dim xrow As Integer
node = 1
For xrow = 1 To 10000
If Range("b" & xrow).Value < 0 And Cells("b" & xrow).Value <
Cells("b" &
xrow).Value Then
Range("a" & xrow).Value = 33
End If
Next
End Sub

I want to search through 10000 cells in column B and if the value is
not 0,
and not equal to the previous cell, then enter "33". The figures are
arbitrary, but this is the task I need completed. Thanks for the
help!




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF formula-simple question; simple operator Rich D Excel Discussion (Misc queries) 4 December 6th 07 03:36 PM
Simple Question Brian Excel Discussion (Misc queries) 3 March 10th 06 10:12 AM
Simple Question cmk18[_12_] Excel Programming 1 August 19th 05 03:29 PM
Simple Simple Excel usage question BookerW Excel Discussion (Misc queries) 1 June 23rd 05 10:06 PM
simple question, hopefully a simple answer! Matt B Excel Programming 5 January 13th 04 08:43 PM


All times are GMT +1. The time now is 07:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"