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

VBA coding

Posted on 2013-05-24
8
270 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

792 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