2016-12-31 28 views
0

良い一日以降の予選値で置き換えるために、VBAコード特定の値をスキップして

私は、コーディングに非常に新しく、VBAマクロでものをコーディングする新しい完全なブランドです。 vbaの有用性を発見した後、私は今それについてもっと学ぶ努力をしています。助けや援助は非常に高く評価されます。

これは私がこれまでに働いていたコードです:

Option Explicit 

Const initrow As Integer = 3 
Const ENDROW As Long = 65536 
Const PrimaryLengthCol As Integer = 1 '"A" 

Sub Test() 

    Dim lastrow As Double 
    Dim i As Double 
    Dim irow As Double 

    lastrow = Cells(Rows.Count, "A").End(xlUp).Row 

    Application.ScreenUpdating = False 

    irow = 0 
    i = 0 
    For i = 0 To lastrow 
     If Cells(initrow + irow, PrimaryLengthCol + 2) = "BLANK" Then 
      Continue For 
      Cells(initrow + i, PrimaryLengthCol + 3).Value = Cells(initrow + irow, PrimaryLengthCol + 2).Value 
     End If 
    Next 

End Sub 

だから基本的に私が遭遇してる問題は次のとおりです。

私は列「A」(入力欄に以下しています):

  • 14.155
  • 14.128
  • 15.589
  • BLANK
  • BLANK
  • BLANK
  • 15.158

Iは、各セルをループにVBAコードを必要とし、細胞が等しくされている場合に "ブランク"(文字列)対応するセルは後続の番号をとる。さらに、列「A」の値がゼロに等しい場合、出力は列「B」の「ボイド」に等しい。

だから、コラム "B"(出力列)内の所望の出力は次のようにする必要があります:

  • ボイド
  • 14.155
  • 14.128
  • 15.589
  • 15.158
  • 15.158
  • 15.158
  • 15。

最後基準158は、その列内のセル「は」等しい「ブランク」より前のセルにゼロ値が先行したした場合、それらの「ブランク」場合と「ボイド」値にも等しいです出力列 "B":だから

列 "A" は、このシナリオを持っていた場合:

  • BLANK
  • BLANK

出力列「B」はする必要があります:

  • 無効
  • 無効
  • のボイド私はループにしたいので、継続適用する方法がわからないんだ

次の反復のために「空白」セルを「スキップ」するが、それに続く適格値と共に「B」列に対応する値をまだ充填する。私はvbaを通してこれを完成させることを好むでしょう。なぜなら、言語を学ぶことをもう一度試みているから、私はそれを自分自身に強要しているからです。

再度、この問題に関する援助をいただければ幸いです。

ありがとうございました!ここで

+0

(a)あなたはこれまでにどのようなコードを思い付いていますか? (これは「私たちがあなたのためにコードしている」サイトではありません - それは "私たちがあなたの**コード作業を手助けする"サイトです)。(b) 'BLANK 'は実際のテキスト文字列" BLANK "文字列、または空のセル? (c)これはExcelの公式では簡単に実現できますが、列Bと列Cを使用する必要があります(誰かが気に入っていない限り)。あなたのソリューション**は** VBAを必要としますか、またはあなたは数式の使用に開放されていますか? – YowE3K

答えて

0

Slaiのヒントに基づいて、既に私が試していたコードと結びついて大変感謝しています。問題は次のコードで解決されています。私はすべての私の元の基準を達成するために2つの機能に分割しなければならなかった。誰かが問題をより効率的に解決する方法を持っているなら、私はそれについて学ぶことが大事です。

Option Explicit 

Const initrow As Integer = 3 
Const ENDROW As Long = 65536 
Const PrimaryLengthCol As Integer = 1 '"A" 

Sub FirstIter() 

' initial iteration that exacts value from the adjustment column 
    Dim i As Double 
    Dim irow As Double 

    Worksheets(MatchMLWorksheet).Activate 
    irow = 0 

    While Not (IsEmpty(Cells(initrow + irow, PrimaryLengthCol + 2)))   ' loop until empty cell 
     If Cells(initrow + irow, PrimaryLengthCol + 2).Value = 0 Then 
      Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = "Void" ' cell.Offset(, 1) is the cell on the right 
     ElseIf Cells(initrow + irow, PrimaryLengthCol + 2).Value = "BLANK" Then 
      i = irow       ' sets the count to where cell iteration is 
      Do 
       i = i + 1      ' increments the Do Until loop untils 
               ' it hits the first cell with "BLANK" 
      Loop Until Cells(initrow + i, PrimaryLengthCol + 2).Value <> "BLANK" Or Cells(initrow + i, PrimaryLengthCol + 2).Value <> 0 

      Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = Cells(initrow + i, PrimaryLengthCol + 2).Value 
               ' Overall counter is at the iteration of "blank" 
               ' resets counter to match overall loop 
     Else 
      Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = Cells(initrow + irow, PrimaryLengthCol + 2).Value ' else just use the same value 
     End If 

     irow = irow + 1      ' move to the cell below 
    Wend 

