Solved

Sweep a defined area replacing certain values with data contained within an array

Posted on 2014-01-26
1
138 Views
Last Modified: 2014-02-01
Hi,

What I am trying to do is define a two dimension array,

dim mydata (1 to 5, 1 to 2) as string

mydata(1,1)="[version]"
mydata(1,2)="v1.4.3"

Then, when a certain defined range within the sheet is changed, it does a check to see if any of the cells equal mydata(1,-) contain [version] and if they do it replaces them with mydata(,2)

So in this if I type into C3 "[update]" it will become v1.4.3 straight away.

This is my current code, which did work until I tried adding arrays in...

Private Sub Worksheet_Change(ByVal Target As Range)
   
Dim mydata(1 To 5, 1 To 2) As String
   
mydata(1, 1) = "[version]"
mydata(1, 2) = "v1.4.3"
   
    For lp = 1 To 40
        For lp2 = 1 To 40
       
            For lp3 = 1 To 5
                If Sheets("Sheet1").Cells(lp, lp2) = mydata(lp3, 1) Then
                    Sheets("Sheet1").Cells(lp, lp2) = mydata(lp3, 2)
                   
                   
                    Beep
                End If
            Next
       
       
        Next
    Next
   
End Sub




As you can see I am using a for loop to check 40 x 40 cells, other than fixing the above so it works please :)  Is there a better way to scan 40x40 cells and change values based on criteria from array?

Cheers
0
Comment
Question by:DemonForce
1 Comment
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39810755
Assuming that the users are changing the values of the cells (rather than formulas), you should restrict the operation of the macro to those cells that are changed within the 40 x 40 cell grid of interest. Having done that, you can use VLOOKUP to return the new value.

Note that you need to fully populate the mydata array within the sub.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range, targ As Range
Dim mydata As Variant, v As Variant
Set targ = Range("A1:AN40")     'Watch these cells for user-initiated changes
Set targ = Intersect(targ, Target)
If targ Is Nothing Then Exit Sub

    'Add additional Array blocks within the outer array for other rows in mydata array
mydata = Array(Array("[version]", "v1.4.3"), Array("[version1]", "v1.4.3a"))
    
For Each cel In targ.Cells
    Set v = Nothing
    v = Application.VLookup(cel.Value, mydata, 2, False)
    If Not IsError(v) Then
        Beep
        Application.EnableEvents = False
        cel.Value = v
        Application.EnableEvents = True
    End If
Next
End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows 7 Share with concurrent edits(Excel) 3 33
Compile Error 7 41
Excel VBA When using VLookup 6 28
Problem counting secound table of filtered data. 9 20
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now