Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading cells in Excel 2010 and writing to another cell

Posted on 2013-11-04
7
Medium Priority
?
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

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

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
Norie earned 2000 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 35

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

618 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