Macro to save the world, combine cell contents based on other cell value


"Excel VBA Help"

 


Play Friv Now - Play Friv Games
Go Back   The Code Cage Forums > Microsoft Topics > Microsoft Excel Forum
Our Info Pages:-
The Code Cage - Microsoft Office help

Notices

Microsoft Excel Forum Members Post questions in this forum if they are related to using Microsoft Excel, this includes Worksheet Functions, Excel VBA Programming, Simple Macro Code & Charting, Excel formula etc.

 
 
Thread Tools Rate Thread Display Modes
  #1  
Old 11th February 2014, 20:07
jeffsdan's Avatar
jeffsdan jeffsdan is offline
Junior Member
MS Office Version: Excel 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2014
Location: Boston, USA
Age: 35
Posts: 6
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) jeffsdan is on a distinguished road
USA
Default Macro to save the world, combine cell contents based on other cell value
     Excel: VBA   


------ Register to get rid of these "In Post" ads! ------

Category: Excel: VBA   

I am having an impossible time finding a solution for this. I need to combine cell contents in an unusual way (see below), based on if there is an "x" in column A. See below for the exact representation of what I need to do in a worksheet with 200,000 total cells of content. Is there any hope? Anyone who can help will be my personal hero for life, and you may save a life (mine!)!


Current data format:

col1col2col3col4 
A B C D E
x a l g
b m h Title A
c n i
x d o j
e p k Title B
f q l Title C
g r m
The above table can be copied and dropped directly in to Excel


Needed format:

col1col2col3col4 
A B C D E
x a b c l m n g h i Title A
x d e f g o p q r j k l m Title B/Title C
The above table can be copied and dropped directly in to Excel


Last edited by Simon Lloyd; 18th February 2014 at 20:46. Reason: Tables updated to reflect actual worksheet data


The Code Cage Advertisment
Advertisement
  #2  
Old 11th February 2014, 20:27
_Mordred_'s Avatar
_Mordred_ _Mordred_ is offline
The Code Cage Staff
MS Office Version: MS Office 2003, 2007, 2010, & 2013
MS Office Skill Level: Can use VBA
 
Join Date: Jul 2011
Location: Winnipeg, Canada
Age: 43
Posts: 1,235
Posts Per Day: 0.53
My Top Tip Count: 2
Thanks to others: 207
Thanked 49 Times in 49 Posts
Reputation: 704 (26) _Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold
2 Top Tips
Canada
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


Hey Jeffsdan, it's Stuugie from Daniweb, hopefully you get the answers you need now.
__________________
_Mordred_

Want to say thanks? then hit this How to say thanks! button in any post you want to thank
If you find one of my posts helpful then hit this one Helpful Answer in the post that helped most!



Keep on coding in the free world.



  #3  
Old 17th February 2014, 19:10
jeffsdan's Avatar
jeffsdan jeffsdan is offline
Junior Member
MS Office Version: Excel 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2014
Location: Boston, USA
Age: 35
Posts: 6
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) jeffsdan is on a distinguished road
USA
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


I got the code below to work to do what I am looking to do, except the macro freezes on rows where two consecutive rows have an "x" in column A. Thoughts?
VBA CODE:
Sub test() 
    Dim i As Long, j As Long, startRow As Long, endRow As Long, startCol As Long, endCol As Long, pasteRow As Long 
    Dim sht1 As String, sht2 As String 
     
    sht1 = ActiveSheet.Name 
     
    startRow = 2 
    startCol = 1 'Column with "x"
     
    endRow = Cells(Rows.Count, "B").End(xlUp).Row 
    endCol = Cells(1, Columns.Count).End(xlToLeft).Column 
     
    Sheets.Add after:=Sheets(1) 
     
    sht2 = ActiveSheet.Name 
     
    pasteRow = 1 
     
    With Sheets(sht1) 
        For i = startRow To endRow 
            If .Cells(i, startCol) = "x" Then 
                If Len(.Cells(i + 1, startCol)) = 0 Then 'continue until next instance of "x"
                    Sheets(sht2).Cells(pasteRow, 2) = .Cells(i, 2) 
                     
                    i = i + 1 
                     
                    Do While Len(.Cells(i, startCol)) = 0 
                        Sheets(sht2).Cells(pasteRow, 2) = Sheets(sht2).Cells(pasteRow, 2) & " " & .Cells(i, 2) 
                         
                        i = i + 1 
                         
                        If i > endRow Then Exit Sub 
                    Loop 
                     
                    pasteRow = pasteRow + 1 
                End If 
                i = i - 1 
            End If 
        Next i 
    End With 
