2016-07-26 8 views
0

Excel VBAでユーザーフォームを使用すると、自分のコードで使用されている現在の行のセルを参照する式セクションを作成できないようです。私はそれが行2のデータだけを参照するのは嫌です。行1はヘッダー行です。私の公式は動作しますが、Withループで作業中の行を参照しません

どうすればいいですか?

Private Sub cmdAccept_Click() 

Dim lastrow As Long 
Dim RowCount As Long 
Dim fieldData As Worksheet 
    lastrow = Range("A65536").End(xlUp).Row 

RowCount = Worksheets("fieldData").Range("A1").CurrentRegion.Rows.Count 
With Worksheets("fieldData").Range("A1") 
    .Offset(RowCount, 0).Value = Me.txtBox_fieldData_dateDelivered.Value 
    .Offset(RowCount, 1).Value = Me.comboBox_fieldData_fieldName.Value 
    .Offset(RowCount, 2).Value = Me.txtBox_fieldData_acres.Value 
    .Offset(RowCount, 3).Value = Me.comboBox_fieldData_crop.Value 
    .Offset(RowCount, 4).Value = Me.txtBox_fieldData_product1.Value 
    .Offset(RowCount, 5).Value = Me.txtBox_fieldData_product2.Value 
    .Offset(RowCount, 6).Value = Me.txtBox_fieldData_product3.Value 
    .Offset(RowCount, 7).Value = Me.txtBox_fieldData_product4.Value 
    .Offset(RowCount, 8).Value = Me.txtBox_fieldData_product5.Value 
    .Offset(RowCount, 9).Value = Me.txtBox_fieldData_product6.Value 
    .Offset(RowCount, 10).Value = "=Range($E2:$J2)" 
    .Offset(RowCount, 11).Value = "=SUM((11.06*$E2)+(11.7*$F2)+(11.04*$G2)+(10.9*$H2)+(10.28*$I2)+(9.5*$J2)" 
    .Offset(RowCount, 12).Value = "=SUM(((11.06*$E2)*(32/100)/$C2)+((11.7*$F2)*(10/100)/$C2)+((11.04*$G2)*(12/100)/$C2)+((10.9*$H2)*(8/100)/$C2)+((10.28*$I2)*(7/100)/$C2)+((9.5*$J2)*(0/100)/$C2))" 
    .Offset(RowCount, 13).Value = "=SUM(((11.06*$E2)*(0/100)/$C2)+((11.7*$F2)*(34/100)/$C2)+((11.04*$G2)*(0/100)/$C2)+((10.9*$H2)*(25/100)/$C2)+((10.28*$I2)*(24/100)/$C2)+((9.5*$J2)*(0/100)/$C2))" 
    .Offset(RowCount, 14).Value = "=SUM(((11.06*$E2)*(0/100)/$C2)+((11.7*$F2)*(0/100)/$C2)+((11.04*$G2)*(0/100)/$C2)+((10.9*$H2)*(0/100)/$C2)+((10.28*$2I)*(0/100)/$C2)+((9.5*$J2)*(0/100)/$C2))" 
    .Offset(RowCount, 15).Value = "=SUM(((11.06*$E2)*(0/100)/$C2)+((11.7*$F2)*(0/100)/$C2)+((11.04*$G2)*(26/100)/$C2)+((10.9*$H2)*(0/100)/$C2)+((10.28*$I2)*(0/100)/$C2)+((9.5*$J2)*(0/100)/$C2))" 
End With 
End Sub 

ユーザカラム10を通してカラム1を充填するユーザーフォームに入力特定のデータは、「データを受け入れる」をクリックし、次に式が実行する必要があり、その結果は、カラムから同じ行に配置されK列目からP列目に移動します。完了すると、「閉じる」コマンドボタンが押されるまで、ユーザーはデータを入力し続けます。

+0

