?
Solved

Reading cells in Excel 2010 and writing to another cell

Posted on 2013-11-04
7
Medium Priority
?
405 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 34

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
Technology Partners: 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 34

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 34

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

771 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