Solved

Reading cells in Excel 2010 and writing to another cell

Posted on 2013-11-04
7
365 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
Comment Utility
Can you please upload a sample file?
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Will myimage only appear once if it appears?
0
 

Author Comment

by:jmohsin
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Made a change on line 13 and it worked!
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

772 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

11 Experts available now in Live!

Get 1:1 Help Now