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

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
In workBook1, i have five columns (A,B,C,D,E) with data. Here, each row is a separate record & is going have unique data.
sample data is given below.

A B C D E
1 Q1 A1 A2 A3 A4
2 Q2 B1 B2 B3 B4

In workBook1, when i execute a VBA script , the following needs to done sequentially.
1. First, create a new workbook (for example workbook2). and, steps 2 to 6 should be repeated for all rows of workbook 1.
2. value Q1 of workbook1 should be copied to B2 in workbook2
3. if value A1 cell is in green color, then value A1::1;; should be add/appended to I2 column in workbook2. Else, append A1::0;; to I2 column of workbook2.
4. if value A2 cell is in green color, then value A2::1;; should be appended to I2 column of workbook2. Else, append A2::0;; to I2 column of workbook2.
5 if value A3 cell is in green color, then value A3::1;; should be appended to I2 column of workbook2. Else, append A3::0;; to I2 column of workbook2.
6. if value A4 cell is in green color, then value A4::1;; should be appended to I2 column of workbook2. Else, append A4::0;; to I2 column of workbook2.
7. Save workbook2 & close workbook2.

Example: for your reference, I2 Column Workbook2 should be like below.
chandu::1;;Shekar::0;;Raju::0;;Gopal::0;;
Venkat::0;;Gopi::1;;Stephen::0;;minda::0;;

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,444
Is the whole cell colored green or only the font colored green? How is the green color applied to the cells? Is it done manually or by conditional formatting?
 
Last edited:

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
Is the whole cell colored green or only the font colored green? How is the green color applied to the cells? Is it done manually or by conditional formatting?
appreciates for your quick response. manually select a cell of a record & by using fill color feature.

Actually, either way by fill color or font color is fine to me.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,444
Make sure you manually color the cells green. It's important that you select the proper shade of green whose colorindex=43. Try:
Code:
Sub writetoevv()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range, val As Range, scrWS As Worksheet, desWB As Workbook
    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 = 43 Then
                Cells(Rows.Count, val.Column + 1).End(xlUp).Offset(1, 0) = Cells(Rows.Count, val.Column + 1).End(xlUp).Offset(1, 0) & val & "::1;;"
            Else
                Cells(Rows.Count, val.Column + 1).End(xlUp).Offset(1, 0) = Cells(Rows.Count, val.Column + 1).End(xlUp).Offset(1, 0) & val & "::0;;"
            End If
        Next val
    Next rng
    Application.ScreenUpdating = True
End Sub
 

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
hi

when i tried, i got "runtime error 91" on this line
LastRow = srcws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

object variable or with block variable not set..

would it be feasible to change it to any color of "fill color".
 
Last edited:

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
i myself fixed runtime error 91 by commenting "LastRow" & took static value of 10 to test the macro.

macro is working, how ever not matching with expected result.
macro just pasting ::0;; in C column to F column from 2nd row(record) to 6th row(record).

expected result is 'i2' cell of Workbook2 should contain the value "xddd::1;;ddd::0;;dddd::0;;adal::0;;"
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,444
I think that it would be easier to help and test possible solutions if I could work with your actual file which includes any macros you are currently using. Perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do using a few examples from your data and referring to specific cells, rows, columns and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,444
Try:
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 = 43 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
 

writetoevv

Board Regular
Joined
Mar 9, 2012
Messages
57
Hi mumps

i got this output on ith column of new book. it seems it is always going to 'else' part of code & not copying content of cells.
also, Q11111, Q22222,Q33333,Q4444 needs to be copied on B Coloumn in new workbook.

Output on ith Column
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;
::0;;::0;;::0;;::0;;

<colgroup><col style="width:48pt" width="64"> </colgroup><tbody>
</tbody>
 
Last edited:

Forum statistics

Threads
1,078,486
Messages
5,340,618
Members
399,387
Latest member
amrita34

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top