End Sub 



The Code Cage Advertisment
Advertisement
  #4  
Old 18th February 2014, 16:06
LisaGreen's Avatar
LisaGreen LisaGreen is offline
Solution Centre Professional
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Can use VBA
 
Join Date: Jun 2011
Location: Houten, Nederlands
Age: 66
Posts: 568
Posts Per Day: 0.24
My Top Tip Count: 1
Thanks to others: 9
Thanked 18 Times in 16 Posts
Reputation: 270 (14) LisaGreen is a jewel in the roughLisaGreen is a jewel in the roughLisaGreen is a jewel in the rough
1 Top Tips
Great_Britain
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


Hmmmm.....

I've tried the code with the example and all I get is a blank sheet.

Change startcol to 2 and see what happens.

Also, the code as written will not pick up the A B C D E row. Is that the intention?

HTH
Lisa



  #5  
Old 18th February 2014, 18:59
jeffsdan's Avatar
jeffsdan jeffsdan is offline
Junior Member
MS Office Version: Excel 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2014
Location: Boston, USA
Age: 35
Posts: 6
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) jeffsdan is on a distinguished road
USA
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


In my sheet, the "x"'s are in column A so it works great as long as there aren't "x"'s in consecutive rows, in which case the macro freezes. I assume this is because the macro is looking to combine the cells of the other columns below rows with "x"s, but the macro doesn't know what to do if the next row also has an "x". Thoughts?



  #6  
Old 18th February 2014, 19:13
LisaGreen's Avatar
LisaGreen LisaGreen is offline
Solution Centre Professional
MS Office Version: MS Office 97, 2003, 2007,2010
MS Office Skill Level: Can use VBA
 
Join Date: Jun 2011
Location: Houten, Nederlands
Age: 66
Posts: 568
Posts Per Day: 0.24
My Top Tip Count: 1
Thanks to others: 9
Thanked 18 Times in 16 Posts
Reputation: 270 (14) LisaGreen is a jewel in the roughLisaGreen is a jewel in the roughLisaGreen is a jewel in the rough
1 Top Tips
Great_Britain
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


You mean the example you posted isn't the example you are working with!?

With respect... can we get on the same er... sheet.. here.

Lisa



The Code Cage Advertisment
Advertisement
  #7  
Old 18th February 2014, 19:35
jeffsdan's Avatar
jeffsdan jeffsdan is offline
Junior Member
MS Office Version: Excel 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2014
Location: Boston, USA
Age: 35
Posts: 6
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) jeffsdan is on a distinguished road
USA
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


It is the same, just note that the first column isn't the column that says "row1, row2, etc", but rather its the first column with data ("X"'s).



  #8  
Old 18th February 2014, 20:47
Simon Lloyd's Avatar
Simon Lloyd Simon Lloyd is offline
Administrator
MS Office Version: MS Office 97 - 2013
MS Office Skill Level: Can use most of MS Office
 
Join Date: Aug 2008
Location: Manchester, England
Age: 53
Posts: 11,884
Posts Per Day: 3.51
My Top Tip Count: 11
Thanks to others: 49
Thanked 441 Times in 425 Posts
Reputation: 6660 (194) Simon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond reputeSimon Lloyd has a reputation beyond repute
11 Top Tips
Wales
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


I've edited the tables to remove Row1...etc
__________________
Regards,
Simon Lloyd
Microsoft Office Help

Want to say thanks? then hit this How to say thanks! button in any post you want to thank
If you find one of my posts helpful then hit this one Helpful Answer in the post that helped most!





  #9  
