473,670 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy to another sheet based on values

3 New Member
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("she et 1")

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

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

'Cells(sRow, "F").Copy Destination:=De stSheet.Cells(d Row, "B")
'Cells(sRow, "E").Copy Destination:=De stSheet.Cells(d Row, "C")
'Cells(sRow, "D").Copy Destination:=De stSheet.Cells(d Row, "D")


End If
Next sRow
Mar 4 '08 #1
4 4021
VBWheaties
145 New Member
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("she et 1")

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

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

'Cells(sRow, "F").Copy Destination:=De stSheet.Cells(d Row, "B")
'Cells(sRow, "E").Copy Destination:=De stSheet.Cells(d Row, "C")
'Cells(sRow, "D").Copy Destination:=De stSheet.Cells(d Row, "D")


End If
Next sRow
Is it not working or something? Not sure what the issue is.
Mar 4 '08 #2
kadghar
1,295 Recognized Expert Top Contributor
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").E nd(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
JDVOIGT
3 New Member
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 Recognized Expert Top Contributor
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
5759
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
5
2679
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
3211
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. The PK (ProductID) for each record on the form is an AUTONUMBER. This form also has a sub-form, linked via ProductID. The subform also
26
21316
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
14687
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 = dataGridView.SelectedCells.RowIndex; dataGridView.Rows.AddCopy(selectedRowIndex); } So when the user presses Ctrl-Shft-V, a copy of the first row of a user's
13
3246
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 have recurring pricing structure, query pricing history for location and product, then select appropriate costing method from filtered list. This historical data sheet (subform 2) would be optional method for populating costing. I have run across...
0
3201
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 been searching for code for each part of the task separately and trying to piece together multiple macros, that do something similar, to what I’m trying to accomplish in my over all task, but I’m not having a lot of luck. So, here’s the entire task,...
0
1188
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 value of column overtime from pivot to another sheet. How?
2
3536
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
8469
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.