?
Solved

Cell tha that are empty

Posted on 2011-09-09
12
Medium Priority
?
408 Views
Last Modified: 2012-05-12
What I would like to do is if a cell in excel is empty I would like to code to skip and move to the next one.. here is my code so far:
iRow = 2
Do While  oSheet.Cells(iRow, 1).Value <> ""
    colA = oSheet.Cells(iRow, 1).Value
    colB = oSheet.Cells(iRow, 2).Value
    colC = oSheet.Cells(iRow, 3).Value

But when I add this to it Do While  Not IsEmpty (oSheet.Cells(iRow, 1).Value) <> "" ... It stops working. I want it to read the cell and if it is empty just skip it and move on to the next one.
I am reading the rows like this: and the output is in XML format

If UCase(Left(colA,2))= "XA" Then
        oOut.WriteLine("  <XA_data>")
        oOut.WriteLine("  <EIACODXA>" & colB & "</EIACODXA>")

Thank you
0
Comment
Question by:drezner7
[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
  • 6
  • 6
12 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36515412
As long as your loop has a terminator to identify the end of range then change the while to an if ... but it is important to identify and exit strategy of course.

iRow = 2
If  oSheet.Cells(iRow, 1).Value <> ""
    colA = oSheet.Cells(iRow, 1).Value
    colB = oSheet.Cells(iRow, 2).Value
    colC = oSheet.Cells(iRow, 3).Value

Chris
0
 

Author Comment

by:drezner7
ID: 36520375
Would it be this with adding the Then at the end of the If ?

iRow = 2
If  oSheet.Cells(iRow, 1).Value <> "" Then

But when I add this I get an error of "Loop without do" ..  I have never seen this error before please help

 
0
 

Author Comment

by:drezner7
ID: 36520449
Here is the code... I am still getting the error 'Loop without do'

iRow = 2
If oSheet.Cells(iRow, 1).Value <> "" Then
    colA = oSheet.Cells(iRow, 1).Value
    colB = oSheet.Cells(iRow, 2).Value
    colC = oSheet.Cells(iRow, 3).Value

If UCase(Left(colA,2))= "XA" Then
        oOut.WriteLine("  <XA_data>")
        oOut.WriteLine("  <EIACODXA>" & colB & "</EIACODXA>")

iRow = iRow+1
   
Loop

oExcel.quit
WScript.Echo "Conversion Completed Successfully"
oOut.Close  

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 59

Expert Comment

by:Chris Bottomley
ID: 36520920
No my point was removal of the do loop as it stands and then looping as required so for example

Chris


firstrow = 2
lastrow = osheet.Cells(osheet.Rows.Count, 1).End(xlUp).Row

For iRow = firstrow To lastrow
    If osheet.Cells(iRow, 1).Value <> "" Then
        colA = osheet.Cells(iRow, 1).Value
        colB = osheet.Cells(iRow, 2).Value
        colC = osheet.Cells(iRow, 3).Value
    End If
Next

Open in new window

0
 

Author Comment

by:drezner7
ID: 36522257
My apologies, but I am still confused,... I am still learning how to code in vbscript. I do not understand loop as required. In my mind I have to loop every Row and column, but skip the 'The oOut.Writeline if the cell is empty.

In this section of the code colD was empty, but it still prints it out the tags in the XML.

If UCase(Left(colA,2))= "XB" Then
        oOut.WriteLine("  <XB_data>")
        oOut.WriteLine("  <EIACODXA>" & colB & "</EIACODXA>")
        oOut.WriteLine("  <LSACONXB>" & colC & "</LSACONXB>")
        oOut.WriteLine("  <ALTLCNXB>" & colD & "</ALTLCNXB>")
        oOut.WriteLine("  <LCNTYPXB>" & colE & "</LCNTYPXB>")
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36522276
Your original was only testing on colun a ... How many cells do you want to test on each iteration?
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36522287
Ie I assume

If UCase(Left(colA,2))= "XB" Then

Is meant to test column d and therefore would assume colA is defined as 1 and that 4 is needed for the column I'd

If UCase(Left(4,2))= "XB" Then

Chris
0
 

Author Comment

by:drezner7
ID: 36522544
Column A has multiple values for example:
XB
XB
CA
CA
HH
HH
HB
HB
XA
etc...

Once the code reads col A based on what ever it equals it would grab the values to rest of the columns on the right. I think it would be easier If I just show u the whole code... and sample data.


 code.txt
sample.xlsx
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36523365
So any column can be blank and if blank you want to skip the write line for that specific column ... or for the whole row?

Chris
0
 

Author Comment

by:drezner7
ID: 36524183
for the column
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 36524262
Then look to your code lines where:

If UCase(Left(colA,2))= "XB" Then
        oOut.WriteLine("  <XB_data>")
        oOut.WriteLine("  <EIACODXA>" & colB & "</EIACODXA>")
        oOut.WriteLine("  <LSACONXB>" & colC & "</LSACONXB>")
        oOut.WriteLine("  <ALTLCNXB>" & colD & "</ALTLCNXB>")
        oOut.WriteLine("  <LCNTYPXB>" & colE & "</LCNTYPXB>")

can be reproduced as

If UCase(Left(colA,2))= "XB" Then
        oOut.WriteLine("  <XB_data>")
        if colB <> "" then oOut.WriteLine("  <EIACODXA>" & colB & "</EIACODXA>")
        if colC <> "" then oOut.WriteLine("  <LSACONXB>" & colC & "</LSACONXB>")
        if colD <> "" then oOut.WriteLine("  <ALTLCNXB>" & colD & "</ALTLCNXB>")
        if colE <> "" then oOut.WriteLine("  <LCNTYPXB>" & colE & "</LCNTYPXB>")

And simply repeat for every writeline.
0
 

Author Closing Comment

by:drezner7
ID: 36524362
Thank you very much.... Nice ...!! You just saved me tons of work
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 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