Excel Macro copy to cell based on criteria
Solved/Closed
megamikeridge
Posts
4
Registration date
Wednesday October 5, 2011
Status
Member
Last seen
October 12, 2011
-
Oct 5, 2011 at 04:41 PM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Oct 15, 2011 at 07:21 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Oct 15, 2011 at 07:21 AM
Related:
- Macro to insert row in excel based on criteria
- Spell number in excel without macro - Guide
- Insert checkmark in word - Guide
- Insert gif in excel - Guide
- Insert draft watermark in word on all pages - Guide
- How to insert photo in word for resume - Guide
3 responses
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Oct 6, 2011 at 08:28 PM
Oct 6, 2011 at 08:28 PM
If the case is always that the filled row will occur before blank one , then you can use dictionary object. For that
Loop thru your first row till end
if both cells are not empty, combine the value of B and D as key and value of C as value . as you go thru rows, first check if that key is in the dictionary, if yes, you have the value else add to the dictionary
' to create dic object
Set dicMyDic = CreateObect("Scripting.Dictionary")
'this is how key can be
strKey = B1 & "|" & D1
'check if value of D is blank or not
' to add the value to dic
IF Not (dicMyDic.Exists(strKey )) then dicMyDic.Add Key:=strKey, Item:=val
'to get the value from dic
IF (dicMyDic.Exists(strKey )) then val = dicMyDic(strKey)
Hope that will help
Loop thru your first row till end
if both cells are not empty, combine the value of B and D as key and value of C as value . as you go thru rows, first check if that key is in the dictionary, if yes, you have the value else add to the dictionary
' to create dic object
Set dicMyDic = CreateObect("Scripting.Dictionary")
'this is how key can be
strKey = B1 & "|" & D1
'check if value of D is blank or not
' to add the value to dic
IF Not (dicMyDic.Exists(strKey )) then dicMyDic.Add Key:=strKey, Item:=val
'to get the value from dic
IF (dicMyDic.Exists(strKey )) then val = dicMyDic(strKey)
Hope that will help
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
552
Oct 6, 2011 at 10:24 AM
Oct 6, 2011 at 10:24 AM
Hi megamikeridge,
Sorry dude, having trouble understanding what you are trying to accomplish. Can you provide a small sample of you sheet before and after running the code (i.e. how it is now and how you want it to be).
You can also use a filesharing site like www.speedyshare.com to upload your file if that is easier.
Best regards,
Trowa
Sorry dude, having trouble understanding what you are trying to accomplish. Can you provide a small sample of you sheet before and after running the code (i.e. how it is now and how you want it to be).
You can also use a filesharing site like www.speedyshare.com to upload your file if that is easier.
Best regards,
Trowa
megamikeridge
Posts
4
Registration date
Wednesday October 5, 2011
Status
Member
Last seen
October 12, 2011
Oct 6, 2011 at 04:44 PM
Oct 6, 2011 at 04:44 PM
Thank you @TrowaD. Sorry for the confusion. Enclosed is a template.
http://www.4shared.com/file/KmZ4QWy-/Workbook1.html
In Sheet 1 is what the values look like
In Sheet 2 is what all the values should look like.
In short, in sheet1 there are missing values for various rows (col D mainly). I need to create a copy + paste macro that can search through the rows (columns B&D) that are complete AND search through the other rows that have Col B (with values) and Col D (null). When the macro finds a match, it can copy + paste the values from the complete row (column B & D) into the incomplete row (Column D).
There are multiple ways to create this, it seems as though I've picked the wrong way, can't figure it out today. Code is buggy. Any help is appreciated. None is ok too :( :( thanks
http://www.4shared.com/file/KmZ4QWy-/Workbook1.html
In Sheet 1 is what the values look like
In Sheet 2 is what all the values should look like.
In short, in sheet1 there are missing values for various rows (col D mainly). I need to create a copy + paste macro that can search through the rows (columns B&D) that are complete AND search through the other rows that have Col B (with values) and Col D (null). When the macro finds a match, it can copy + paste the values from the complete row (column B & D) into the incomplete row (Column D).
There are multiple ways to create this, it seems as though I've picked the wrong way, can't figure it out today. Code is buggy. Any help is appreciated. None is ok too :( :( thanks
Oct 10, 2011 at 09:42 AM
The thing I did notice and want to add to Rizvisa's post is that you need to activate it.
Open VB goto Tools > references and select Microsoft Scripting Runtime.
Best regards,
Trowa
Oct 12, 2011 at 02:59 PM
Oct 15, 2011 at 07:21 AM
Set dicMyDic = CreateObect("Scripting.Dictionary")
you do not need to include the reference if you make a call like above. How ever since you indicated that you are not familiar with dictionary object, adding the reference would hep you to see what else you can do with dictionary due to availability of intellisense, when you use the object However in order to use that feature, you would need to change the call as
dim dicMyDic as new Scripting.Dictionary