select最后一行,然后在VB中用excel格式化

我试图创build一个用户窗体,以逻辑方式将数据插入电子表格的最后一行,但是一旦将数据放入单元格中,数据就不会格式化。 我的第一个想法是简单地select最后一行,并在放入数据之前对其进行格式化。'.Rows.Autofit'属性适用于此代码,但其他设置(如左alignment的文本)是这样的我真的需要,不工作。 我错过了什么? (我用NDA原因replace了所有的用户forms的文本和variables,我知道这会产生重复)

Private Sub StuffUpload_Click() Dim ws As Worksheet ' Grabs the worksheet that the user is currently looking at, making this ' form work on all sheets Set ws = ActiveSheet ' Make sure all required fields have been entered If Stuff.Text = "" Then MsgBox "You must enter Stuff." Stuff.SetFocus Exit Sub End If ' Add a dash of formatting to the last row so the stuff we put into it will ' look nice ws.Range("B" & Rows.Count).End(xlUp).Offset(1).Select With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False .Rows.AutoFit End With ' Adds the Stuff info into Col B at the Last Blank Row ws.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = Me.Stuff.Value ' Add date and time stamp for the moment the comment was entered ws.Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Date + Time ' Adds the Stuff info into Col D at the last Blank Row ws.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Me.Stuff.Value Unload Me End Sub 

尝试用下面的代码replace你的代码:

 Private Sub StuffUpload_Click() Dim ws As Worksheet Dim LastRow As Long Dim RngAnchor As Range ' Grabs the worksheet that the user is currently looking at, ' making this form work on all sheets Set ws = ActiveSheet ' Make sure all required fields have been entered If Stuff.Text = "" Then MsgBox "You must enter Stuff." Stuff.SetFocus Exit Sub End If ' Add a dash of formatting to the last row so the stuff we put into it will ' look nice With ws LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row ' get the last row in column "B" ' set the anchor point of the range in column "B" Set RngAnchor = .Range("B" & LastRow + 1) ' Adds the Stuff info into Col B at the Last Blank Row RngAnchor.Value = Me.Stuff.Value ' Add date and time stamp for the moment the comment was entered RngAnchor.Offset(, 1).Value = Now ' Adds the Stuff info into Col D at the last Blank Row RngAnchor.Offset(, 2).Value = Me.Stuff.Value '<-- already added this on Column "B" ' now format last row, cells "B:D" With RngAnchor.Resize(1, 3) .HorizontalAlignment = xlLeft .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False .Rows.AutoFit End With End With Unload Me End Sub