Solved

VBA coding

Posted on 2013-05-24
8
278 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

726 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