473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Copy to another sheet based on values

I am building a P&L on worksheet(1) based on data from worksheet(2). I need to copy rows of data from worksheet(2) and paste onto worksheet(1). The pasting process needs to find a named range (one cell) on worksheet(1) and insert a row for the copied data above the named cell. In addition, each row on worksheet(2) must first be identified by a cell value in row (d) and be pasted to a corresponding named cell.

Source = Worksheet(2)
Destination Worksheet(1)

Worksheet(2) and has hundreds of rows
Worksheet(2) column (d) has the identifier value(s), like 1000 and 1100, which both need to go to the gross sales section on worksheet(1) –the code below only has 1000.
Worksheet(1) has a named range called RPT_GS. The copied data needs to be inserted above the named range RPT_GS.

The code below is a mess and does not find the named range RPT_GS. If the below code is so ugly, please feel free to start over. I really appreciate any help. Thanks, John


Sub FIND_COPY_PASTE_1000()

Dim DestSheet As Worksheet
Set DestSheet = Worksheets("sheet 1")

Dim sRow As Long
Dim dRow As Long
Dim sCount As Long
sCount = 0
dRow = 1

For sRow = 1 To Range("D360").End(xlUp).Row
If Cells(sRow, "D") Like "1000" Then
sCount = sCount + 0
dRow = dRow + 1

'Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "B")
'Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "C")
'Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")


End If
Next sRow
Mar 4 '08 #1
4 4001
VBWheaties
145 100+
I am building a P&L on worksheet(1) based on data from worksheet(2). I need to copy rows of data from worksheet(2) and paste onto worksheet(1). The pasting process needs to find a named range (one cell) on worksheet(1) and insert a row for the copied data above the named cell. In addition, each row on worksheet(2) must first be identified by a cell value in row (d) and be pasted to a corresponding named cell.

Source = Worksheet(2)
Destination Worksheet(1)

Worksheet(2) and has hundreds of rows
Worksheet(2) column (d) has the identifier value(s), like 1000 and 1100, which both need to go to the gross sales section on worksheet(1) –the code below only has 1000.
Worksheet(1) has a named range called RPT_GS. The copied data needs to be inserted above the named range RPT_GS.

The code below is a mess and does not find the named range RPT_GS. If the below code is so ugly, please feel free to start over. I really appreciate any help. Thanks, John


Sub FIND_COPY_PASTE_1000()

Dim DestSheet As Worksheet
Set DestSheet = Worksheets("sheet 1")

Dim sRow As Long
Dim dRow As Long
Dim sCount As Long
sCount = 0
dRow = 1

For sRow = 1 To Range("D360").End(xlUp).Row
If Cells(sRow, "D") Like "1000" Then
sCount = sCount + 0
dRow = dRow + 1

'Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "B")
'Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "C")
'Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")


End If
Next sRow
Is it not working or something? Not sure what the issue is.
Mar 4 '08 #2
kadghar
1,295 Expert 1GB
I am building a P&L on worksheet(1) based on data from worksheet(2). I need to copy rows of data from worksheet(2) and paste onto worksheet(1). The pasting process needs to find a named range (one cell) on worksheet(1) and insert a row for the copied data above the named cell. In addition, each row on worksheet(2) must first be identified by a cell value in row (d) and be pasted to a corresponding named cell.

...
Your code doesnt seem that bad; the problems i think you might be having are:
  1. When you run the code, you have to make sure your active sheet is the second, or (a better solution) use it as the parent of the cells while searching.
  2. Range("D360").End(xlUp).Row means: You stand in cell D360, then you press Ctrl+upArrow and then you see the row number. If you have no empty spaces in column D, that'll be the first row, and your FOR will go from 1 to 1. Maybe you want to use .End(xlDown)
  3. When you make the copy-paste, you're not inserting a new row, may be you want to insert it before you copy-paste.

With some luck, something like this will solve this three problems:

Expand|Select|Wrap|Line Numbers
  1. Sub FIND_COPY_PASTE_1000()
  2.  
  3.   Dim DestSheet  As Worksheet
  4.   Set DestSheet = Worksheets("sheet 1")
  5.  
  6.   Dim sRow       As Long   
  7.   Dim dRow       As Long     
  8.   Dim sCount     As Long
  9.   sCount = 0
  10.   dRow = 1
  11. with worksheets("sheet2")
  12.   For sRow = 1 To Range("D360").End(-4121).Row '-4121 is the numerical value of xlDown, inside excel, they're the same, outside excel, only the numeric value is good.
  13.         If .Cells(sRow, "D") Like "1000" Then
  14.         sCount = sCount + 0
  15.         dRow = dRow + 1
  16.         Rows(dRow).Insert Shift:=-4121 
  17.         .Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "B")
  18.         .Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "C")
  19.         .Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")
  20.      End If
  21.   Next sRow
  22. end with
  23. end sub
Mar 4 '08 #3
Sorry, the problem is when the 1000 value is found in sheet2 row d, i need the macro to insert a row for each time it finds a 1000 (and copy the entire row the 1000 is contained in).

Thanks!
Mar 4 '08 #4
kadghar
1,295 Expert 1GB
Sorry, the problem is when the 1000 value is found in sheet2 row d, i need the macro to insert a row for each time it finds a 1000 (and copy the entire row the 1000 is contained in).

Thanks!
the code i've just posted will do that ^.^
just change the .end(-4121) to your original .end(xlup)
Mar 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
3
by: david | last post by:
Hi, I've been reading tons of posts on how to copy records, but to no avail....i'm still stuck. There are three tables: Main, Sub-Form1 & Sub-Form2 I have a form which displays some data....
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
2
by: michael sorens | last post by:
I tried to do a simple operation on a Windows Form in VS2005 inside a key_down handler: if (e.Control && e.Shift && e.KeyCode == Keys.V) { int selectedRowIndex =...
13
by: jmpigott | last post by:
I have searched forever trying to solve a problem that simplifies recurring info inputs to my database the basic structure is Orders tbl main form and pricing_tbl subform and third when orders...
0
by: Taxman | last post by:
Windows XP, MS Office Excel 2003 If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve...
0
by: barkarlo | last post by:
I make pivot table in excel. in page field is item month. in row field is employees. in column field is working hours,sunday hours, overtime and ect. Now I want with VBA code copy/paste only...
2
by: Deven Oza | last post by:
Hi, Does anyone have an idea that how to copy column(s) from one excel sheet to another sheet using sql server. Thanks for your help in advance. -Deven
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.