VBA - Using two Arrays to replace Variable

mboufleur

New Member
Joined
Feb 18, 2016
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I don´t have any particular VBA skills, but I have attempted to do a macro to change a variable. It however does not seem to be working.

The variable in question is a String from within the Macro, and it should be easy. I was following some tips from this post, but that does not seem to be working in my case, since I have changed a bunch of codes which I assumed would not matter, and for this case no information from within the workbook is being used.

To better explain the problem, this is one of those cases when a variable (which in this case is the filename) is compared to an array, and the correspoding value from another array has to be used to replace this variable. Here is the code I have attempted to use:

VBA Code:
Sub NameChange()

    Dim SourceFile As String
    Dim Original_Name As Variant
    Dim Final_Name As Variant
    Dim i As Long

    SourceFile = Application.ActiveWorkbook.Name

    Original_Name = Array("Apples", "Oranges", "Bananas")
    Final_Name = Array("Red", "Orange", "Yellow")

            For i = 0 To UBound(Original_Name)
                Set SourceFile = .Find(Final_Name(i), , , xlWhole, , , False, , False)
            Next

    MsgBox "File is " & SourceFile

End Sub

My code is broken, I am sure - but due to my lack of knowledge of VBA, I simply cannot seem to elaborate a logic for this to work. Can someone help me out on this?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
try this:
VBA Code:
Sub NameChange()

    Dim SourceFile As String
    Dim Original_Name As Variant
    Dim Final_Name As Variant
    Dim i As Long

    SourceFile = Application.ActiveWorkbook.Name
    
    Original_Name = Array("Apples", "Oranges", "Bananas")
    Final_Name = Array("Red", "Orange", "Yellow")

            For i = 0 To UBound(Original_Name)
              If SourceFile = Original_Name(i) Then
               SourceFile = Final_Name(i)
               Exit For
              End If
            Next i

    MsgBox "File is " & SourceFile

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,192
Members
448,554
Latest member
Gleisner2

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