'WITH'がループ構造ではありません。私はあなたが[Forループ](http://stackoverflow.com/documentation/vba/1873/flow-control-structures/8169/for-loop)が必要であると推測しています。 – Comintern

答えて

0

これがどのように機能するのか教えてください。

Private Sub cmdAccept_Click() 

    Dim x As Long 
    Dim fieldData As Worksheet 

    With Worksheets("fieldData") 
     x = .Range("A" & Rows.Count).End(xlUp).row + 1 
     .Cells(x, 1).Value = Me.txtBox_fieldData_dateDelivered.Value 
     .Cells(x, 2).Value = Me.comboBox_fieldData_fieldName.Value 
     .Cells(x, 3).Value = Me.txtBox_fieldData_acres.Value 
     .Cells(x, 4).Value = Me.comboBox_fieldData_crop.Value 
     .Cells(x, 5).Value = Me.txtBox_fieldData_product1.Value 
     .Cells(x, 6).Value = Me.txtBox_fieldData_product2.Value 
     .Cells(x, 7).Value = Me.txtBox_fieldData_product3.Value 
     .Cells(x, 8).Value = Me.txtBox_fieldData_product4.Value 
     .Cells(x, 9).Value = Me.txtBox_fieldData_product5.Value 
     .Cells(x, 10).Value = Me.txtBox_fieldData_product6.Value 
     .Cells(x, 11).Value = "=Range($E2:$J2)" 
     .Cells(x, 12).Value = "=SUM((11.06*$E2)+(11.7*$F2)+(11.04*$G" & x & ")+(10.9*$H" & x & ")+(10.28*$I2)+(9.5*$J2)" 
     .Cells(x, 13).Value = "=SUM(((11.06*$E2)*(32/100)/$C" & x & ")+((11.7*$F2)*(10/100)/$C" & x & ")+((11.04*$G" & x & ")*(12/100)/$C" & x & ")+((10.9*$H" & x & ")*(8/100)/$C" & x & ")+((10.28*$I2)*(7/100)/$C" & x & ")+((9.5*$J2)*(0/100)/$C" & x & "))" 
     .Cells(x, 14).Value = "=SUM(((11.06*$E2)*(0/100)/$C" & x & ")+((11.7*$F2)*(34/100)/$C" & x & ")+((11.04*$G" & x & ")*(0/100)/$C" & x & ")+((10.9*$H" & x & ")*(25/100)/$C" & x & ")+((10.28*$I2)*(24/100)/$C" & x & ")+((9.5*$J2)*(0/100)/$C" & x & "))" 
     .Cells(x, 15).Value = "=SUM(((11.06*$E2)*(0/100)/$C" & x & ")+((11.7*$F2)*(0/100)/$C" & x & ")+((11.04*$G" & x & ")*(0/100)/$C" & x & ")+((10.9*$H" & x & ")*(0/100)/$C" & x & ")+((10.28*$2I)*(0/100)/$C" & x & ")+((9.5*$J2)*(0/100)/$C" & x & "))" 
     .Cells(x, 16).Value = "=SUM(((11.06*$E2)*(0/100)/$C" & x & ")+((11.7*$F2)*(0/100)/$C" & x & ")+((11.04*$G" & x & ")*(26/100)/$C" & x & ")+((10.9*$H" & x & ")*(0/100)/$C" & x & ")+((10.28*$I2)*(0/100)/$C" & x & ")+((9.5*$J2)*(0/100)/$C" & x & "))" 

    End With 
End Sub 
+0

何も起こらなかった。 –

+0

列Aの最後に使用された行は何ですか? –

+0

@JDStewart 'cmdAccept'がクリックされるたびに' Worksheets( "fieldData")にレコードを追加するコードを修正しました。 –

0

ありがとう、トーマス、他のコーディング方法に目を向けてくれてありがとう!これは私が式が働く作るためのコードです...

Private Sub cmdAccept_Click() 
Dim RowCount As Long 
Dim fieldData As Worksheet 
Set fieldData = Worksheets("fieldData") 

RowCount = fieldData.Range("A1").CurrentRegion.Rows.Count + 1 
With fieldData 
    .Cells(RowCount, 1).Value = Me.txtBox_fieldData_dateDelivered.Value 
    .Cells(RowCount, 2).Value = Me.comboBox_fieldData_fieldName.Value 
    .Cells(RowCount, 3).Value = Me.txtBox_fieldData_acres.Value 
    .Cells(RowCount, 4).Value = Me.comboBox_fieldData_crop.Value 
    .Cells(RowCount, 5).Value = Me.txtBox_fieldData_product1.Value 
    .Cells(RowCount, 6).Value = Me.txtBox_fieldData_product2.Value 
    .Cells(RowCount, 7).Value = Me.txtBox_fieldData_product3.Value 
    .Cells(RowCount, 8).Value = Me.txtBox_fieldData_product4.Value 
    .Cells(RowCount, 9).Value = Me.txtBox_fieldData_product5.Value 
    .Cells(RowCount, 10).Value = Me.txtBox_fieldData_product6.Value 
    .Range("K" & RowCount - 1 & ":P" & RowCount).FillDown 
End With 

End Subの

関連する問題