Application.ScreenUpdating

KCLA1785

New Member
Joined
Nov 13, 2014
Messages
19
Hello. :)
I have the following formula which searches another sheet gathers the relevant info then pastes it to Sheet 1 !
Problem is it overwrites all cells in all columns and I would like it to only fill column A to Column H ?
can you help me please ??
VBA Code:
Sub Test2()
Dim myWord$
myWord = InputBox("Type Purchase Order Number Below...", "Purchase Order Number")
If myWord = "" Then Exit Sub

Application.ScreenUpdating = False
Dim xRow&, NextRow&, LastRow&
NextRow = 11
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "*" & myWord & "*") > 0 Then
Rows(xRow).Copy Sheets("Sheet1").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Application.ScreenUpdating = True


End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
At the moment your code copies the whole row Rows(xRow).Copy
Change it to copy just columns 1 to 8 (A:H).
VBA Code:
Range(Cells(xRow, 1), Cells(xRow, 8)).Copy Sheets("Sheet1").Cells(NextRow, 1)

You should also qualify your cells location with the sheet as well (all cell references, so the LastRow definition & the Worksheetfunction).
VBA Code:
Sheets("Sheet2").Range(Sheets("Sheet2").Cells(xRow, 1), Sheets("Sheet2").Cells(xRow, 8)).Copy Sheets("Sheet1").Cells(NextRow, 1)

You could shorten the referencing by using a With... End With code block.

The Application.ScreenUpdating isn't needed... not sure why you've called the question that.
 
Upvote 0
Darren... Your an absolute legend and its not my code, I'm just making repairs to the worksheet but i'm unfamiliar with this kind of copy ?
Anyway thank you very much for your help mate :) its working :)
 
Upvote 0
The Application.ScreenUpdating isn't needed...
@KCLA1785, I would leave the screenupdating = false in there as with the way that you are interacting with the sheet multiple times you are going to get screen flicker and slow the code if you remove it.

Btw, another way to to write the reference to the range is
Rich (BB code):
Range(Sheets("Sheet2").Cells(xRow, "A"), Sheets("Sheet2").Cells(xRow, "H")).Copy Sheets("Sheet1").Cells(NextRow, 1)
which you might find more intuitive.

Or another option is
VBA Code:
Intersect(Sheets("Sheet2").Rows(xRow), Sheets("Sheet2").Range("A:H")).Copy Sheets("Sheet1").Cells(NextRow, 1)
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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