Copy values to new sheet / next empty row

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
Hi there, I have the following code. It copies values from sheet 1 to sheet 2, with the range of A2:J999. The issue, is that the range is actually dynamic. Sometimes, I just need A2:J10, other times, A2:J25.

Should I be performing this function using a different method? Throughout this function, I also want to add a value "Not Exported" to column K. Is it easy to append this value to each row as part of this function?

Thank you all very much, in advance :) Have a great night.

Code:
Function CopyResults()

  Application.ScreenUpdating = False
  Dim copySheet As Worksheet
  Dim pasteSheet As Worksheet


  Set copySheet = Worksheets("1")
  Set pasteSheet = Worksheets("2")


  copySheet.Range("A2:J999").Copy
  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You can't move rows with a Function.
A function will only return a result, not carry out actions.

Try

Code:
Sub MM1()
Application.ScreenUpdating = False
Dim WS1 As Worksheet, WS2 As Worksheet, lr As Long

Set WS1 = Worksheets("1")
Set WS2 = Worksheets("2")
lr = WS1.Cells(Rows.Count, "J").End(xlUp).Row

WS1.Range("K2:K" & lr).Value = "Not Exported"
WS1.Range("A2:J" & lr).Copy
WS2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
 
Upvote 0
What do you mean by not being able to move rows? I'm just trying to copy. Any idea on appending the word not exported? Maybe I should create a function for that before it copies data over to the other sheet.
 
Upvote 0
I'm curious.
You said:
Hi there, I have the following code. It copies values from sheet 1 to sheet 2, with the range of A2:J999.

How is it possible you have a Function copying data to another sheet?

What do you mean by not being able to move rows? I'm just trying to copy. Any idea on appending the word not exported? Maybe I should create a function for that before it copies data over to the other sheet.
 
Upvote 0
See the 2nd line of my post...copy OR move
A function will only return a result, not carry out actions.

Code:
Any idea on appending the word not exported?

I have included the line for that in the code.

Have you tried the code I posted ?
 
Upvote 0
You got me... no I didn't. Added a couple items for formatting... and this works great. I have an off topic question, not sure if there is an answer.

Earlier in my macro I do text to columns to split out some text. And later on, if I copy more text in a similar format, it is as if the text to column setting is cached, as it parses the new data. Is there any way to prevent this from happening without exiting excel completely?
 
Upvote 0
Maybe you should post ALL the code involved.
But normally, the text to columns doesn't set to any particular setting
 
Upvote 0
I figured it out :) Did a new text to column, and made delimited by space = false, which turned this feature off.
 
Upvote 0
OK...glad you got it sorted AND thanks for sharing the solution...:beerchug:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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