Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Rainhard Fischer   on Jan 17 In MS Office Category.

  
Question Answered By: Jamie Roberts   on Jan 17

Just thought of another technique that might REALLY REALLY REALLY simplify
your script. But only if certain conditions are true...

So, before I get into it...
In your script, You have a bunch of things  you're testing for in column  "AI"
(thingone, thingtwo,thingthree)
If you find  them, you're inserting something else in AQ...
What is the "something else".. is it another cell value or a constant?

The reason I ask is this:
VBA has a Dictionary object.
It's pretty basic, but VERY fast.

If you can use it, you set  it up like:

Dim THINGS, Stat

Set THINGS = CreateObject("Scripting.Dictionary")
Stat = THINGS.RemoveAll
THINGS.Add "THINGONE", "A"
THINGS.Add "THINGTWO", "B"
THINGS.Add "THINGTHREE", "C"
THINGS.Add "THINGFOUR", "D"
THINGS.Add "THINGFIVE", "E"
THINGS.Add "THINGSIX", "F"
THINGS.Add "THINGSEVEN", "G"
THINGS.Add "THINGEIGHT", "H"
THINGS.Add "THINGNINE", "I"
THINGS.Add "THINGTEN", "H"
THINGS.Add "THINGELEVEN", "J"
THINGS.Add "THINGTWENVE", "K"
THINGS.Add "THINGTHIRTEEN", "M"

then your entire loop becomes:

For Trow = 2 To LastRow
If Sheets(ShtName).Range("I" & Trow).Value = "BIGSUPPLIER" Then
If THINGS.exists(Sheets(ShtName).Range("AI" & Trow).Value) Then
Sheets(ShtName).Range("AQ" & Trow).FormulaR1C1 =
THINGS.Item(Sheets(ShtName).Range("AI" & Trow).Value)
Sheets(ShtName).Range("A" & Trow).Interior.Color =
RGB(204, 255, 255)
Else
If Sheets(ShtName).Range("AI" & Trow) <> Empty Then
Sheets(ShtName).Range("AI" & Trow).Select
Sheets(ShtName).Range("B" & Trow & ":AR" &
Trow).Interior.ColorIndex = 3
response = MsgBox("The following is a list  of acceptable
values for the Rep/OSR field  when the supplier is IBM" & vbCr _
& vbCr & vbTab & "THINGONE" & vbCr _
& vbTab & "THINGTWO" & vbCr _
& "The value you have entered  is not one of these
allowed values. Please change  this value.", vbOKOnly)
Else
Sheets(ShtName).Range("AI" & Trow).Select
End If
End If
Else
Application.EnableEvents = True
Exit Sub
End If
Next


If you need to add "things", then you just add them to the dictionary...

It's very fast with large numbers of items.
(I needed to read in a text file containing 145,000 records and needed to look
up data for 20,000 items.
Of course, I couldn't load it into a sheet and do a vlookup, so I read it and
loaded 6 dictionaries.
The whole lookup/update takes less than 90 seconds!)

Share: 

 

This Question has 17 more answer(s). View Complete Question Thread

 


Tagged: