Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default a Disaster in the Making - R/T error 1004

I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default a Disaster in the Making - R/T error 1004

Not sure this is the problem, but you changed the value of SRng(i) in the
first loop, after it has been found and before the FindNext. i = i + 1


"JMay" wrote in message
...
I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" &
Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default a Disaster in the Making - R/T error 1004

You have a bug here, too:

Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))

If TheHdr isn't the activesheet, this will cause an error:

with worksheets("thehdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
end with

(notice the dots in front of the second .range() object.)

Then delete this line:
Set g = Nothing

And change this line:
Set g = Nothing
to:
Set g = .FindNext(g)

That last line is equivalent to:
Set g = .findnext(after:=g)

(find it after the last one you found)

VBA's help for .findnext shows another example.

JMay wrote:

I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default a Disaster in the Making - R/T error 1004

I'm trying to compare each cell in SRng with DRng and if there is a match
copy the content of Col B (2Cols to left) of SRng to 1 row below the matching
DRng Column

"Dave Peterson" wrote:

You have a bug here, too:

Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))

If TheHdr isn't the activesheet, this will cause an error:

with worksheets("thehdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
end with

(notice the dots in front of the second .range() object.)

Then delete this line:
Set g = Nothing

And change this line:
Set g = Nothing
to:
Set g = .FindNext(g)

That last line is equivalent to:
Set g = .findnext(after:=g)

(find it after the last one you found)

VBA's help for .findnext shows another example.

JMay wrote:

I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default a Disaster in the Making - R/T error 1004

Maybe...

Option Explicit
Sub NewMatchStuff()

' I'm trying to compare each cell in SRng with DRng and if there is a match
' copy the content of Col B (2Cols to left) of SRng to 1 row below the
' matching dRng Column

Dim SRng As Range
Dim myCell As Range
Dim dRng As Range
Dim res As Variant

With Worksheets("TheHdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
End With

Set dRng = Worksheets("Hdr formula").Range("A1:T1")

For Each myCell In SRng.Cells
res = Application.Match(myCell.Value, dRng, 0)
If IsError(res) Then
'no match
Else
myCell.Offset(0, -2).Copy _
Destination:=dRng(res).Offset(1, 0)
End If
Next myCell

End Sub

You may want to assign values (.value = .value) or copy|paste special|values. I
guessed with the code I used.

You could use .find if you wanted to. But if you use it, make sure you specify
all the parms. Don't rely on what you think the parms should be.

Excel and VBA share these settings. So if some other code or the user changes
something (values instead of formulas or part instead of whole), you may have an
intermittent bug that's difficult to find.

JMay wrote:

I'm trying to compare each cell in SRng with DRng and if there is a match
copy the content of Col B (2Cols to left) of SRng to 1 row below the matching
DRng Column

"Dave Peterson" wrote:

You have a bug here, too:

Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))

If TheHdr isn't the activesheet, this will cause an error:

with worksheets("thehdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
end with

(notice the dots in front of the second .range() object.)

Then delete this line:
Set g = Nothing

And change this line:
Set g = Nothing
to:
Set g = .FindNext(g)

That last line is equivalent to:
Set g = .findnext(after:=g)

(find it after the last one you found)

VBA's help for .findnext shows another example.

JMay wrote:

I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub


--

Dave Peterson


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default a Disaster in the Making - R/T error 1004

Dave,

Thanks Soooo much; I've printed out your code and explanation. I can follow
it's logic just fine.

Jim May

"Dave Peterson" wrote:

Maybe...

Option Explicit
Sub NewMatchStuff()

' I'm trying to compare each cell in SRng with DRng and if there is a match
' copy the content of Col B (2Cols to left) of SRng to 1 row below the
' matching dRng Column

Dim SRng As Range
Dim myCell As Range
Dim dRng As Range
Dim res As Variant

With Worksheets("TheHdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
End With

Set dRng = Worksheets("Hdr formula").Range("A1:T1")

For Each myCell In SRng.Cells
res = Application.Match(myCell.Value, dRng, 0)
If IsError(res) Then
'no match
Else
myCell.Offset(0, -2).Copy _
Destination:=dRng(res).Offset(1, 0)
End If
Next myCell

End Sub

You may want to assign values (.value = .value) or copy|paste special|values. I
guessed with the code I used.

You could use .find if you wanted to. But if you use it, make sure you specify
all the parms. Don't rely on what you think the parms should be.

Excel and VBA share these settings. So if some other code or the user changes
something (values instead of formulas or part instead of whole), you may have an
intermittent bug that's difficult to find.

JMay wrote:

I'm trying to compare each cell in SRng with DRng and if there is a match
copy the content of Col B (2Cols to left) of SRng to 1 row below the matching
DRng Column

"Dave Peterson" wrote:

You have a bug here, too:

Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))

If TheHdr isn't the activesheet, this will cause an error:

with worksheets("thehdr")
Set SRng = .Range("D3", .Range("D" & .Rows.Count).End(xlUp))
end with

(notice the dots in front of the second .range() object.)

Then delete this line:
Set g = Nothing

And change this line:
Set g = Nothing
to:
Set g = .FindNext(g)

That last line is equivalent to:
Set g = .findnext(after:=g)

(find it after the last one you found)

VBA's help for .findnext shows another example.

JMay wrote:

I've been trying to debug the below code for better than 3 hours, without
success.
Can someone help me?

Sub NewMatchStuff()
Set SRng = Worksheets("TheHdr").Range("D3", Range("D" & Rows.Count).End(xlUp))
Set DRng = Worksheets("Hdr formula").Range("A1:T1")
i = 1
With DRng
Do
Set g = .Find(SRng(i))
If Not g Is Nothing Then
faddress = g.Address
g.Offset(1).Value = SRng(i).Offset(0, -2).Value
End If
i = i + 1
Loop Until Not g Is Nothing
Do
Set g = Nothing 'Need to reset g to Nothing
Set g = .FindNext(SRng(i)) '*** R/T 1004 Here
i = i + 1
Loop While Not g Is Nothing And g.Address < faddress
End With
End Sub

--

Dave Peterson


--

Dave Peterson

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
run time error 1004 general odbc error excel 2003 vba Mentos Excel Programming 5 January 24th 11 02:56 PM
XL 2007 Toolbar Disaster Native Excel Discussion (Misc queries) 3 November 16th 07 03:06 PM
Run-Time error 1004 issue just not making sense robs3131 Excel Programming 3 June 5th 07 05:24 AM
run-time error '1004': Application-defined or object-deifined error [email protected] Excel Programming 5 August 10th 05 09:39 PM
Duplicate Disaster Zip Codes Excel Discussion (Misc queries) 3 June 13th 05 05:02 PM


All times are GMT +1. The time now is 04:14 AM.

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

About Us

"It's about Microsoft Excel"