Results 1 to 16 of 16

Thread: I could use a hand with this one.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Resolved I could use a hand with this one.

    Ok I have code here that will move the text that is highlighted in yellow in any cell in row C from worksheet A to the same postion in worksheet B.

    VB Code:
    1. Dim aColor As Integer
    2.     Dim aCell As Range
    3.     Dim aWorksheet As Worksheet
    4.     Dim bWorksheet As Worksheet
    5.     Set aWorksheet = Worksheets("DATA")
    6.     Set bWorksheet = Worksheets("NegativeFMs")
    7.    
    8.     r1 = 2
    9.     c1 = 1
    10.     a1 = 1
    11.     b1 = 1
    12.     ' get color of  field C
    13.     ' if it is yellow the copy to another workbook
    14.     Do While aWorksheet.Cells(r1, c1 + 2) <> ""
    15.         Set aCell = aWorksheet.Cells(r1, c1 + 2)
    16.         aColor = aCell.Interior.ColorIndex
    17.         If aColor = 6 Then
    18.             bWorksheet.Cells(a1, b1).Value = "'" & aWorksheet.Cells(r1, c1 + 2).Value
    19.             a1 = a1 + 1
    20.         End If
    21.         r1 = r1 + 1
    22.     Loop
    23. End Sub



    But what i want to do is instead of moving the text of the yellow cell in row C to worksheet b, I want to move the whole row of any text in any cell thats bold in worksheet a to worksheet b

    Example:
    Worksheet A
    A B C D E F G
    N N Y N N N N
    T T T S S T T

    Workshhet B
    A B C D E F G
    N N Y N N N N

    I hope this makes sense. This would really help me out ALOT, If not thanks for taking a look at it.
    Last edited by The_Rookie; Jan 6th, 2005 at 03:45 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    Welcome to the Forums.

    Once you have your row you can select the entire row, copy it, select the
    other sheet, and paste the row in. Change the indexes to your variables, etc.
    Just a demo of the mechanics involved.

    VB Code:
    1. Workbooks(1).Sheets("Sheet1").Rows(1).Select
    2.     Workbooks(1).Sheets("Sheet1").Rows(1).Copy
    3.     Workbooks(1).Sheets("Sheet2").Activate
    4.     Workbooks(1).Sheets("Sheet2").Paste Destination:=Workbooks(1).Sheets("Sheet2").Rows(1)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    WOW! thanks for the quick reply RobDog888!!!

    Thanks for the help but it looks like i would have to be specific with the row for it to copy the way you posted it there. And... what im trying to do is 1st locate the cell with bold text 2nd select the row that that cell is a part of then Paste it into the 2nd worksheet.

    im a suppernoob if you cant tell...the code that i listed came from someone else who was trying to help but hes not availible anymore. I kinda understand it but some of it...most of it I dont. Im just being honest here.
    but thanks for taking a look at it.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    No prob.
    I had some time so I integrated my code with yours. I think this is how it should go...

    VB Code:
    1. Dim aColor As Integer
    2.     Dim aCell As Range
    3.     Dim aWorksheet As Worksheet
    4.     Dim bWorksheet As Worksheet
    5.     Set aWorksheet = Worksheets("DATA")
    6.     Set bWorksheet = Worksheets("NegativeFMs")
    7.    
    8.     r1 = 2
    9.     c1 = 1
    10.     a1 = 1
    11.     b1 = 1
    12.     ' get color of  field C
    13.     ' if it is yellow the copy to another workbook
    14.     Do While aWorksheet.Cells(r1, c1 + 2) <> ""
    15.         Set aCell = aWorksheet.Cells(r1, c1 + 2)
    16.         aColor = aCell.Interior.ColorIndex
    17.         If aColor = 6 Then
    18.             bWorksheet.Cells(a1, b1).Value = "'" & aWorksheet.Cells(r1, c1 + 2).Value
    19.             aWorksheet.Activate 'Make sure we are on aWorksheet
    20.             aWorksheet.Rows(r1).Select
    21.             aWorksheet.Rows(r1).Copy
    22.             bWorksheet.Activate 'Switch to bWorksheet for the paste
    23.             bWorksheet.Paste Destination:=bWorksheet.Rows(a1)
    24.             aWorksheet.Activate 'Switch back
    25.             a1 = a1 + 1
    26.         End If
    27.         r1 = r1 + 1
    28.     Loop
    29. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    Hey Thanks man!!!
    im almost there now...I just changed it so that instead of anythig yellow
    VB Code:
    1. aColor = aCell.Interior.ColorIndex
    2.         If aColor = 6 Then
    ..that it moves anything in bold text
    VB Code:
    1. aColor = aCell.Font.Bold
    2.             If aColor = True Then

    Now how would i get it to recognize ANY cell with bold text on Worksheet A and copy that row to woeksheet B? Cause right now it will just copy the row of any bold text in any cell in Colum C

    im learning slowly but surley but im stuck right here.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    You would probably need to do a nested loop to loop accross the columns
    looking at the cell in column A, B, C, ...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    can you give an example of a nested loop (yes, I am that green)? Im gonna do some searching on the web to see if i can figure it out.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    Nested loop inside another loop example.
    VB Code:
    1. Dim x As Integer
    2. Dim y As Integer
    3.  
    4. For x = 1 To 10
    5.     For y = 1 To 20
    6.         Msgbox x & y
    7.     Next
    8. Next
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    Ok i think im even more lost today.....It will still only copy the bold text in Colum C.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    No, it is just an example of how to place a loop inside another loop.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Talking Re: I could use a hand with this one.

    If I may stop in here....

    Your code will only check one cell
    VB Code:
    1. Do While aWorksheet.Cells(r1, c1 + 2) <> ""
    2.         Set aCell = aWorksheet.Cells(r1, c1 + 2)
    3.         aColor = aCell.Interior.ColorIndex
    4.         If aColor = 6 Then
    You never change the Cell r1,c1+2, use Robdog888's suggestion for nested loops. First you musr decide which range you want to check (how many rows, how many columns)
    Knowing that you do something like
    VB Code:
    1. For i=1 To NumberofRowsToCheck
    2.     For j=1 to NumberOfColumnsToCheck
    3.         Set aCell = aWorksheet.Cells(i, j)
    4.         aColor = aCell.Font.Bold
    5.         If aColor = True Then
    6.              aWorksheet.Activate 'Make sure we are on aWorksheet
    7.              aWorksheet.Rows(i).Select
    8.              aWorksheet.Rows(i).Copy
    9.              bWorksheet.Activate 'Switch to bWorksheet for the paste
    10.              bWorksheet.Paste Destination:=bWorksheet.Rows(a1)
    11.              a1=a1+1
    12.             aWorksheet.Activate 'Switch back
    13.         End If
    14.     Next i
    15. Next j

    Hope that helps!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I could use a hand with this one.

    I welcome anyone that wants to help anytime.

    You can use this code snippet to determine the last column used so your
    inner loop will not miss any columns.

    VB Code:
    1. ActiveWorkbook.Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Column
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    Ill give it a try but when i use the
    VB Code:
    1. For i=1 To NumberofRowsToCheck
    2.     For j=1 to NumberOfColumnsToCheck
    3.         Set aCell = aWorksheet.Cells(i, j)

    Do i get rid of this all together?
    VB Code:
    1. Do While aWorksheet.Cells(r1, c1 + 2) <> ""
    2.         Set aCell = aWorksheet.Cells(r1, c1 + 2)
    And for the rows to check I just want to check the whole sheet so would i put in there 65536?


    Thanks for all your paitence help fellas....

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    Got it to work for the most part but im using
    VB Code:
    1. i As Long
    instad of integer
    and putting in 65536 for the rows.......
    is there any way to speed it up ?

    And Also if 2 cells in the same row have bold text it pastes' it to bworksheet
    twice....is there any way to avoid that from happening?
    heres the code so far
    VB Code:
    1. Dim aColor As Integer
    2. Dim aCell As Range
    3. Dim aWorksheet As Worksheet
    4. Dim bWorksheet As Worksheet
    5. Dim i As Long
    6. Dim j As Integer
    7. Set aWorksheet = Worksheets("Console")
    8. Set bWorksheet = Worksheets("Results")
    9.  
    10. r1 = 2
    11. c1 = 1
    12. a1 = 1
    13. b1 = 1
    14. ' get Text of FM field
    15. ' if it is BOLD the copy to another workbook
    16. For i = 2 To 65536
    17. For j = 1 To 13
    18. Set aCell = aWorksheet.Cells(i, j)
    19. aColor = aCell.Font.Bold
    20. If aColor = True Then
    21. aWorksheet.Activate 'Make sure we are on aWorksheet
    22. aWorksheet.Rows(i).Select
    23. aWorksheet.Rows(i).Copy
    24. bWorksheet.Activate 'Switch to bWorksheet for the paste
    25. bWorksheet.Paste Destination:=bWorksheet.Rows(a1)
    26. a1 = a1 + 1
    27. End If
    28. Next
    29. Next
    30. End Sub
    Thanks for all the help guys ive become a office hero thanks to you!!!

  15. #15
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I could use a hand with this one.

    Sorry about the double pasting, I missed that.
    you need to leave the column if you have fund one bold item.
    Just insert a linwe like
    VB Code:
    1. j=NumberOfColumnsToCheck
    after you have pasted

    And for the speeding up use RobDog888s snippet the check for the last rownumber used.
    it would look something like that:
    VB Code:
    1. Dim aColor As Integer
    2. Dim aCell As Range
    3. Dim aWorksheet As Worksheet
    4. Dim bWorksheet As Worksheet
    5. Dim i As Long
    6. Dim j As Integer
    7. Set aWorksheet = Worksheets("Console")
    8. Set bWorksheet = Worksheets("Results")
    9.  
    10. r1 = 2
    11. c1 = 1
    12. a1 = 1
    13. b1 = 1
    14. ' get Text of FM field
    15. ' if it is BOLD the copy to another workbook
    16. For i = 2 To aWorksheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    17. For j = 1 To 13
    18. Set aCell = aWorksheet.Cells(i, j)
    19. aColor = aCell.Font.Bold
    20. If aColor = True Then
    21. aWorksheet.Activate 'Make sure we are on aWorksheet
    22. aWorksheet.Rows(i).Select
    23. aWorksheet.Rows(i).Copy
    24. bWorksheet.Activate 'Switch to bWorksheet for the paste
    25. bWorksheet.Paste Destination:=bWorksheet.Rows(a1)
    26. a1 = a1 + 1
    27. j=13
    28. End If
    29. Next
    30. Next
    31. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: I could use a hand with this one.

    Thanks for the Reply opus it works well

    But Somethimes I have alot of data...and the scrren looks like its going to explode because i guess its checking back and forth for every cell then pasing it to bworkbook... Is there a way that we can do it where it shows the progress of the process on a status bar of some sort on aworksheet then when its complete...switch over to bworksheet for viewing the results?

    Again thanks for your time and your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width