LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Vlookup in VBA

Hi,

I am working with a report that is downloaded into Excel. The format cannot
be changed, so I have to reformat in order to generate a pivot table.

Here is an example of the layout:
John Smith 54
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Totals for John Smith

Mary Jones 88
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Field 1 Field 2 Field 3 Field 4
Totals for Mary Jones

Etc.

The lines of data vary per employee and per report. I am creating a macro
that will automate the process, but am encountering a problem with my method.

I insert two headings to the right of Field 4 called EmployeeName and
EmployeeNo.

I then create a Vlookup that looks the combined information in the field at
the top of each section.

It works fine, except that the references aren't relative, so if the number
of rows change, the vlookup doesn't work.

Here is part of my code:

ActiveCell.FormulaR1C1 = "=VLOOKUP(R2C1,Employees,2,0)"
Range("Q3").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R2C1,Employees,3,0)"
Range("P3:Q3").Select
Selection.AutoFill Destination:=Range("P3:Q13")
Range("P3:Q13").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Rows("2:2").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A2").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Rows("14:14").Select
Selection.Delete Shift:=xlUp
Range("A16").Select
Selection.End(xlToRight).Select
Range("P16").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R15C1,Employees,2,0)"
Range("Q16").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R15C1,3,0)"
Range("Q16").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R15C1,Employees,3,0)"
Range("P16:Q16").Select
Selection.AutoFill Destination:=Range("P16:Q29")

I have to resolve the number of times this repeats as well, but would like
to resolve this first!

Thanks for any assistance.


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If (Vlookup 0) working, but what if Vlookup cell does not exist Steve Excel Worksheet Functions 18 November 18th 09 07:33 PM
Vlookup problem - unable to get the vlookup property Fred Excel Programming 2 August 22nd 08 05:23 PM
using a vlookup to enter text into rows beneath the vlookup cell Roger on Excel Excel Programming 1 November 29th 07 12:09 PM
Combine VLOOKUP and IF function so #NA isn't returned as a value from VLOOKUP buffgirl71 Excel Discussion (Misc queries) 12 November 14th 06 11:36 PM
Which is faster: VLOOKUP-worksheet or VB-array VLOOKUP? erikhs[_20_] Excel Programming 1 August 6th 06 06:18 PM


All times are GMT +1. The time now is 02:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"