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>
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,198
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
9,198
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
9,198
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
9,198
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:

Watch MrExcel Video

Forum statistics

Threads
1,102,778
Messages
5,488,828
Members
407,658
Latest member
Arias610

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top