Solved

VBA coding

Posted on 2013-05-24
8
255 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

920 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

16 Experts available now in Live!

Get 1:1 Help Now