VBA to variant returns string through iteration

RodrigoFinguer

Board Regular
Joined
Jun 13, 2017
Messages
75
Hello guys, i am beating my head here

need your help... i have 2 strings and i want a variant to call this string through an iteration, but i can't get it.


Code:
Sub teste()
Dim i As Integer


For i = 1 To 2


var1 = "Empresa"
var2 = "Material"


desc = "var" & i


l = Application.Match(desc, Range("A1:A100"), 0)
Columns(l).Select
Next


End Sub

My "desc" is returning "var1" and not "Empresa" as i need.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Use an array:

Code:
Sub teste()
Dim i As Integer
Dim var(1 to 2) as string
var(1) = "Empresa"
var(2) = "Material"

For i = 1 To 2

desc = var(i)


l = Application.Match(desc, Range("A1:A100"), 0)
Columns(l).Select
Next


End Sub
 
Upvote 0
Use an array:

Code:
Sub teste()
Dim i As Integer
Dim var(1 to 2) as string
var(1) = "Empresa"
var(2) = "Material"

For i = 1 To 2

desc = var(i)


l = Application.Match(desc, Range("A1:A100"), 0)
Columns(l).Select
Next


End Sub

Worked, but for my i = 2 i got and error 2042 in my "l = Application.Match(desc, Range("A1:A100"), 0)"
 
Upvote 0
That means the value wasn't found.
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,730
Members
448,294
Latest member
jmjmjmjmjmjm

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