vLOOK UP INTO vba

  • Thread starter jln via OfficeKB.com
  • Start date
J

jln via OfficeKB.com

Here is the recorded macro that i did. I need to change it to count the
number of records and match that with what the vlook up needs to do. The
amount of records changes for each file that i work on.


Sub LPMIVlookup()
'
' LPMIVlookup Macro
' Macro recorded 10/20/2006 by J922703
'

'
Range("A1:Y1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("E19").Select
ActiveWindow.SmallScroll ToRight:=14
Sheets("MGICLPMI").Select
Range("A1:O1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Sheets("Modem").Select
Range("Z2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-24],MGICLPMI!R1C4:R635C14,11,0)"
Selection.AutoFill Destination:=Range("Z2:Z2439")
Range("Z2:Z2439").Select
Selection.Copy
Range("Z2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("AA2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-13]-RC[-1]"
Range("AA2").Select
Selection.AutoFill Destination:=Range("AA2:AA2439")
Range("AA2:AA2439").Select
Selection.Copy
ActiveWindow.SmallScroll ToRight:=-4
Range("N2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("O2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O2442")
Range("O2:O2442").Select
Selection.Copy
Range("O2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
 
B

Bernie Deitrick

Try the macro below.

HTH,
Bernie
MS Excel MVP

Sub LPMIVlookup2()
' LPMIVlookup Macro
' Macro recorded 10/20/2006 by J922703

Dim myR1 As Range
Dim myR2 As Range
Dim myR3 As Range
Dim myR4 As Range

Set myR1 = Range("A1:Y1")
Set myR1 = Range(myR1, myR1.End(xlDown))
myR1.Sort Key1:=myR1.Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Set myR2 = Sheets("MGICLPMI").Range("A1:O1")
Set myR2 = Range(myR2, myR2.End(xlDown))
myR2.Sort Key1:=myR2.Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Set myR3 = Sheets("Modem").Range("Z2:Z2439")
myR3.FormulaR1C1 = "=VLOOKUP(RC[-24],MGICLPMI!" & myR2.Address(True, True, xlR1C1) & ",11,0)"
myR3.Copy
myR3.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
myR3.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Set myR4 = Sheets("Modem").Range("AA2:AA2439")
myR4.FormulaR1C1 = "=RC[-13]-RC[-1]"
myR4.Copy
Sheets("Modem").Range("N2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Set myR4 = Sheets("Modem").Range("O2:O2442")
myR4.FormulaR1C1 = "=RC[-2]+RC[-1]"
myR4.Copy
Sheets("Modem").Range("O2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub


jln via OfficeKB.com said:
Here is the recorded macro that i did. I need to change it to count the
number of records and match that with what the vlook up needs to do. The
amount of records changes for each file that i work on.


Sub LPMIVlookup()
'
' LPMIVlookup Macro
' Macro recorded 10/20/2006 by J922703
'

'
Range("A1:Y1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("E19").Select
ActiveWindow.SmallScroll ToRight:=14
Sheets("MGICLPMI").Select
Range("A1:O1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Sheets("Modem").Select
Range("Z2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-24],MGICLPMI!R1C4:R635C14,11,0)"
Selection.AutoFill Destination:=Range("Z2:Z2439")
Range("Z2:Z2439").Select
Selection.Copy
Range("Z2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("AA2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-13]-RC[-1]"
Range("AA2").Select
Selection.AutoFill Destination:=Range("AA2:AA2439")
Range("AA2:AA2439").Select
Selection.Copy
ActiveWindow.SmallScroll ToRight:=-4
Range("N2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("O2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O2442")
Range("O2:O2442").Select
Selection.Copy
Range("O2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
 
B

Bernie Deitrick

The line

Set myR1 = Range("A1:Y1")

Should also have the sheet name:

Set myR1 = Sheets("Sheet Name").Range("A1:Y1")

Sorry about that - it will default to the active sheet otherwise....


HTH,
Bernie
MS Excel MVP


Bernie Deitrick said:
Try the macro below.

HTH,
Bernie
MS Excel MVP

Sub LPMIVlookup2()
' LPMIVlookup Macro
' Macro recorded 10/20/2006 by J922703

Dim myR1 As Range
Dim myR2 As Range
Dim myR3 As Range
Dim myR4 As Range

Set myR1 = Range("A1:Y1")
Set myR1 = Range(myR1, myR1.End(xlDown))
myR1.Sort Key1:=myR1.Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Set myR2 = Sheets("MGICLPMI").Range("A1:O1")
Set myR2 = Range(myR2, myR2.End(xlDown))
myR2.Sort Key1:=myR2.Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Set myR3 = Sheets("Modem").Range("Z2:Z2439")
myR3.FormulaR1C1 = "=VLOOKUP(RC[-24],MGICLPMI!" & myR2.Address(True, True, xlR1C1) & ",11,0)"
myR3.Copy
myR3.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
myR3.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Set myR4 = Sheets("Modem").Range("AA2:AA2439")
myR4.FormulaR1C1 = "=RC[-13]-RC[-1]"
myR4.Copy
Sheets("Modem").Range("N2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Set myR4 = Sheets("Modem").Range("O2:O2442")
myR4.FormulaR1C1 = "=RC[-2]+RC[-1]"
myR4.Copy
Sheets("Modem").Range("O2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub


jln via OfficeKB.com said:
Here is the recorded macro that i did. I need to change it to count the
number of records and match that with what the vlook up needs to do. The
amount of records changes for each file that i work on.


Sub LPMIVlookup()
'
' LPMIVlookup Macro
' Macro recorded 10/20/2006 by J922703
'

'
Range("A1:Y1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("E19").Select
ActiveWindow.SmallScroll ToRight:=14
Sheets("MGICLPMI").Select
Range("A1:O1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Sheets("Modem").Select
Range("Z2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-24],MGICLPMI!R1C4:R635C14,11,0)"
Selection.AutoFill Destination:=Range("Z2:Z2439")
Range("Z2:Z2439").Select
Selection.Copy
Range("Z2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("AA2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-13]-RC[-1]"
Range("AA2").Select
Selection.AutoFill Destination:=Range("AA2:AA2439")
Range("AA2:AA2439").Select
Selection.Copy
ActiveWindow.SmallScroll ToRight:=-4
Range("N2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("O2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O2442")
Range("O2:O2442").Select
Selection.Copy
Range("O2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
 
J

jln via OfficeKB.com

Hey i started looking at this today and wasnt sure what is beeing done in the
vlookup, also is there a way for it to see if the sheet is there. Sometime I
dont have both sheets.

Bernie said:
The line

Set myR1 = Range("A1:Y1")

Should also have the sheet name:

Set myR1 = Sheets("Sheet Name").Range("A1:Y1")

Sorry about that - it will default to the active sheet otherwise....

HTH,
Bernie
MS Excel MVP
Try the macro below.
[quoted text clipped - 106 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top