vlookup between two worksheets in VBA - vlookup

i have tried alot doing vlookup between two workbooks, one is open (book1) and the other
needs to be opened by GetOpenFilename() function let us call it (book2).
(book1) contains sheet1 where i want to do the vlookup
(book2) contains sheet2 where i want to match the id (column 1) and get the names (column2) by vlookup.
here is my code. Notice that the number of rows changes every month so, i need to loop until the last row in sheet1(in book1).
Sub status_first_Step()
Range("C:C").Insert
Set book1 = Sheets("Sheet1")
book2 = Application.GetOpenFilename()
Workbooks.Open book2
Set book2 = ActiveWorkbook
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
ws1.Cells(i, "C").Value = Application.VLookup(book1.Cells(i, 1).Value, book2.Sheets("sheet2").Columns("A:Y"), 2, 0)
Next i
End Sub
Thank You.

Related

Dynamic Google Sheets Column + Row formula

I have a good sheet that I want to grab the header which a date time stamp which will match against another sheet find the entries with that date and suburb and type and give me an average cost.
My formula is =AVERAGEIFS(Sheet1!C:C,Sheet1!A:A, B11:B, Sheet1!F:F, C10) which gives me the average but i've hard coded the header date:
example:
What I want to do is dynamically add the data from the row above with the date time instead of of manually adding it in the formula something like this:
=AVERAGEIFS(Sheet1!C:C,Sheet1!A:A, B11:B, Sheet1!F:F, =CHAR(COLUMN()+64) & 10)
Which would automatically grab the column + row 10 e.g C10, D10, E10.
If i put =CHAR(COLUMN()+64) & 10 in its own cell it works but when I add it to averageifs condition it gives me a parsing error.
Expecting C10, D10, E10 from =CHAR(COLUMN()+64) & 10 which should allow me to dynamically filter data on the date int he header above it.
try:
=AVERAGEIFS(Sheet1!C:C, Sheet1!A:A, B11:B, Sheet1!F:F, INDIRECT(CHAR(COLUMN()+64)&10))

vlookup google sheets first and last name compare

Okay so i have a this formula:
=ArrayFormula({"Manager:";IF(D5:D="",,IFERROR(VLOOKUP("*"&REGEXEXTRACT(D5:D,"[a-zA-Z]+")&"*",'Client-Manager'!A:B,2,FALSE),"NO MATCH"))})
It works fine with what i have it doing now but i'm running into a problem now were i need to compare both last and first name and i can't figure out how to do it.
Here is an example sheet:
Sheet im working with
Im comparing in "work-order" sheet column D with and in "manager" sheet column A to then output back on "work-order" sheet column G.
Formula i'm working with resides in "Manager" cell in "work-order" sheet in column G
try:
=ARRAYFORMULA({"Manager:"; IF(D5:D="",,
IFERROR(VLOOKUP("*"&REGEXEXTRACT(REGEXREPLACE(D5:D, ",", ),
"[a-zA-Z ]+")&"*", 'Client-Manager'!A:B, 2, ),
IFERROR(VLOOKUP("*"&REGEXEXTRACT(D5:D,
"[a-zA-Z]+")&"*", 'Client-Manager'!A:B, 2, ), "NO MATCH")))})

Tag the 6 first wording days of the month

