Thanks very much for your help on my last question.
Here I am trying to use vlookups in vba with variables that are set depending on which worksheet I am working on.
I want to loop through a column (C) doing vlookups until the last row and enter the value 'unknown' on an error. For each worksheet I work on the table_array and column_index will change. So i want to be able to preset these values in case statements before calling the function temp_for_calcs as below.
here is the code I want to adapt to make it accept preset variables for the vlookup. I have tried to use application.worksheetfunction.vlookup and application.vlookup but when I try and autofill these to the end of my column they autofill the values not the formula.
If anyone can shed any light on this I'd REALLY appreciate it
Here I am trying to use vlookups in vba with variables that are set depending on which worksheet I am working on.
I want to loop through a column (C) doing vlookups until the last row and enter the value 'unknown' on an error. For each worksheet I work on the table_array and column_index will change. So i want to be able to preset these values in case statements before calling the function temp_for_calcs as below.
Code:
If ActiveWorkbook.Worksheets(i).Name = "valve" Then ''''VALVE'''''''''''
For case_no = 1 To 4
Select Case case_no
Case 1
Set filter_range = Worksheets(i).Range("j:j,s:s")
Set calcs_sheet = Sheets("valve_length_calcs")
Set tables_sheet = Sheets("valve_length_tables")
Set table_Array = Sheets("flag_matrix").Range("a:be")
Set col_index = 57
here is the code I want to adapt to make it accept preset variables for the vlookup. I have tried to use application.worksheetfunction.vlookup and application.vlookup but when I try and autofill these to the end of my column they autofill the values not the formula.
Code:
Private Function temp_for_calcs_more()'''''' calculations and autofill with variable lengths ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set myrange = Sheets("temp_for_calcs").Columns("c:c") 'setting range for countif of variable length column
Sheets("temp_for_calcs").Select
Range("d2").Formula = "=countif(b:b,c2)" 'counting number of each unique flag in column a
countnonblank = Application.WorksheetFunction.CountA(myrange) 'counts how many values in b
Range("e2").Formula = "=IFERROR(VLOOKUP(c2,flag_matrix!A:be,57,FALSE),""unknown"")" '%%%%%NEED TO MAKE THIS DYNAMIC%%%%%%
Range("f2").Formula = "=IF(e2<>""unknown"",d2*e2,""unknown"")" ''flag score * no flags
Range("i2").Formula = "=sum(d:d)" 'total flag count
Range("g2").Formula = "=IF(e2<>""unknown"",d2/$i$2 *100,""unknown"")" '% of total score
If countnonblank > 2 Then 'accounts for 1 value and column header, stops autofill error if no rows to fill
' fills to the end of data in row b
'''CHANGE BELOW''' table array and column index number'
Range("d2").AutoFill Destination:=Range("d2:d" & countnonblank)
Range("e2").AutoFill Destination:=Range("e2:e" & countnonblank) ------------->>>> autofills values if i use application/applicationworksheetfunction
Range("f2").AutoFill Destination:=Range("f2:f" & countnonblank)
Range("g2").AutoFill Destination:=Range("g2:g" & countnonblank)
End If
If anyone can shed any light on this I'd REALLY appreciate it