Don't copy blank cells or delete blank lines?

PsycholoJackal

New Member
Joined
Dec 10, 2019
Messages
3
Hi all,

For my project, I want to be able to have excel in VBA deliver usernames to 2 existing notepads that's in a folder. I made it so that it takes up to 10 usernames. My issue here is that sometimes I only need to deliver 1 username but in the text file it also delivers the empty cells which becomes the empty lines in the notepad. Any way to make it to not copy the blank cells or have it delete the blank lines? Code:

Option Explicit

Public Sub XLRangeToNotepad()

Dim iPtr As Integer
Dim sFileName As String
Dim intFH As Integer
Dim oCell As Range

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST LOGOFF\Iron\logoff-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
Print #intFH, oCell.Value
Next oCell

Close intFH

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST CLEAN\Iron\clear-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
Print #intFH, oCell.Value
Next oCell

Close intFH
MsgBox "Done!"

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I have added an if to your Print lines:
VBA Code:
Option Explicit

Public Sub XLRangeToNotepad()

Dim iPtr As Integer
Dim sFileName As String
Dim intFH As Integer
Dim oCell As Range

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST LOGOFF\Iron\logoff-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
If Len(oCell.Value) > 0 Then Print #intFH, oCell.Value
Next oCell

Close intFH

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST CLEAN\Iron\clear-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
If Len(oCell.Value) > 0 Then Print #intFH, oCell.Value
Next oCell

Close intFH
MsgBox "Done!"

End Sub
 
Upvote 0
I have added an if to your Print lines:
VBA Code:
Option Explicit

Public Sub XLRangeToNotepad()

Dim iPtr As Integer
Dim sFileName As String
Dim intFH As Integer
Dim oCell As Range

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST LOGOFF\Iron\logoff-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
If Len(oCell.Value) > 0 Then Print #intFH, oCell.Value
Next oCell

Close intFH

sFileName = "C:\Users\" & Environ("UserName") & "\Desktop\TEST CLEAN\Iron\clear-users.txt"

Close
intFH = FreeFile()
Open sFileName For Append As intFH

For Each oCell In Range("B2:B11")
If Len(oCell.Value) > 0 Then Print #intFH, oCell.Value
Next oCell

Close intFH
MsgBox "Done!"

End Sub
Hi John. I just tried it, it duplicated the usernames and also kept the bank lines in the notepad.
1576071518074.png
 

Attachments

  • 1576071411284.png
    1576071411284.png
    11.1 KB · Views: 9
  • 1576071457929.png
    1576071457929.png
    15.1 KB · Views: 11
Upvote 0
Oh dear - I'm not sure why that should happen (maybe the 'blank' cells have spaces in them?? - even so the code can't duplicate the rows as it steps through!), but try replacing the 2 if statements with this one:
If Len(oCell.Text) > 0 Then Print #intFH, oCell.Value
If there are spaces or other non-printable characters in the cells then we need to think again.
 
Upvote 0
Oh dear - I'm not sure why that should happen (maybe the 'blank' cells have spaces in them?? - even so the code can't duplicate the rows as it steps through!), but try replacing the 2 if statements with this one:
If Len(oCell.Text) > 0 Then Print #intFH, oCell.Value
If there are spaces or other non-printable characters in the cells then we need to think again.
It works perfectly! Thank you so much for your help =)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top