Old 19th February 2014, 17:33
jeffsdan's Avatar
jeffsdan jeffsdan is offline
Junior Member
MS Office Version: Excel 2010
MS Office Skill Level: No VBA Skills
 
Join Date: Feb 2014
Location: Boston, USA
Age: 35
Posts: 6
Posts Per Day: 0.00
My Top Tip Count: 0
Thanks to others: 0
Thanked 0 Times in 0 Posts
Reputation: 10 (4) jeffsdan is on a distinguished road
USA
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


I am still at a loss of how to get this to work. The Macro above works, except it freezes when there is an "x" on two consecutive rows. Any thoughts on what can be done?



The Code Cage Advertisment
Advertisement
  #10  
Old 19th February 2014, 18:44
_Mordred_'s Avatar
_Mordred_ _Mordred_ is offline
The Code Cage Staff
MS Office Version: MS Office 2003, 2007, 2010, & 2013
MS Office Skill Level: Can use VBA
 
Join Date: Jul 2011
Location: Winnipeg, Canada
Age: 43
Posts: 1,235
Posts Per Day: 0.53
My Top Tip Count: 2
Thanks to others: 207
Thanked 49 Times in 49 Posts
Reputation: 704 (26) _Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold_Mordred_ is a splendid one to behold
2 Top Tips
Canada
Default Re: Macro to save the world, combine cell contents based on other cell value

------ Register to get rid of these "In Post" ads! ------


What happens if you replace startCol with a 1?
__________________
_Mordred_

Want to say thanks? then hit this How to say thanks! button in any post you want to thank
If you find one of my posts helpful then hit this one Helpful Answer in the post that helped most!



Keep on coding in the free world.



The Code Cage Advertisment
Advertisement
 
The Code Cage at Facebookhe Code Cage at Twitter


Tags
based, cell, combine, contents, macro, save, world

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

The Code Cage Affilliates



http://www.thecodecage.com/forumz tested by McAfee Security http://www.thecodecage.com/forumz tested by McAfee Security

All times are GMT +1. The time now is 07:42.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Template-Modifications by TMS
No part of this board, functionality or creatives may be copied or reproduced either in part or full without the express permission of The Code Cage Team.
Copyright ©2008 - , Simon lloyd.
We are not associated with nor employed by Microsoft in any way, we simply provide resources!
All MS office icons are registered trademarks of the application they represent and are used with permission from Microsoft
UA-8135493-1
Cultural Forum | Study at Malaysian University
X vBulletin 3.8.7 Debug Information
  • Page Generation 0.22545 seconds
  • Memory Usage 6,649KB
  • Queries Executed 16 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footerEnd
  • (1)ad_footerStart
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_headerEnd
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_leftcolumn_code
  • (1)ad_navbar_below
  • (5)ad_postbit_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (10)helpful_answers
  • (1)helpfulanswers_javascript
  • (1)navbar
  • (3)navbar_link
  • (3)navbar_noticebit
  • (52)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_ctoptip
  • (10)postbit_onlinestatus
  • (36)postbit_reputation
  • (10)postbit_wrapper
  • (1)showthread_changepostsowner_link
  • (1)showthread_extra_fields
  • (1)showthread_quickreply_mrgdp
  • (1)showthread_reason_popup
  • (1)spacer_close
  • (1)spacer_open
  • (1)su_form
  • (7)tagbit
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_datastore.php
  • ./includes/class_hook.php
  • ./awcoding/plugins/awc.php
  • ./awcoding/plugins/ds.php
  • ./awcoding/plugins/ss.php
  • ./includes/functions_notice.php
  • ./mobiquo/smartbanner.php
  • ./mobiquo/include/classTTConnection.php
  • ./mobiquo/smartbanner/head.inc.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/vbatags.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php
  • ./includes/functions_misc.php 

Hooks Called:
  • init_startup
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_check_criteria
  • notices_noticebit
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • helpfulanswers_showthreadorpost
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • helpfulanswers_show_normal
  • reputation_power
  • reputation_image
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • error_fetch
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete