VB Script Error

philmatth

Board Regular
Joined
Oct 6, 2008
Messages
123
Office Version
  1. 2003 or older
Platform
  1. Windows
I'm having a problem with a VB script which was kindly posted for me some time ago and has functioned perfectly until I've tried it on the sheet below. The highlighted fields should be removed when the macro is run and the associated image names should be moved to the adjacent fields.



The script does move the image names for the first three duplicate prod-id's but doesn't remove the highlighted cells and gives the error below:

Set nRng = Union(nRng, .Item(k).Offset(1).Resize(.Item(k).Count - 1))

prods.gif


Here's the VBscript:

Sub ImageManip
Dim Rng As Range
Dim Dn As Range
Dim k
Dim nRng As Range
Dim Temp As Range
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Dn
Else
Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
End If
Next

For Each k In .keys
If .Item(k).Count > 1 Then
If nRng Is Nothing Then
Set Temp = .Item(k)(1).Offset(, 1)
Set nRng = .Item(k).Offset(1).Resize(.Item(k).Count - 1)
Else
Set Temp = .Item(k)(1).Offset(, 1)
Set nRng = Union(nRng, .Item(k).Offset(1).Resize(.Item(k).Count - 1))
End If
Temp.Resize(, .Item(k).Count) = Application.Transpose(.Item(k).Offset(, 1).Value)
End If
Next k
nRng.EntireRow.Delete
End With
End Sub

Any help at all would be much appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,527
Messages
6,179,328
Members
452,907
Latest member
Roland Deschain

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