Macro to locate a reference number in a table and edit the full row

skf786

Board Regular
Joined
Sep 26, 2010
Messages
156
Hello,

i have a table with headers in c31:h31, reference number being in column C. i am using a macro to add data to this table.

i am looking to add a 'Modify data' macro button to the sheet which picks up the reference number given in cell c25 (outside the table), then locates this reference number in column C of the table then replaces the full row with the contents of cells c25:h25.

Many thanks in advance,

SKF
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this. Change Table and sheet names if necessary
VBA Code:
Sub FindReplace()

Dim LookValue  As String
Dim rngSearch As Range, rngFound As Range
Dim ws As Worksheet
Dim Tbl As ListObject

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set Tbl = ws.ListObjects("Table1")

LookValue = ws.Range("C25")
Set rngFound = Tbl.DataBodyRange.Columns(1).Find(LookValue, LookAt:=xlWhole)

If Not rngFound Is Nothing Then
    ws.Range("C25", "H25").Copy rngFound
End If

End Sub
 
Upvote 0
If You have no table
just try
VBA Code:
Sub test()
Cells(Range("C25") + 30, 3).Resize(, 6) = Range("c25:h25").Value
End Sub
 
Upvote 0
Try this. Change Table and sheet names if necessary
VBA Code:
Sub FindReplace()

Dim LookValue  As String
Dim rngSearch As Range, rngFound As Range
Dim ws As Worksheet
Dim Tbl As ListObject

Set ws = ActiveWorkbook.Sheets("Sheet1")
Set Tbl = ws.ListObjects("Table1")

LookValue = ws.Range("C25")
Set rngFound = Tbl.DataBodyRange.Columns(1).Find(LookValue, LookAt:=xlWhole)

If Not rngFound Is Nothing Then
    ws.Range("C25", "H25").Copy rngFound
End If

End Sub
thanks Zot, this works perfectly
 
Upvote 0
Dear Zot,

im trying to use the attached macro on a protected sheet. im getting an error. can you please help me fix it. Also is it possible to paste 'special' into the table as 'Values'

thanks again for your help.
VBA Code:
Sub FindReplace()

With ActiveSheet
      .Unprotect "7860"



Dim LookValue  As String
Dim rngSearch As Range, rngFound As Range
Dim ws As Worksheet
Dim Tbl As ListObject

Set ws = ActiveWorkbook.Sheets("1")
Set Tbl = ws.ListObjects("Table134568919")

LookValue = ws.Range("d22")
Set rngFound = Tbl.DataBodyRange.Columns(1).Find(LookValue, LookAt:=xlWhole)

If Not rngFound Is Nothing Then
    ws.Range("d22", "k22").Copy rngFound
End If


 .Protect "7860"


End Sub
 
Upvote 0
I think it was just because you forget the With End statement ;)

I re-arrange the code and change the ActiveSheet to ws since it has been declared to save typing ?. Now using Paste Special for value only. Somehow using PasteSpecial you cannot make it in single line because it will give error. Nee to move to next line. Not sure the reason because I'm not expert. ?
VBA Code:
Sub FindReplace()

Dim LookValue  As String
Dim rngSearch As Range, rngFound As Range
Dim ws As Worksheet
Dim Tbl As ListObject

Set ws = ActiveWorkbook.Sheets("1")
Set Tbl = ws.ListObjects("Table134568919")

With ws
    .Unprotect "7860"
    LookValue = .Range("d22")
    Set rngFound = Tbl.DataBodyRange.Columns(1).Find(LookValue, LookAt:=xlWhole)
    If Not rngFound Is Nothing Then
        .Range("d22", "k22").Copy
        rngFound.PasteSpecial (xlPasteValues)
    End If
    .Protect "7860"
 End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top