VBA script for conditional copy, format & paste from workbook1 to workbook2

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
also, in new workbook , Q11111, Q22222,Q33333,Q4444 needs to be copied on B Coloumn of respective rows . [Pardon me, though i mentioned it in initial post, i missed this part in dropbox excel file. i will update.]
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
also, in new workbook , Q11111, Q22222,Q33333,Q4444 needs to be copied on B Coloumn of respective rows . [Pardon me, though i mentioned it in initial post, i missed this part in dropbox excel file. i will update.]
with bit of hands on, copying Q11111, Q22222,Q33333,Q4444 in B Coloumn of new workbook has been implemented & done.

however, ith column of new worksheet issue still exist.
Output on ith Column
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;

Thanks & Regards.
 
Last edited:

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
1 obersationi noticed

code always going to else part of if ..else, because "val.Interior.ColorIndex returning -4142" & we are checking whether it is equal to 43.
as it is not matching or equal, it is always going to else part of code.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,761
As I mentioned on Post #4 , you have to make sure that the colorindex of the shade of green you use in your cells matches the colorindex in the code. Run the following macro and change the range (in red) to a cell that is colored in green. This will display the colorindex of the shade of green you are using. Replace the "43" in the code with the displayed colorindex. That should fix the problem.
Code:
Sub test()
    MsgBox Range([COLOR="#FF0000"]"E2"[/COLOR]).Interior.ColorIndex
End Sub
 

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
As you said ,i used test(), got 14 as colorindex & used colorIndex as 14 in the macro you provided.
However, still the o/p remains same. it seems the value of 'val' is nothing on debug..i think it is the problem. further, it always going else part..pls check o/p.
Output on ith Column
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;

Meanwhile, i tried a different approach..it is producing result which is 80% of my expectations. few small issues still exist. it needs to be fixed. i shared macro below.

O/P:--
A1111::0;;
B1111::1;;
C1111::0;;
D1111::0;;
A2222::0;;
B2222::0;;
C2222::1;;
D2222::0;;
A3333::0;;
B3333::0;;
C3333::0;;
D3333::1;;
A4444::1;;
B4444::0;;
C4444::0;;
D4444::0;;

<tbody>
</tbody>

Macro:
Sub writetoevv_Ultimate_Test()
Application.ScreenUpdating = False
Dim LastRow As Long, val As Range, scrWS As Worksheet, desWB As Workbook, x As Long: x = 2
Set srcws = ThisWorkbook.Sheets("Sheet1")

Dim rng As Range: Set rng = Application.Range("B1:E4")
Dim cel As Range
For Each cel In rng.Cells
With cel
For Each val In srcws.Range("B" & rng.Row & ":E" & rng.Row)
If cel.Interior.ColorIndex = 14 Then
Cells(x, 9) = cel & "::1;;"
Else
Cells(x, 9) = cel & "::0;;"
End If
Next val
x = x + 1
End With
Next cel

Application.ScreenUpdating = True


End Sub
 
Last edited:

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,761
Click [URL="https://app.box.com/s/5qh1fz8qz0kpuju59ouleh1vulthg9d7"]here [/URL]to download your file. It seems to be working properly for me.

Code:
Sub writetoevv()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range, val As Range, scrWS As Worksheet, desWB As Workbook, x As Long: x = 2
    Set srcws = ThisWorkbook.Sheets("Sheet1")
    LastRow = srcws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set desWB = Workbooks.Add
    For Each rng In srcws.Range("A1:A" & LastRow)
        Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = rng
        For Each val In srcws.Range("B" & rng.Row & ":E" & rng.Row)
            If val.Interior.ColorIndex = 14 Then
                Cells(x, 9) = Cells(x, 9) & val & "::1;;"
            Else
                Cells(x, 9) = Cells(x, 9) & val & "::0;;"
            End If
        Next val
        x = x + 1
    Next rng
    Application.ScreenUpdating = True
End Sub
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,761
This is what I got.
<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:251px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >Q11111</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td >A1111::0;;B1111::1;;C1111::0;;D1111::0;;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >Q22222</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td >A2222::0;;B2222::0;;C2222::1;;D2222::0;;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >Q33333</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td >A3333::0;;B3333::0;;C3333::0;;D3333::1;;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td >Q4444</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td >A4444::1;;B4444::0;;C4444::0;;D4444::0;;</td></tr></table> <br />Excel Tabellen im Web darstellen - Excel Jeanie Html 4
 

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
yes, that is correct format..appreciates..

donot know why im getting w/o values..i used ur vba script as it is except commenting following line..' LastRow = srcws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row ...instead i used a static value of 5/10..but it didnt worked for me

could you share ur macro enabled excel sheet with me..pls
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,761
Click the link in Post #16 for the file. You shouldn't have to change any line in the macro.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,090,078
Messages
5,412,245
Members
403,423
Latest member
fori_gump

This Week's Hot Topics

Top