ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   lookup,copy, paste macro?? (https://www.excelbanter.com/excel-programming/296947-lookup-copy-paste-macro.html)

Hugh Askew

lookup,copy, paste macro??
 
I have searched Google and the groups for this, but unable to find.

Need a macro to take a entered value in any cell on Sheet 1, column A,
look up that value on Sheet 2, column A, go one row over to col. B,
copy the cell, then paste it into Sheet 1, column B, next to the
entered value.
Just like Vlookup, only copypaste .
Need to paste the cell contents, not just values.


any and all help GREATLY appreciated.

Glyn[_3_]

lookup,copy, paste macro??
 
Hi Hugh

If I've understood you correctly then this should do it ...

Sub doIt()

' first get your sheet1 column A lookup value from whichever rowNumber .. let's make it 4
rownumber = 4
lookupValueA = Sheets("sheet1").Cells(rownumber, 1)

'now iterate through your list on sheet2 (assume list ends with first blank cell encountered)
rowPointer = 1
While Sheets("sheet2").Cells(rowPointer, 1) < ""
If Sheets("sheet2").Cells(rowPointer, 1) = lookupValueA Then
lookupValueB = Sheets("sheet2").Cells(rowPointer, 2) ' get adjacent value in column B
End If
rowPointer = rowPointer + 1
Wend

'now put that value in column B in sheet1
Sheets("sheet1").Cells(rownumber, 2) = lookupValueB

' bingo ... it's inefficient in that it looks through the whole of Sheet2's values ... but what the heck!
' btw it assumes no duplication of values in your list sheet2

End Sub

Dave Peterson[_3_]

lookup,copy, paste macro??
 
I would have thought that contents meant values, but if you mean formatting,
too, how about something like this:

Option Explicit
Sub testme()

Dim res As Variant
Dim wks As Worksheet
Dim lookupWks As Worksheet
Dim lookupRng As Range
Dim myCell As Range
Dim myRng As Range

Set wks = Worksheets("sheet1")
Set lookupWks = Worksheets("sheet2")

With lookupWks
Set lookupRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
End With

With wks
Set myRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
For Each myCell In myRng.Cells
If IsEmpty(myCell) Then
'do nothing
Else
res = Application.Match(myCell.Value, lookupRng, 0)
If IsError(res) Then
'no match-what should be done?
Else
lookupRng(res).Offset(0, 1).Copy _
myCell.Offset(0, 1)
End If
End If
Next myCell
End With

End Sub


I used .offset(0,1) in both spots, but you could change each of them to what you
want.


Hugh Askew wrote:

I have searched Google and the groups for this, but unable to find.

Need a macro to take a entered value in any cell on Sheet 1, column A,
look up that value on Sheet 2, column A, go one row over to col. B,
copy the cell, then paste it into Sheet 1, column B, next to the
entered value.
Just like Vlookup, only copypaste .
Need to paste the cell contents, not just values.

any and all help GREATLY appreciated.


--

Dave Peterson


Hugh Askew

lookup,copy, paste macro??
 
Dave, many,many gracious thanks! EXACTLY what was needed!!!



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


All times are GMT +1. The time now is 08:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com