End Sub 

Sub FinalIter() 
'Checks entire column to see if it contains any "BLANK" 

    Worksheets(MatchMLWorksheet).Activate 

    Dim num As Double 
    num = 0 
    Dim cell As Range 
    Dim iMsg As Integer 
    Dim b As Double 

    Columns("D:D").Select 
     Set cell = Selection.Find(What:="BLANK", After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False) 
    If cell Is Nothing Then 
     ' There are no more "BLANK" 
     iMsg = MsgBox("There are no more BLANK values!", vbOKOnly) 
    Else 
     While Not (IsEmpty(Cells(initrow + num, PrimaryLengthCol + 3)))   ' loop until empty cell 
      If Cells(initrow + num, PrimaryLengthCol + 3).Value = "BLANK" Then 
       b = num       ' sets the count to where cell iteration is 
       Do 
        b = b + 1      ' increments the Do Until loop untils 
                ' it hits the first cell with "BLANK" 
       Loop Until Cells(initrow + b, PrimaryLengthCol + 3).Value <> "BLANK" 

       Cells(initrow + num, PrimaryLengthCol + 3) = Cells(initrow + b, PrimaryLengthCol + 3).Value 
               ' Overall counter is at the iteration of "blank" 
               ' resets counter to match overall loop 
      End If 

      num = num + 1      ' move to the cell below 
     Wend 

    End If 

End Sub 
0

は、簡単に使用するために作成し、変更する可能性は少し異なったアプローチ(テストしていません)です:エクセルR1C1式(もテストされていない)と

Dim cell As Range 
Set cell = Cells(Rows.Count, "A").End(xlUp) ' last cell 

While cell.Row > 2       ' loop until row 3 
    If cell = 0 Then 
     cell(, 2) = "Void"     ' cell(, 2) is the cell on the right of cell 
    Else If cell = "BLANK" Then 
     cell(, 2) = cell(2)     ' value from the cell below 
    Else 
     cell(, 2) = cell     ' else just use the same value 
    End If 

    Set cell = cell(0)      ' move to the cell above 
Wend 

より高度なアプローチ:

Dim colB As Range 
Set colB = ThisWorkbook.Worksheets("Sheet1").Range("A3").CurrentRegion.Offset(,1).Resize(,1) 

colB.FormulaR1C1 = "=IF(RC[-1]=0, ""Void"", IF(RC[-1]=""BLANK"", R[1]C[-1], RC[-1]))" 

colB.Value2 = colB.Value2 ' optional to convert the formulas to values 

.CurrentRegionは、(セルA3をクリックするとCtrl + Aを押すのと同様の)空のセルで囲まれた矩形範囲を取得し、その後.Offset(,1)はです範囲Bを空にしない場合のために範囲を1つの列にサイズ変更する場合は、範囲Bを列Bに移動し、.Resize(,1)はオプションです。

+0

ありがとうございます。あなたの提案したループソリューションを試しましたが、 Else if cell = "BLANK" cell.Offset(、1)= cell.Offset(-1) セルが "BLANK"(テキスト文字列対応するセルは、後続の適格な番号をとる。だから、 15.589 BLANK BLANK BLANK 15.158 はとして出てくる必要があります: 15.589 15.158 15.158 15.158 15.158 そして、 "BLANK" の値の順序はランダムなので、私はしません特定のオフセットによってオフセットすることができます。 本当にありがとうございます。私はちょうどあなたについてオフセットを学んだ。 – NewbieCode

+0

@NewbieCodeああ私は参照してください..私は慎重に質問と例を読んでいない。ループは、最後のセルから開始するために逆にすることができます。 – Slai

+0

@NewbieCode更新されたサンプルを試して、必要に応じて修正することができます。 – Slai

関連する問題