Hi Yossy,
I'm happy for you that my code helped. On you question of learning, there
are lots of books available. I am lucky where I live because we have free
libraries and they have a good selection of books on Excel. Try for one that
specifically says it is for Excel VBA Programming. One that I particularly
like is VBA and Macros for Microsoft Excel by Bill Jelen.
Once you improve your knowledge a little then spend some time on the forum
and try to solve other peoples problems. doesn't matter if you don't post the
solutions, just try to solve them and then look at what other people have
replied.
--
Regards,
OssieMac
Yossy said:
THankssssssssssssssssss OssieMac. It Works great. Can you advise me on ways
to improve macro skills. Any/all recommendation would be totally appreciated.
I have little knowledge but not indept. What do I need to know and better
grasp to be very good in writing Macro.
Thanks so much!
OssieMac said:
Hi Yossy,
I was not sure whether you wanted the vlookup in the next cell under the
title or if there is other data under the title and you wanted it in the
first blank cell so I found the first blank cell.
Also I changed the find to remove the wild cards. Changing xlWhole to xlPart
removes the need for the wild cards at beginning and end of a string. You
only need the wild cards if they are used in lieu of middle characters like
this.
"Tile in*the Period"
If you check out the cell where the vlookup formula is inserted then you
will be able to see what the concatenation of the string variables is doing
where the code creates the vlookup formula.
I have inserted lots of comments so hope it does what you want but feel free
to get back to me if you need more help.
By the way this looks like it might be a student assignment. Is my
assumption correct?
Sub Do_Vlookup()
Dim wbThis As Workbook 'This workbook
Dim wbData As Workbook 'Vlookup range workbook
Dim strWbData As String 'Name of workbook with Vlookup range
Dim strShtData As String 'Name of worksheet with Vlookup range
Dim sh As Worksheet 'Each worksheet
Dim objCellToFind As Object 'Cell to find value in
Dim targetCol As String 'Column for find
Dim targetCell As Range 'Cell for vlookup formula
'Assign this workbook to a variable
Set wbThis = ThisWorkbook
'Attempt to assign data workbook to a variable
'will return error if not open and hense the
'On Error routine
On Error Resume Next
'Edit to your data workbook name
Set wbData = Workbooks("Data for Yossy.xlsm")
On Error GoTo 0
'If data workbook is not already open then
'open the data workbook and assign it to a variable
If wbData Is Nothing Then
'Replace path and workbook name with your path and data workbook name
Set wbData = Workbooks.Open _
("C:\Users\Peter\Documents\Excel\Test Macros\Data for Yossy.xlsm")
End If
'Assign data workbook name to string variable
'that is enclosed in square brackets for use in
'Vlookup formula
strWbData = "[" & wbData.Name & "]"
'Assign data sheet name to a string variable
'that is enclosed in square brackets for Vlookup formula
'Edit to your data worksheet name
strShtData = "Sheet1"
'I have assumed that the title is still in
'column A as per the previous code I helped you with.
targetCol = "A"
For Each sh In wbThis.Sheets
With sh
Set objCellToFind = .Columns(targetCol). _
Find(What:="Tile in month for the Period", _
after:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If objCellToFind Is Nothing Then
'*Tile in month for the Period* not found
'so bypass code down to Next.
GoTo byPassLabel
Else
'Find first blank cell under title
Set targetCell = objCellToFind
Do
Set targetCell = targetCell.Offset(1, 0)
Loop Until targetCell = ""
End If
'Insert Vlookup formula in cell.
'Edit the range to suit your range
targetCell = "=VLOOKUP(E5,'" & strWbData & strShtData & _
"'!$A$2:$B$27,2,FALSE)"
End With
byPassLabel:
Next
End Sub