Excel提取空间/格式



我正在EXCEL中构建一个摘录。我已经问了一些关于它的问题,但随着我的不断进步,我也继续面临着阻碍我让它看起来专业的障碍。以下是我的摘录。。。

我从SQL Server中选择日期。。

Select Location,Name, Q1, Comments
From tblInfo

然后我制作标题以提取

With xlSheetInfo
.Cells(5, 1).ColumnWidth = 50
.Cells(5, 1).Value = "School"
.Cells(5, 2).ColumnWidth = 25
.Cells(5, 2).Value = "CLIENT NAME"
.Cells(5, 3).Value = "Q1"
.Cells(5, 4).Value="Comments"
End with

然后我用数据填充它。。。使用记录集

While Not g_RS3.EOF
For i = xlCol To rCount
Cells(xlRow, xlCol).Value = g_RS3("School")
Cells(xlRow, xlCol).Font.Bold = True
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("LastName") & " ," & g_RS3("FirstName")
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("Q01")
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("Comments")
Cells(xlRow, xlCol).EntireColumn.AutoFit
xlrow = 1
g_rs3.movenext
Next i
Wend

我遇到的问题在评论部分。用户可以输入多行/多句评论。我看到的是,一些用户输入一条评论,做一些换行,然后输入另一条评论——对于同一条记录。因此,如果你看一下电子表格,它会显示出这些不必要的空格。在某些情况下,用户会打断几行,然后输入评论,在这种情况下,这看起来也不专业。我很想把所有的空格都去掉,这样如果有几行评论,下面就会有一行。这是我所说的。。。

https://i.stack.imgur.com/T0dmS.png

我玩过RTrim,LTrim,Trim,但似乎不起作用。另一件事是,我可能有任何值(NULL)。有什么想法吗????

将此函数放入模块中:

Public Function RemoveLf(strIn As String) As String
Dim strOut As String
strOut = Replace(strIn, vbLf, " ")
RemoveLf = strOut
End Function

然后在你的代码中,你可以替换这一行:

Cells(xlRow, xlCol).Value = g_RS3("Comments")

使用此行:

Cells(xlRow, xlCol).Value = RemoveLf(g_RS3("Comments"))

HTH-

最新更新