I am trying to find a way to tag the 6 first wording days (Monday to Friday) of the month.
For example, for November I want:
Date
FlagDate
11/01/2021
True
11/02/2021
True
11/03/2021
True
11/04/2021
True
11/05/2021
True
11/06/2021
False
11/07/2021
False
11/08/2021
True
11/09/2021
False
11/10/2021
False
11/11/2021
False
11/12/2021
False
11/13/2021
False
11/14/2021
False
11/15/2021
False
11/16/2021
False
11/17/2021
False
11/18/2021
False
11/19/2021
False
11/20/2021
False
11/21/2021
False
and so on.
Here is M code that adds a column to your existing table.
It assumes that the column Name in your table is Date
It also assumes you have a List of the relevant holiday dates spanning at least the relevant time frame. I obtained the list from https://publicholidays.fr and buffered it to keep it from continuously downloading
See the comments in the M-Code to understand the algorithm
Note that there is no requirement that the Date column encompasses a full month. But if it starts on, for example, the 15th, there will be nothing in that month marked TRUE since the first6 are not there.
Assumption: Date column spans less than 1 year
If that is not the case, will need a different algorithm to create the first6 list.
M Code
//Before adding the "Flag" column enter this code to create a buffered list of working days in the months in the Date column
workingDays =
let
//generate list of all dates from beginning of first month to the last date listed
dts = List.Transform(
{Number.From(Date.StartOfMonth(#"Previous Step"[Date]{0}))..
Number.From(List.Max(#"Previous Step"[Date]))},each Date.From(_)),
//Using List.Generate, create a list of working days from the preceding list
//working days will be days that are Monday-Friday and NOT found in the list of Holiday dates
wdys = List.Generate(
()=>[wd=if Date.DayOfWeek(dts{0})>= Day.Monday
and Date.DayOfWeek(dts{0})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{0}) then dts{0} else null,
idx = 0],
each [idx] < List.Count(dts),
each [wd=if Date.DayOfWeek(dts{[idx]+1})>= Day.Monday
and Date.DayOfWeek(dts{[idx]+1})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{[idx]+1}) then dts{[idx]+1} else null,
idx = [idx]+1],
each [wd]),
//clean the wdys List by removing the Nulls
cleanList = List.RemoveNulls(wdys),
//create a list of the Months within the workday list
monthsInList= List.Distinct(List.Transform(cleanList, each Date.Month(_))),
//Use List.Accumulate to create lists of the first 6 working days in each month
first6 = List.Accumulate(monthsInList,{},
(state, current)=> state &
List.FirstN(List.Select(cleanList,each Date.Month(_)=current),6))
in List.Buffer(first6),
//Now add a column and set the flag if a Date in the date column is found in the first6 list
flagged = Table.AddColumn(#"Previous Step","FlagDate", each List.Contains(workingDays,[Date]), type logical)
in
flagged
filtered to show only dates marked True
M Code Edited to handle spans of more than one year
Change MonthsInList to include the year, and also Test for that in the List.Accumulate step
//Before adding the "Flag" column enter this code to create a buffered list of working days in the months in the Date column
workingDays =
let
//generate list of all dates from beginning of first month to the last date listed
dts = List.Transform(
{Number.From(Date.StartOfMonth(#"Previous Step"[Date]{0}))..
Number.From(List.Max(#"Previous Step"[Date]))},each Date.From(_)),
//Using List.Generate, create a list of working days from the preceding list
//working days will be days that are Monday-Friday and NOT found in the list of Holiday dates
wdys = List.Generate(
()=>[wd=if Date.DayOfWeek(dts{0})>= Day.Monday
and Date.DayOfWeek(dts{0})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{0}) then dts{0} else null,
idx = 0],
each [idx] < List.Count(dts),
each [wd=if Date.DayOfWeek(dts{[idx]+1})>= Day.Monday
and Date.DayOfWeek(dts{[idx]+1})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{[idx]+1}) then dts{[idx]+1} else null,
idx = [idx]+1],
each [wd]),
//clean the wdys List by removing the Nulls
cleanList = List.RemoveNulls(wdys),
//create a list of the Months within the workday list
monthsInList= List.Distinct(List.Transform(cleanList, each Date.ToText(_,"yyyy-MM"))),
//Use List.Accumulate to create lists of the first 6 working days in each month
first6 = List.Accumulate(monthsInList,{},
(state, current)=> state &
List.FirstN(List.Select(cleanList,each Date.ToText(_,"yyyy-MM")=current),6))
in List.Buffer(first6),
//Now add a column and set the flag if a Date in the date column is found in the first6 list
flagged = Table.AddColumn(#"Previous Step","FlagDate", each List.Contains(workingDays,[Date]), type logical)
in
flagged
You can create a custom column with DAX with the following formula
= CONTAINS(TOPN(6,FILTER('Table',WEEKDAY('Table'[Date],2)<6)),'Table'[Date],'Table'[Date])
You can update TopN(6 to accordingly update the number of first working days

read two columns in Excel using python

I want to write a python script which should read xlsx file and based on value of column X, it should write/append file with the value of column Z.
Sample data:
Column A Column X Column Y Column Z
123 abc test value 1
124 xyz test value 2
125 xyz test value 3
126 abc test value 4
If value in Column X = abc then it should create a file (if not existing already) in some path with name abc.txt and insert the value of column Z in abc.txt file, likewise if Column X = xyz then it should create a file in same path with xyz.txt and insert the value of column Z in xyz.txt file.
from openpyxl import load_workbook
wb = load_workbook('filename.xlsm')
ws = wb.active
for cell in ws.columns[9]: #here column 9 is value is what i am testing which is Column X of my example.
if cell.value == "abc":
print ws.cell(column=12).value #this is not working and i dont know how to read corresponding value of another column
Please suggest what could be done.
Thank you.
Change
print ws.cell(column=12).value
By:
print ws.columns[col][row].value
in your case:
print ws.columns[12-1][cell.row-1].value
Note that if you use this indexation method cols and rows start with index 0. This is why I'm doing cell.row-1, so take it into account when you address your column, if your 12 starts counting from 1 you'll have to address to 11.
Alternatively you can access to your information cell like this: ws.cell(row = cell.row, column = 12).value. Note in this case cols and rows start at 1.

VBA Excel : Read file, and stock images in a column

I am new to VBA (I mean, REALLY new) and I would like you to give me some tips.
I have an Excel file with 2 columns: SKU and media_gallery
I also have images stocked in a folder (lets name it /imageFolder)
I need to parse the imageFolder and look for ALL images sarting by SKU.jpg , and put them into the media_gallery column separated by a semicolon ( ; )
Example: My SKU is "1001", I need to parse the image folder for all images starting by 1001 (all image have this pattern: 1001-2.jpg , 1001-3.jpg etc...)
I can do that in Java or C# but I want to give a chance to VBA. :)
How can I do that?
EDIT: I only need file names yes! And I should of said that I have 20 000 images in my folder, and 8000 SKUs , so I don't know how we can handle looping on 20 000 images names.
EDIT2: If SKU contains a dash ( - ), I don't need to treat it, so I can pass to the next SKU. And each SKU has a maximum of 5 images (....;SKU-5.jpg)
Thanks all.
How to insert images given you have one image name per cell in a column: How to get images to appear in Excel given image url
Take the above and introduce an inner loop for the file name:
if instr(url_column.Cells(i).Value, "-") = 0 then
dim cur_file_name as string
cur_file_name = dir("imageFolder\" & url_column.Cells(i).Value & "*.jpg")
do until len(cur_file_name) = 0
image_column.Cells(i).Value = image_column.Cells(i).Value & cur_file_name & ";"
cur_file_name = Dir
loop
end if