Solved

Cell tha that are empty

Posted on 2011-09-09
12
399 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
  • 6
  • 6
12 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
Comment Utility
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
Comment Utility
Your original was only testing on colun a ... How many cells do you want to test on each iteration?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
for the column
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much.... Nice ...!! You just saved me tons of work
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

13 Experts available now in Live!

Get 1:1 Help Now