VBA Reducing IF functions

vanik16

New Member
Joined
Apr 27, 2021
Messages
4
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

I have a VBA code set up to import and copy excel files onto a master sheet.

The VBA code also checks if the value in the "C" column (Text3) contains a specific location, and if it does the "B" column (Text2) changes accordingly.

It currently works fine with the code below, however, there are hundreds of possible locations, and the VBA code would have to contain IF functions separately for each one.

I've tried playing around with some INDEX/MATCH VBA code, but to no success. A table of the corresponding Locations and respective codes can be used on a separate sheet.

Any advice to reducing the code?

Thanks in advance!

VBA Code:
            With wb.Sheets(1)

                Text1 = .Cells(1, 1).Value         
                Text2 = .Cells(1, 2).Value            
                Text3 = .Cells(1, 3).Value                                 
                Text3a = UCase(Mid(Text3, InStr(Text3, " ") + 1, 5))        '5 characters following the first <Space> in the field
                  
            End With         
          
            ThisWorkbook.Activate
        
                    If "LOCATION1" = Text3a Then
                        Text2 = "123"
                    Else
                    End If

                    If "LOCATION2" = Text3a Then
                        Text2 = "456"
                    Else
                    End If

                    If "LOCATION3" = Text3a Then
                        Text2 = "789"
                    Else
                    End If

            .Cells(NewTempRow, 1) = Text1
            .Cells(NewTempRow, 2) = Text2
            .Cells(NewTempRow, 3) = Text3
          
            NewTempRow = NewTempRow + 1

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi & welcome to MrExcel.
You could use range.find on a separate sheet like
VBA Code:
            Dim Fnd As Range
            With ThisWorkbook.Sheets("List")
               Set Fnd = .Range("A:A").Find(text3a, , , xlWhole, , , False, , False)
               If Not Fnd Is Nothing Then
                  text2 = Fnd.Offset(, 1).Value
               End If
            End With
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
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