2017-11-01 11 views
1

スプレッドシートの最後の行に論理的にデータを挿入するユーザーフォームを作成しようとしていますが、データをドロップするとデータはフォーマットされません細胞。私の最初の考えは、データをドロップする前に、最後の行を選択してフォーマットすることだけです。この '.Autofit'プロパティはこのコードで機能しますが、他の設定、たとえば左揃えのテキストなど私は本当に必要、働かないでください。私は何が欠けていますか? (。私は、これは複製を作成する知っているNDA上の理由から、「スタッフ」で、すべてのユーザーフォームのテキスト物事や変数を置き換え)最後の行を選択し、VBでExcelでフォーマットします。

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 
+0

あなたが本当にVBを使用しています。ネット?コードはちょうど普通のVBAのように見えるので、誤って[vb.net]タグを追加した可能性があります。 – YowE3K

+0

'Set ws = ActiveWorkbook.ActiveSheet'?アクティブなシートを使用する必要がある場合は、 'Set ws = ActiveSheet'を実行します。 –

+0

vb.netタグを削除し、ActiveWorkbookを削除するコードを更新しました。みんなありがとう! – Motornerve

答えて

1

は、以下のコードを使用してコードを交換してみてください:

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 
+0

この行に「タイプの不一致」エラーがあります LastRow = .Cells(.Rows.Count、 "B")。終了(xlUp) ' 列の最後の行を取得します。 – Motornerve

+0

@Motornerveあなたが正しいです、更新されたコードで今すぐお試しください –

+0

作品は魅力的です!以前はRngAnchorを使ったことはありませんでしたが、私はその単純さが好きです。私はあなたにアップフォートを与えましたが、うーん、私は新しく、数字に影響を与えることはできません。また、データをセルに配置した後にデータをフォーマットする必要があることもわかりました。私はそれも私の問題だったと思う。シャイありがとう! – Motornerve

関連する問題