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
Solved

Reading cells in Excel 2010 and writing to another cell

Posted on 2013-11-04
7
394 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

829 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