Questions and a problem: The questions are, do you want to copy all of the rows from the “data to copy & paste” to the appropriate areas of the “New Worksheet,” or do you want to copy just one particular company. If just one company, how will the code know which one? what do you want the code to do if there is not a matching company number on the “New Worksheet?” The problem is that the code in the workbook you uploaded is password protected, so we can’t test the code to do what you want.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Copy & Paste based on Variable (Excel 2002)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Copy & Paste based on Variable (Excel 2002)
- This topic has 20 replies, 3 voices, and was last updated 18 years, 10 months ago.
AuthorTopicH. Legare Coleman
AskWoody PlusMay 4, 2006 at 5:35 pm #431679Viewing 2 reply threadsAuthorReplies-
WSLJM
AskWoody LoungerMay 4, 2006 at 5:41 pm #1010876Hello!
I need help as usual… my search in this forum found a few posts that may be similar, but because I’m not up on the code (yet!) I can’t tell if it will do what I need. I’ve attached a worksheet as an example… what I’m in need of doing is to transfer a large amount of data from one worksheet to another based on a variable. For example, there may be 10 lines out of 150 lines that need to be copied over to the next worksheet, however they need to go in a certain place in the new worksheet. The variable is the company number which is built into our department number… for example the company number is 75 and the data that needs to be copied has a department number of 75R&D. I’d love a macro that could search the data and find all lines that have a “75” in the left 2 characters, and then copy and paste those lines into the next worksheet… BUT it has to paste it in a spot where company 75’s lines need to be. Basically I have about 10 company numbers, which means I’ll have 10 areas on the new worksheet where the 10 separate sets of data need to be pasted… man, I hope my spreadsheet makes more sense?!?!?! Any help on this would be greatly appreciated!!
THANKS!
Lana -
WSLJM
AskWoody LoungerMay 4, 2006 at 5:50 pm #1010900Hi Legare,
Sorry about that… for example purposes I had generalized a file that had a macro in it that someone else wrote, therefore the password. It wasn’t anything I wrote, so it had nothing to do with my copy & paste dilemma. I copied my example to a “clean” workbook and re-attached to the original question.
To answer the question in your reply, I would like to copy ALL of the rows from the “data to copy & paste” to the appropriate areas of the “New Worksheet”. One thing to keep in mind is that the data in the “data to copy & paste” worksheet may be 150 lines one month and 250 lines long the next, so it varies. I planned on setting up the 10 journal entry templates in the “New Worksheet” with plenty of space between each to accomodate the copying & pasting of the varying lengths of data from the “data to copy & paste” worksheet.
To answer your next question, there will always be a journal entry template for EVERY company, so we shouldn’t have a problem there. Also keep in mind that the first two digits (Left,2) in the department code (in the example 75R&D) is the company number, so I thought we could read the left two digits to determine which got moved and to where?!?! Does that make sense?
Thanks!
Lana -
H. Legare Coleman
AskWoody PlusMay 4, 2006 at 6:34 pm #1010912Does this do what you want?
Public Sub CopyData()
Dim oSrc As Worksheet, oDst As Worksheet
Dim I As Long, J As Long, K As Long, lDMax As Long, lSMax As Long
Dim strCpy As String
Set oSrc = Worksheets("data to copy & paste")
Set oDst = Worksheets("New Worksheet")
lDMax = oDst.Range("A65536").End(xlUp).Row - 1
lSMax = oSrc.Range("A65536").End(xlUp).Row - 1
For I = 0 To lDMax
If oDst.Range("B1").Offset(I, 0).Value = "Financial Division" Then
strCpy = oDst.Range("B1").Offset(I + 1, 0).Value
K = 0
For J = 0 To lSMax
If oSrc.Range("A1").Offset(J, 0).Value "" Then
If Left(oSrc.Range("A1").Offset(J, 0).Value, 2) = strCpy Then
oDst.Range("A1").Offset(I + 4 + K).Value = oSrc.Range("A1").Offset(J, 0).Value
oDst.Range("B1").Offset(I + 4 + K).Value = oSrc.Range("A1").Offset(J, 1).Value
oDst.Range("K1").Offset(I + 4 + K).Value = oSrc.Range("A1").Offset(J, 2).Value
K = K + 1
End If
End If
Next J
End If
Next I
End Sub
-
WSLJM
AskWoody Lounger -
WSLJM
AskWoody LoungerJune 23, 2006 at 12:59 am #1017451Hi Legare… you were so kind to help me out with a great macro, and now I’m changing my mind on what I want it to do… I’m hoping to tweak it a bit further to automate it even more. Rest assured, I’ve learned so much from you and Hans in regards to Macros, however I’m unable (YET) to actually write the code, just a bit of tweaking at this point. Anyway, below is what I’d like to change around on the original macro that you had written for me… I’m hoping it is doable (if that is a word)… these days, I’m pretty confident that almost anything is possible as you guys & gals on Woody’s Lounge are brilliant!! Anyway, here is my wish list…
Please refer to the attached document…
*I’d like to copy & paste A7 and B7 to the New Worksheet A15 and B15
*The narrative in D7 to go over to G15 in the New Worksheet
*The dollar amount in cell C7 to go over to the New Worksheet K15Then it would scan down and recognize A10 is also company 75 (as the first two digits of the Unit are the company number) and need to go over to the new worksheet on the line below the one mentioned above.
Then it would re-scan the data looking for the next incremental number (83 in this case), and copy all of the data in this worksheet over to a NEW journal entry template in the “New Worksheet” following the same concept in paragraph one above.
Legare has already created a macro to do this, however I’d like to ADD to it if possible…
Instead of having a journal entry template for each company already created in the “New Worksheet”, once the next company’s data is ready to be transferred (in this case company 83), I’d like to have the macro copy the “New Worksheet” range A11 thru I15 (as row 15 will have formula’s in it) and place these copied cells 3 rows AFTER the last row of the previous company’s data (in this case after company 75’s data).
Of Course then since row 15 was copied (due to the need of formulas in a few of those cells) and now contain some unit, nature, amount, & narrative data that pertain to the previous company number (A24,B24,&G24 in this case) need to be “cleared out” so that company 83’s data can be copied over to these cells.
To complicate it, I do not want any of the zero dollar amount lines to be copied over to the “New Worksheet”… they should just be ignored.
As always… thanks for the help!!
Lana -
H. Legare Coleman
AskWoody PlusJune 23, 2006 at 5:39 pm #1017558I read your post a little earlier this morning and I was hoping to have time to respond. Unfortunately, I am preparing to leave town for two weeks of vacation and, after taking a look, your request is going to take a little more time than I am going to have available before I leave. Hopefully, someone else will jump in and give you some help. If you don’t get what you need, give me another post after July 9th and I will see what I can do.
-
WSHansV
AskWoody Lounger -
WSLJM
AskWoody LoungerJune 24, 2006 at 12:26 am #1017608Thanks Hans… this works great! You’ll be proud to know that I am learning something from you!! I changed the code line below to be “not equal” to zero as opposed to “greater than” zero as some of the amounts will be a credit value, thus be less than zero… I neglected to include that tidbit in my wish list… anyway, I was totally impressed with myself that my change actually worked… soon I hope to have just a sliver of the knowledge you possess in regards to VBA and Excel. You are truly talented. Thanks again Hans!
Lana
If wshSource.Range(“C” & lngSourceRow) 0 Then -
WSLJM
AskWoody LoungerJune 27, 2006 at 5:41 pm #1018193Hi Hans,
I’d like to change the code to copy and paste “values” as opposed to just paste… the data I’m copying has formula’s as it’s read from a pivot table for summarization purposes, so the current macro is sorting the columns and then the formula’s get copied to the target worksheet. In looking at the code I see a “copy destination” which I’m assuming is the “paste” part, but as you know I’m not trained on all the commands yet so I’m unsure how to get it to “paste values” instead. Of course, I see in the code that there are two copy destination functions, and I only want to change the movement of the Source data to the Target worksheet. The copying of A11:I14 in the target worksheet works fine, so no need to change that part of the code to “paste values”. If you could explain breifly in words what it is doing that would be great as well… I’ve studied it numerous times and I’ve caught on to part of it, but the paste part has eluded me.
Thanks as always!
Lana -
WSHansV
AskWoody LoungerJune 27, 2006 at 6:06 pm #1018195One way would be to use Paste Special with the option to paste only values, but it is even simpler to set (the value of) the target cell to (the value of) the source cell: change the lines
wshSource.Range(“A” & lngSourceRow).Copy Destination:=wshTarget.Range(“A” & lngTargetRow)
wshSource.Range(“B” & lngSourceRow).Copy Destination:=wshTarget.Range(“B” & lngTargetRow)
wshSource.Range(“C” & lngSourceRow).Copy Destination:=wshTarget.Range(“K” & lngTargetRow)
wshSource.Range(“D” & lngSourceRow).Copy Destination:=wshTarget.Range(“G” & lngTargetRow)to
wshTarget.Range(“A” & lngTargetRow) = wshSource.Range(“A” & lngSourceRow)
wshTarget.Range(“B” & lngTargetRow) = wshSource.Range(“B” & lngSourceRow)
wshTarget.Range(“K” & lngTargetRow) = wshSource.Range(“C” & lngSourceRow)
wshTarget.Range(“G” & lngTargetRow) = wshSource.Range(“D” & lngSourceRow)Just for educational purposes: the code to Paste Special is a bit longer – you have to use separate instructions to copy and to paste:
wshSource.Range(“A” & lngSourceRow).Copy
wshTarget.Range(“A” & lngTargetRow).PasteSpecial Paste:=xlPasteValues
wshSource.Range(“B” & lngSourceRow).Copy
wshTarget.Range(“B” & lngTargetRow).PasteSpecial Paste:=xlPasteValues
wshSource.Range(“C” & lngSourceRow).Copy
wshTarget.Range(“K” & lngTargetRow).PasteSpecial Paste:=xlPasteValues
wshSource.Range(“D” & lngSourceRow).Copy
wshTarget.Range(“G” & lngTargetRow).PasteSpecial Paste:=xlPasteValues -
WSLJM
AskWoody LoungerJune 27, 2006 at 7:43 pm #1018238Hi Hans… of course the code works, but my concept doesn’t, as the sort causes a problem with the data due to the formulas I have, therefore causing the data to be copied to the target worksheet incorrectly… anyway, I’ve resorted to recording the following macro
Columns(“F:I”).Select
Selection.Copy
Range(“A1”).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Subto copy and paste values of the “source” data, then the sort and subsequent copying & pasting into the “target” worksheet won’t be a problem… I’m getting frustrated because I tried to copy the recorded macro and input it into your code and obviously putting it in the wrong place as it keeps giving me the “debug” error. Where / how can I place this recorded code into your code… it needs to be the first thing that happens… prior to the sort.
As another option, I also tried to “call” the recorded macro AND “call” your code via ONE command button, but apparently I’m only allowed ONE call per button??? Is that right?
Thanks for the help!
Lana -
WSHansV
AskWoody LoungerJune 27, 2006 at 8:01 pm #1018250Below the instruction
Set wshSource = …
insert the lines
wshSource.Columns(“F:I”).Copy
wshSource.Range(“A1”).PasteSpecial Paste:=xlPasteValuesA button can execute only one macro, but that macro can call other macros if necessary:
Sub ButtonClick()
Call Macro1
Call Macro2
Call Macro3
End Sub -
WSLJM
AskWoody LoungerJune 27, 2006 at 9:39 pm #1018263 -
WSHansV
AskWoody LoungerJune 27, 2006 at 9:44 pm #1018274 -
WSLJM
AskWoody Lounger -
WSHansV
AskWoody LoungerJune 27, 2006 at 10:11 pm #1018277 -
WSLJM
AskWoody Lounger -
WSHansV
AskWoody LoungerJune 27, 2006 at 10:23 pm #1018279Yep, by adding an argument to the Sort method: change
wshSource.Range(“A” & lngSourceRow).CurrentRegion.Sort _
Key1:=wshSource.Range(“A” & (lngSourceRow – 1)), Header:=xlYesto
wshSource.Range(“A” & lngSourceRow).CurrentRegion.Sort _
Key1:=wshSource.Range(“A” & (lngSourceRow – 1)), Order1:=xlDescending, Header:=xlYes
-
-
-
-
-
WSLJM
AskWoody LoungerJune 27, 2006 at 10:35 pm #1018281Ahh… perfect!! And to prove that I am learning from you… I added more code to the end of the macro by copying your columns code to copy some blank columns over columns A thru D so that it gets “blanked out” after the data has been move over to the target worksheet… this way the next person doesn’t have to remember to “clear” the contents. It actually worked! Yahoo!
I’m learn’in!!
Thanks Hans!!
Lana
Viewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Does windows update component store “self heal”? (Awaiting moderation)
by
Mike Cross
22 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
39 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
4 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
45 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
3 hours, 21 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
3 hours, 22 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
10 hours, 23 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
11 hours, 29 minutes ago -
0Patch, where to begin
by
cassel23
5 hours, 31 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 1 hour ago -
89 million Steam account details just got leaked,
by
Alex5723
12 hours, 53 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 9 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
11 hours, 46 minutes ago -
Installer program can’t read my registry
by
Peobody
6 hours, 44 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
22 hours, 26 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 5 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 20 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 5 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 7 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day, 3 hours ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 10 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 11 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 16 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 21 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 21 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
18 hours, 30 minutes ago -
May 2025 updates are out
by
Susan Bradley
57 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 3 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
3 days, 3 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.