VBA Help - Adding search button to extract data from another worksheet and pasting associated rows to a different worksheet

Quynhtly

New Member
Joined
Apr 12, 2011
Messages
1
Hi,

I am having some trouble with creating coding that would allow me to extract client data from the "Data" worksheet and pasting associated information to a worksheet called "Summary". I have created a button and search function but it seems to fail to pick up any data in the "Data" worksheet - the error message keeps appearing as no data is found.

My aim is to be able to search for a client name. eg John and have all data in column D, H and S copied over to worksheet "Summary" for all rows with "John" in the worksheet "Data".

I want data from worksheet "Data" column D to appear in worksheet "Summary" column B.

I want data from worksheet "Data" column H to appear in worksheet "Summary" column C.

I want data from worksheet "Data" column S to appear in worksheet "Summary" column D.


Below is the coding that i have in place.


Sub AddButton_Click()
Dim iFound
Dim Message$, Title$, Default$, myCode$
Message = "Client Name" ' Set prompt.
Title = "Search Data" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
myCode = InputBox(Message, Title, Default)
iFound = False
Application.ScreenUpdating = False
'Check data sheet for data wanted!
Worksheets("Data").Select
For Each r In Worksheets("Data").UsedRange.Columns
n = r.Column
If Worksheets("Data").Cells(1, n) = myCode Then
iFound = True
Worksheets("ALL").Range(Cells(2, n), Cells(4, n)).Copy _
Destination:=Worksheets("Summary").Range("C:65536").End(xlUp).Offset(1, 0)
Else
End If
Next r
If iFound = False Then MsgBox "Error: Data not Found!"
Worksheets("Summary").Select
Application.CutCopyMode = True
Application.ScreenUpdating = True
End Sub


Thanking you in advance.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
try this macro "test" (the second macro "undo" is to undo the result i.e. clear data from sheet3)


no blank rows or columns within the data


Code:
Sub test()
Dim r As Range, x As String
x = InputBox("type the name you want for e.g. John")
With Worksheets("data")
Set r = .Range("A1").CurrentRegion

'MsgBox r.Address
With r
.Cells.AutoFilter field:=1, Criteria1:=x
End With
.Cells.SpecialCells(xlCellTypeVisible).Copy
With Worksheets("summary")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
r.AutoFilter
End With
Worksheets("summary").Activate
Range("B1:C1,E1:G1,I1:R1").Select
Selection.EntireColumn.Delete
Range("A1").Select
End Sub

Code:
Sub undo()
Worksheets("summary").Cells.Clear
End Sub

check results with sheet "data"

BEFORE USING THE MACROS IN ORIGINAL FILE SAVE ORIGINAL FILE SAFELY SOMEWHERE.
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

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