对于我的项目,我希望能够在VBA中表现出色,可以将用户名传送到文件夹中的2个现有记事本中。我做到了,因此最多需要10个用户名。我的问题是,有时我只需要提供1个用户名,但是在文本文件中,它还提供了空单元格,这些空单元格成为记事本中的空行。有什么方法可以使其不复制空白单元格或删除空白行?代码:
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