2017-01-25 6 views
2

Cantが私のコードが出力を表示しない理由を理解しているようです。新しいVBAプログラマーは基礎を知っているだけなので、どんな助けも役に立つでしょう。IfとDoUntil VBAコードで出力が表示されない

私が欲しいのは、Excelが特定のtext1の特定の列をチェックしてから、それらの値がtext2に達するまでコピーして貼り付けを開始することです。その後、同じ方法で次の5番目の列をチェックします。 自分のコードに変更を提案できる場合。 列のforループを入れないと、私のコードは次のようになります。

Private Sub CommandButton7_Click() 
    Dim x As Long, y As Long 
    Dim a As Long 

    y = 1 'starts with the first column 
    x = 1 'first row 
    a = 70 'this is the row where i want the data to be posted 

    If Cells(x, y).Value = "text1" Then 'check first for specific text 
     Do Until Cells(x, y).Value = "text2" 'stop here 
      Cells(a, y).Value = Cells(x, y).Value 'copy that data to new row 
      Cells(a, y + 1).Value = Cells(x, y + 1).Value 'and the column adjacent to it 
      x = x + 1 
      a = a + 1 
     Loop 
    Else 
     x = x + 1 'if not on that row then check the next row  
    End If 
End Sub 
+2

最初の行に「text1」がある場合、コードは正常に動作します。そうでなければ、 'x'に1を加えて、それ以外は何もしません。もう1つのループを追加して 'If Cells(x、y).Value =" text1 "'または 'GoTo'文を追加することができます。コードがどのように機能しているかを確認するには、F8キーを押します。これは、行ごとにコードを実行し、結果はすぐにワークシートに表示されます。 –

+0

あんまり大好き!私のelseの後に追加され、それは働いた! @ EganWolf –

答えて

1

あなたのコードはあなたが望むことをしている必要がありますので、ここで間違っているのを見るのは本当に難しいです。

VBAが大文字の文字列を別のものとして扱うため、caseが異なる場合は、ループを実際に入力しない可能性があります。そして、私はtext1が質問の単なるサンプル文字列であると仮定しています。

小文字の文字列を比較すると、大文字の文字があれば正しく比較され、LCase関数を使用すると正しく比較されます。

完全なコード、ハードの

Private Sub CommandButton7_Click() 

Dim x As Long, y As Long 
Dim a As Long 

    y = 1 'starts with the first column 

    x = 1 'first row 
    a = 70 'this is the row where i want the data to be posted 
      If LCase(Cells(x, y).Value) = LCase("text1") Then 'check first for specific text 
      Do Until LCase(Cells(x, y).Value) = LCase("text2") 'stop here 
       Cells(a, y).Value = Cells(x, y).Value 'copy that data to new row 
       Cells(a, y + 1).Value = Cells(x, y + 1).Value 'and the column adjacent to it 
       x = x + 1 
       a = a + 1 
      Loop 
      Else: x = x + 1 'if not on that row then check the next row 

     End If 
End Sub 
+0

ありがとう、しかしEganは正しかった。最初に確認した後、それは止まるでしょう。私のelseの後にGoToを追加し、それは完全に動作します。 –

0

種類は、大きな画像を表示しますが、私はあなたが望む結果生成思う:このコードは完璧にはほど遠いですが、中に動作するはず

Sub FindText() 


Dim textFound As Range 
Dim x As Long, y As Long 
Dim a As Long 
y = 1 'starts with the first column 

x = 0 'first row 
a = 70 'this is the row where i want the data to be posted 

Set textFound = ActiveSheet.Columns(y).Cells.Find("Text1") 

Do Until textFound.Offset(x, 0).Value = "Text2" 

    Cells(a + x, y).Value = textFound.Offset(x, 0).Value 
    Cells(a + x, y + 1).Value = textFound.Offset(x, 1).Value 
    x = x + 1 
Loop 


End Sub 

をほとんどの状況。

関連する問題