[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

VBA coding

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
carlosab
Asked:
carlosab
  • 4
  • 2
  • 2
1 Solution
 
SteveCommented:
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
 
SteveCommented:
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
 
carlosabAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Saqib Husain, SyedEngineerCommented:
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
 
SteveCommented:
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
 
Saqib Husain, SyedEngineerCommented:
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
 
carlosabAuthor Commented:
Barman - thanks for the effort. I tried your code first but couldn't get it to work.
Thank you ssaquibh.
0
 
SteveCommented:
That's cool Carlos, ssaquibh was a better answer :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now