Copying Entirerow to Defined Cell

epoiezam

New Member
Joined
Jan 28, 2016
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Hi All:

When the criteria is met, I want vba to copy entirerow from external workbook/s to this workbook starting from a selected cell. Example at cell (C2) instead of (A2) as per current formula.

:)


Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String


On Error GoTo ErrHandler
Application.ScreenUpdating = False


'Change as desired
strPath = UserForm1.Label1.Caption
strSearch = UserForm1.TextBox1.Value


Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)


'strFile = Dir(strPath & "\*.xls*")
strFile = Dir(strPath & "\*.xlsx")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(FileName:=strPath & "" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)


For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then

Exit Do
Else
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4).EntireRow.Value = rFound.EntireRow.Value
End If
Set rFound = wks.Range("K:K").FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next


wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"


ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub


ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Change this
Code:
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"


to this
Code:
With wOut
.Cells(lRow, 3) = "Workbook"
.Cells(lRow, 4) = "Worksheet"
.Cells(lRow, 5) = "Cell"
.Cells(lRow, 6) = "Text in Cell"


and this

Code:
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4).EntireRow.Value = rFound.EntireRow.Value
to this

Code:
lRow = lRow + 1
.Cells(lRow, 3) = wbk.Name
.Cells(lRow, 4) = wks.Name
.Cells(lRow, 5) = rFound.Address
.Cells(lRow, 6).EntireRow.Value = rFound.EntireRow.Value
 
Upvote 0
Thank you.

The label works nicely at cell (C1). But for the result, they still starts at (A2).

Plus they override :-

.Cells(lRow, 3) = wbk.Name
.Cells(lRow, 4) = wks.Name
.Cells(lRow, 5) = rFound.Address
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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