Solved

VBA coding

Posted on 2013-05-24
8
245 Views
Last Modified: 2013-05-24
In Column H of my spreadsheet, some rows contain values and others do not. But, the cells that appear to be empty are not really. (I think that some previous part of the coding process results in these cells containing a null string rather than truly being empty. I am just guessing that it is a null string rather than some other problem that makes the cell appear to be empty.)

The null string wouldn't bother me, except that it prevents the following code from working as I want it to. This code is intended to select only those rows that have data in Column H. The code works fine if I manually delete the null strings from each "empty" cell.

So, the fix is to either add some new code that will empty the cells that contain the null strings. Or, an inferior but also acceptable solution would be to have the current code modified so that it does not select cells that contain null strings.

Here is the code. Thank you.

 Sub MoveNeedAddressesDataToToBeMailed()

' Sort Need Addresses so that all addresses are at the top
     Sheets("Need Addresses").Select
     Range("A:Z").Sort Key1:=Range("K2"), Order1:=xlAscending, Key2:=Range _
        ("E2"), Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
        :=xlSortNormal
'
'    ActiveWorkbook.Worksheets("Need Addresses").Sort.SortFields.Clear
'    ActiveWorkbook.Worksheets("Need Addresses").Sort.SortFields.Add Key:=Range( _
'        "K:K"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
'        xlSortNormal
'    With ActiveWorkbook.Worksheets("Need Addresses").Sort
'        .SetRange Range("A:Z")
'        .Header = xlYes
'        .MatchCase = False
'        .Orientation = xlTopToBottom
'        .SortMethod = xlPinYin
'        .Apply
'    End With

' Find rows in Need Addresses that contain addresses and copy the rows
    Range("K2:K" & Cells(Rows.Count, "K").End(xlUp).Row).Select
    Selection.EntireRow.Select
    Selection.Cut
'Select sheet To Be Mailed, then find first blank row and paste data there
    Sheets("To Be Mailed").Select
    Range("A1").End(xlUp).Offset(1, 0).Select
    Selection.Insert Shift:=xlDown
    Application.CutCopyMode = False
' Go back to sheet Need Addresses and delete data that was moved
    Sheets("Need Addresses").Select
    Selection.EntireRow.Delete
' Go back to sheet To Be Mailed
    Sheets("To Be Mailed").Select
'Make Mailed Column Yellow
    Columns("P:P").Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
    End With

Open in new window

0
Comment
Question by:carlosab
  • 4
  • 2
  • 2
8 Comments
 
LVL 24

Expert Comment

by:Steve
ID: 39195081
You likely do not have a "null string" but more likely a carriage return, line feed or other non-printing character.

It is quite simple to remove these once we know which one it is.
Could you post a simple sample workbook containing cells with the "null" cells as example.

Thanks.
0
 
LVL 24

Expert Comment

by:Steve
ID: 39195102
You could try something as simple as the following:

Sub clearNonPrinting()

For Each c In ActiveSheet.UsedRange.Cells

    If Len(Trim(c.Value)) = 0 Then c.Value = vbNullString
    If c.Value = Chr(10) Then c.Value = vbNullString
    If c.Value = Chr(13) Then c.Value = vbNullString
Next c

End Sub

Open in new window

0
 

Author Comment

by:carlosab
ID: 39195112
File attached. Note the formula I added to Column G to test whether Column H is blank. If you place the cursor in column H and delete the contents of the cell, the value in Column G changes.
Book1.xlsx
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39195144
Try adding these rows to clear those cells

    Range("H:H").AutoFilter
    Range("H:H").AutoFilter Field:=1, Criteria1:="="
    Range("H2:H" & Rows.Count).ClearContents
    Selection.AutoFilter
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 24

Expert Comment

by:Steve
ID: 39195153
the following should do it:

Sub RemoveBlanks()

For Each c In Range("H1:H31")
    If Len(c.Value) = 0 Then c.Value = Empty
Next c

End Sub

Open in new window

0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39195178
Correction to my code

    Range("H:H").AutoFilter
    Range("H:H").AutoFilter Field:=1, Criteria1:="="
    Range("H2:H" & Rows.Count).ClearContents
    Range("H:H").AutoFilter
0
 

Author Closing Comment

by:carlosab
ID: 39195513
Barman - thanks for the effort. I tried your code first but couldn't get it to work.
Thank you ssaquibh.
0
 
LVL 24

Expert Comment

by:Steve
ID: 39195636
That's cool Carlos, ssaquibh was a better answer :-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

760 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

18 Experts available now in Live!

Get 1:1 Help Now