Solved

Reading cells in Excel 2010 and writing to another cell

Posted on 2013-11-04
7
379 Views
Last Modified: 2013-11-05
I have a very large Excel 2010 file (input.xls) that contains only ONE worksheet which has contains semi-structured data. I want to read data from this worksheet and write to a NEW Excel 2010 file (output.xls).

I want to traverse the worksheet in input.xls, and on EACH row, if a cell contains the value "MyImage", I want to read the value from the cell to its RIGHT (say this is "value001") and write it to the NEW file.

Thus the output.xls file will look like this:
value001
value002
value002
value003
.
.
.
0
Comment
Question by:jmohsin
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39622893
Can you please upload a sample file?
0
 
LVL 33

Expert Comment

by:Norie
ID: 39622960
Will myimage only appear once if it appears?
0
 

Author Comment

by:jmohsin
ID: 39623027
MyImage will appear ONLY ONCE per row. But it may appear on multiple, scattered rows. For instance, "MyImage" may appear ONCE on rows 12, 35, 41, 57.... Total number of rows EXCEEDS 2000.
In this example, I want to read the VALUE from the cell just to the RIGHT of the cell containing "MyImage" on rows 12, 35, 41, 57.....  I then want to write this VALUE only to a new file.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 39623053
Perhaps something like this.
Option Explicit

Sub MoveStuff()
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim rngSrc As Range
Dim wbDst As Workbook
Dim wsDst As Worksheet
Dim rngDst As Range
Dim rngRow As Range
Dim Res As Variant

    Set wbSrc = Worksheets("Input.xls")
    Set wsSrc = wbSrc.Worksheets(1)
    Set rngSrc = wsSrc.UsedRange

    Set wbDst = Workbooks.Add(xlWBATWorksheet)
    Set wsDst = wbDst.Worksheets(1)
    Set rngDst = wsDst.Range("A1")

    For Each rngRow In rngSrc.Rows
    
        Res = Application.Match("MyImage", rngRow, 0)
        
        If Not IsError(Res) Then
            rngRow.Cells(Res + 1).Copy rngDst
            Set rngDst = rngDst.Offset(1)
        End If
        
    Next rngRow

End Sub

Open in new window

0
 

Author Comment

by:jmohsin
ID: 39623354
Where should this VBA snippet sit? In output.xls? I assume input.xls and output.xls should be in the same directory?

I added this code to and BLANK file named output.xlsm that I created in the SAME directory as input.xls.

I then ran it, and got an out of range error on line 13, which is:
Set wbSrc = Worksheets("Input.xls")
0
 

Author Closing Comment

by:jmohsin
ID: 39623505
Made a change on line 13 and it worked!
0
 
LVL 33

Expert Comment

by:Norie
ID: 39623737
Glad you got it sorted.

The code actually creates a new workbook for the results since you didn't specify a specific workbook for them.

It could really go in the standard module of any workbook but would only work if there was a workbook called 'Input.xls' was open.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now