Copy and paste bug

Squareroot

New Member
Joined
Nov 9, 2022
Messages
15
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I am trying to copy and paste some data from one sheet to another. I created a function to just do the copy paste becuse I have to do that for multiple headers. below is my code

Function insert(header)
Sheets(2).Activate
Sheets(2).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True

Range(ActiveCell, Range(ActiveCell.Address).End(xlDown)).Select
Selection.Copy
Sheets(1).Select
Sheets(1).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.Offset(2, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Function


''''''''''''''''''''''''''''''''''''''''''''''''main code''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
....
...
.
insert ("Supplier")

insert ("Description")

insert ("DwgNo")

insert ("QTY")

The code works well when I run it line by line using the F8 key. but when I run the code normally, it will have bug. The data under the supplier header will be pasted indefinitely until the entire column of my sheet 1 is filled. does anyone know what is wrong with my code?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,

I am trying to copy and paste some data from one sheet to another. I created a function to just do the copy paste becuse I have to do that for multiple headers. below is my code

Function insert(header)
Sheets(2).Activate
Sheets(2).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True

Range(ActiveCell, Range(ActiveCell.Address).End(xlDown)).Select
Selection.Copy
Sheets(1).Select
Sheets(1).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.Offset(2, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Function


''''''''''''''''''''''''''''''''''''''''''''''''main code''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
....
...
.
insert ("Supplier")

insert ("Description")

insert ("DwgNo")

insert ("QTY")

The code works well when I run it line by line using the F8 key. but when I run the code normally, it will have bug. The data under the supplier header will be pasted indefinitely until the entire column of my sheet 1 is filled. does anyone know what is wrong with my code?
To add on, not too sure if this is the reason, I have a duplicated header at sheet 1. I have 1 supplier header in row1 and another in row 2
 
Upvote 0
Found reason.
Hi,

I am trying to copy and paste some data from one sheet to another. I created a function to just do the copy paste becuse I have to do that for multiple headers. below is my code

Function insert(header)
Sheets(2).Activate
Sheets(2).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True

Range(ActiveCell, Range(ActiveCell.Address).End(xlDown)).Select
Selection.Copy
Sheets(1).Select
Sheets(1).Cells.Find(What:=header, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.Offset(2, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Function


''''''''''''''''''''''''''''''''''''''''''''''''main code''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
....
...
.
insert ("Supplier")

insert ("Description")

insert ("DwgNo")

insert ("QTY")

The code works well when I run it line by line using the F8 key. but when I run the code normally, it will have bug. The data under the supplier header will be pasted indefinitely until the entire column of my sheet 1 is filled. does anyone know what is wrong with my code?
Please ignore.. found the reason.
 
Upvote 0

Forum statistics

Threads
1,224,731
Messages
6,180,614
Members
452,991
Latest member
JM_000888

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