• 締切済み

VB  FOR~NEXTについて

kokoroneの回答

  • kokorone
  • ベストアンサー率38% (417/1093)
回答No.1

For ループの数行前で、wを定義し、0で初期化しているのに、 For 文で、 As Integer で再定義していますが、これは、故意ですか? また、Forループの中で、w=w+1とカウントアップしていますが、これも故意ですか? For ループの変数をw以外にしたら、どうなりますか?

関連するQ&A

  • VB TEXTデータについて

    下記のコードでTEXTを読み取りできましたが、読み取ったデータを使用して座標計算したいのですが 例えば、6列6行あるとします、1列目(X)、2列目(y)、3列目(X1)、4列目(y2)、5列目(角度)、6列目(予備)、となっているものが6行あります、1列目・1行の値から、6列目・6行の値まで指定するとき どのようなコードがあるのでしょうか、宜しくお願いします。 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sr As System.IO.StreamReader = Nothing ' Try Dim file As String = TextBox1.Text 'SRとは New System.IO.StreamReaderの略 sr = New System.IO.StreamReader(file, System.Text.Encoding.GetEncoding("shift_jis")) Dim doc As String = "" '1行ずつテキストを読み取る Do Until sr.EndOfStream '改行コードが取り除かれてしまうのでVBnewlineで改行を付け直し doc &= sr.ReadLine & vbNewLine Loop 'TEXTBOX.2にdocを代入 TextBox2.Text = doc Catch ex As Exception MessageBox.Show(ex.Message, "read") Finally If sr IsNot Nothing Then sr.Close() sr.Dispose() End If End Try End Sub

  • VBA  FOR・・・・NEXについて

    ★から★までのコードがどうしても成立しません、★以外は線分としてコードが成立するのですが ★から★を入れると成立しません。どう構築したいいのでしょうか、宜しくお願いします。 エクセルでCVSファイルの数値を線分として図面に記入するためのコードになります。 説明不足ですが、宜しくお願いします。 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' LineData用の変数です。 Dim cadLineData As New zwDrawCAD.LineData Dim cadStartPoint As zwDrawCAD.DPoint = cadLineData.Start Dim cadEndPoint As zwDrawCAD.DPoint = cadLineData.End 'とりあえず線のスタート点は(100,100)にしてます。 Const X As Integer = 100 'cadStartPoint.x = 100 Const Y As Integer = 100 'cadStartPoint.y = 100 Const xx As Integer = 5 Const yy As Integer = 6 Dim len As Integer 'TextBox1に入力された線の長さ Dim angle As Double 'TextBox2に入力された角度 単位は度 Dim ansx As Integer '計算された、xの座標 Dim ansy As Integer '計算された、yの座標  ★ Dim ddd(5, 6) As Integer 'TEXTの階数 xx = 5 yy = 6 FILETIME = "c:\DummyData.csv" Open Filename For Input As #1 For i = 1 To yy For j = 1 To xx Input #1, ddd(xx, yy) Next j Next i ★ Close #1 len = CDbl(TextBox5.Text / 2) angle = CDbl(TextBox2.Text + 90) ansx = X + len * (Math.Cos(angle / 180 * Math.PI)) '終点のx座標 ansy = Y + len * (Math.Sin(angle / 180 * Math.PI)) '終点のy座標 cadStartPoint.x = 100 cadStartPoint.y = 100 cadEndPoint.x = ansx cadEndPoint.y = ansy ' 参照したオブ ジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadStartPoint) ' アクティブなDocumentを参照します。 Dim cadDocument As zwDrawCAD.Document = cadApplication.ActiveDocument ' 現在の作図属性を参照します。 Dim cadCurrentDraw As zwDrawCAD.DrawData = cadDocument.CurrentDraw ' 現在のレイヤ、線種、線幅、線色を設定します。 cadLineData.LayerNo = cadCurrentDraw.LayerNo cadLineData.PenStyle = cadCurrentDraw.PenStyle cadLineData.PenWidth = cadCurrentDraw.PenWidth cadLineData.PenColor = cadCurrentDraw.PenColor ' 参照したオブジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadCurrentDraw) ' データベースを参照します。 Dim cadDataBase As zwDrawCAD.CADDB = cadDocument.DataBase ' Undo情報を設定します。 cadDataBase.SetDelimitter("始点座標(100,100) 終点座標((ansx,ansy) の線分") ' 追加した要素の参照用変数です。 Dim cadAddPrimitive As zwDrawCAD.Primitive ' 要素を追加します。 cadAddPrimitive = cadDataBase.Add(cadLineData) ' 参照したオブジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadAddPrimitive) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadLineData) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadDataBase) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadDocument)

  • VBA FOR・・・・NEXTについて

    ★から★までのコードがどうしても成立しません、★以外は線分としてコードが成立するのですが ★から★を入れると成立しません。どう構築したいいのでしょうか、宜しくお願いします。 エクセルでCVSファイルの数値を線分として図面に記入するためのコードになります。 説明不足ですが、宜しくお願いします。 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' LineData用の変数です。 Dim cadLineData As New zwDrawCAD.LineData Dim cadStartPoint As zwDrawCAD.DPoint = cadLineData.Start Dim cadEndPoint As zwDrawCAD.DPoint = cadLineData.End 'とりあえず線のスタート点は(100,100)にしてます。 Const X As Integer = 100 'cadStartPoint.x = 100 Const Y As Integer = 100 'cadStartPoint.y = 100 Const xx As Integer = 5 Const yy As Integer = 6 Dim len As Integer 'TextBox1に入力された線の長さ Dim angle As Double 'TextBox2に入力された角度 単位は度 Dim ansx As Integer '計算された、xの座標 Dim ansy As Integer '計算された、yの座標  ★ Dim ddd(5, 6) As Integer 'TEXTの階数 xx = 5 yy = 6 FILETIME = "c:\DummyData.csv" Open Filename For Input As #1 For i = 1 To yy For j = 1 To xx Input #1, ddd(xx, yy) Next j Next i ★ Close #1 len = CDbl(TextBox5.Text / 2) angle = CDbl(TextBox2.Text + 90) ansx = X + len * (Math.Cos(angle / 180 * Math.PI)) '終点のx座標 ansy = Y + len * (Math.Sin(angle / 180 * Math.PI)) '終点のy座標 cadStartPoint.x = 100 cadStartPoint.y = 100 cadEndPoint.x = ansx cadEndPoint.y = ansy ' 参照したオブ ジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadStartPoint) ' アクティブなDocumentを参照します。 Dim cadDocument As zwDrawCAD.Document = cadApplication.ActiveDocument ' 現在の作図属性を参照します。 Dim cadCurrentDraw As zwDrawCAD.DrawData = cadDocument.CurrentDraw ' 現在のレイヤ、線種、線幅、線色を設定します。 cadLineData.LayerNo = cadCurrentDraw.LayerNo cadLineData.PenStyle = cadCurrentDraw.PenStyle cadLineData.PenWidth = cadCurrentDraw.PenWidth cadLineData.PenColor = cadCurrentDraw.PenColor ' 参照したオブジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadCurrentDraw) ' データベースを参照します。 Dim cadDataBase As zwDrawCAD.CADDB = cadDocument.DataBase ' Undo情報を設定します。 cadDataBase.SetDelimitter("始点座標(100,100) 終点座標((ansx,ansy) の線分") ' 追加した要素の参照用変数です。 Dim cadAddPrimitive As zwDrawCAD.Primitive ' 要素を追加します。 cadAddPrimitive = cadDataBase.Add(cadLineData) ' 参照したオブジェクトを解放します。 System.Runtime.InteropServices.Marshal.ReleaseComObject(cadAddPrimitive) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadLineData) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadDataBase) System.Runtime.InteropServices.Marshal.ReleaseComObject(cadDocument)

  • ※VBA配列

    http://oshiete1.goo.ne.jp/qa5196795.htmlで 質問させてもらった者です。質問不足だったため 質問の内容を追加したかったのですが、追加の方法がわからず またこちらで質問させていただきました Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim X1 As Integer, Y1 As Integer Dim X2 As Integer, Y2 As Integer Dim maru As String, yoko As String, tate As String Dim sankaku As String, shikaku As String Sub 描画() Cells(Y2, X2).Value = shikaku Cells(Y1, X1).Value = sankaku Cells(Y, X).Value = maru End Sub Sub 削除() Cells(Y2, X2).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() X2 = X1 Y2 = Y1 X1 = X Y1 = Y If yoko = "右" Then X = X + 1 Else X = X - 1 End If If X = 30 Then yoko = "左" ElseIf X = 1 Then yoko = "右" End If If tate = "上" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 20 Then tate = "下" ElseIf Y = 1 Then tate = "上" End If End Sub Sub main() maru = "●" sankaku = "▲" shikaku = "■" X = 1 Y = 1 X1 = 1 Y1 = 1 X2 = 1 Y2 = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub a~tの文字が、上記のような動きをする プログラムを作成するにはどのように配列を活かせばいいですか? 配列がよくわかっておらず勉強したのですが…使えずにいます;;

  • Excel VBA ・・・教えてください

    何度も質問させて頂いてます。すみません、 下記のプログラムはこの場で教えて頂いたプログラムで、 実行すると●の後を▲や■が追いかける動きをします。 下記のプログラムをある程度使用して 1~20の数字が順々で追いかけっこする プログラムを作成するにはどのようにすればいいのでしょうか… できればプログラムは長めにならず 20の数字から簡単に増やすことのできるような そんなプログラムが作成したいです… どなたかアドバイスお持ちの方 教えて下さいお願いします... Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim X1 As Integer, Y1 As Integer Dim X2 As Integer, Y2 As Integer Dim maru As String, yoko As String, tate As String Dim sankaku As String, shikaku As String Sub 描画() Cells(Y2, X2).Value = shikaku Cells(Y1, X1).Value = sankaku Cells(Y, X).Value = maru End Sub Sub 削除() Cells(Y2, X2).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() X2 = X1 Y2 = Y1 X1 = X Y1 = Y If yoko = "右" Then X = X + 1 Else X = X - 1 End If If X = 30 Then yoko = "左" ElseIf X = 1 Then yoko = "右" End If If tate = "上" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 20 Then tate = "下" ElseIf Y = 1 Then tate = "上" End If End Sub Sub main() maru = "●" sankaku = "▲" shikaku = "■" X = 1 Y = 1 X1 = 1 Y1 = 1 X2 = 1 Y2 = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub

  • VBAでのデータ転記(再)

    データがsheet1に縦記述で書いてあります。 C列 D列 ああ A01 いい A02 うう A03 . ささ B01 しし B02 すす B03 . はは AA01 ひひ AA02 ふふ AA03 . やや AB01 ゆゆ AB02 よよ AB03 . D列を元にしてC列の文字列をsheet2に以下のように並べたいです。D列はアドレス扱いででアルファベットが変わったら改行してデータを並べるようにします。 A列  B列 C列 ああ いい うう・ ささ しし すす・ ・・・・・ はは ひひ ふふ・ やや ゆゆ よよ・ 作成したコード Dim I As Integer, MAE As String, IMA As String, TEMP2 As String Dim X1 As Integer, Y1 As Integer, X3 As Integer, Y3 As Integer, PINNAME As String X1 = 4: Y1 = 2 X3 = 1: Y3 = 1 MAE = Sheets("Sheet1").Cells(Y1, X1) Do PINNAME = Sheets("Sheet1").Cells(Y1, X1 - 1) IMA = Sheets("Sheet1").Cells(Y1, X1) '今の値が入っている If IMA = "" Then Exit Do End If If Left(MAE, 1) <> Left(IMA, 1) Then Y3 = Y3 + 1: X3 = 1 Sheets("Sheet2").Cells(Y3, X3).Value = PINNAME X3 = X3 + 1 Else Sheets("Sheet2").Cells(Y3, X3).Value = PINNAME X3 = X3 + 1 End If MAE = Sheets("Sheet1").Cells(Y1, X1) Y1 = Y1 + 1 Loop Until IMA = "" Left関数でアドレスの左1文字を前後のセルで比較して異なる場合、改行する方法を考えましたが、AA01 AA02・・・がムリです。このコードに補足すればできるでしょうか?それとも別の考え方で行った方がいいでしょうか? 初心者レベルなので考え方も教えて下さい。宜しくお願いします。

  • VB2005 図形

    VisualBasic2005で Dim n As Integer, m As Integer Dim i As Integer, j As Integer Const lenn = 1500, PAI = 3.14159 Dim th As Single, th1 As Single, th2 As Single Dim x1 As Integer, x2 As Integer Dim y1 As Integer, y2 As Integer n = Val(TextBox1.Text) m = Val(TextBox2.Text) If n < 2 Or m < 2 Then   Exit Sub End If For i = 0 To n - 1   For j = 1 To n - 1     th = j * (PAI / n)     th1 = i * (PAI / m) + th     th2 = i * (PAI / n) - th     x1 = Math.Cos(th1) * lenn + 2000     y1 = Math.Sin(th1) * lenn + 2000      x2 = Math.Cos(th2) * lenn + 2000      y2 = Math.Sin(th2) * lenn + 2000   Next Next の様なコードを使って 球をPictureBoxに書きたいのですが どこにどんなコードが足りないのでしょうか? 教えて下さい

  • VB.NETのメモリ領域について

    VB.NETのメモリ領域について 以下の(1)~(12)の変数のために、 スタック領域、静的領域、ヒープ領域のどこのメモリが使われるか教えてください。 Class Sample   Dim a As Integer '(1)   Dim b As String = "BBB" '(2)   Shared c As Integer '(3)   Shared d As String = "DDD" '(4)   Sub X()     Dim f As Integer '(5)     Dim g As String = "GGG" '(6)     Static h As Integer '(7)     Static i As String = "III" '(8)   End Sub   Shared Sub Y()     Dim k As Integer '(9)     Dim l As String = "LLL" '(10)     Static m As Integer '(11)     Static n As String = "NNN" '(12)   End Sub End Class それぞれこんな認識で合ってますか? スタック領域 (1)(5)(9) スタック領域にポインタ+ヒープ領域に実体 (2)(6)(10) 静的領域 (3)(7)(11) 静的領域にポインタ+ヒープ領域に実体 (4)(8)(12)

  • エラー"Next"に対する"for"がありません

    EXCELのVBAで、A列3行目から入力されているデータ(A列2行目タイトル="FA")を 一度全て半角に変換し、変換した"変換文字"から"!"や"#"の記号をASC関数を利用し 削除、記号だけを削除した文字列をE列に取得しようとしていますが NEXTにたいするFORがありませんとエラーが出てしまいます。 TARGETの設定の仕方が悪いのでしょうか? (win7/EXCEL2010使用) Dim セル As Range Dim TARGET As Range Dim 変換文字 As String Dim i As Long Dim W As Worksheet Set W = Sheets("DATA転記") Set TARGET = W.Range("A3", Range("A65536").End(xlUp)) For Each セル In TARGET 変換文字 = StrConv(セル.Text, vbNarrow) For i = 1 To Len(変換文字) If Asc(変換文字) >= 32 And Asc(変換文字) <= 47 And _ Asc(変換文字) >= 58 And Asc(変換文字) <= 64 And _ Asc(変換文字) >= 91 And Asc(変換文字) <= 96 And _ Asc(変換文字) >= 123 And Asc(変換文字) <= 126 Then _ 変換文字 = WorksheetFunction.Replace(変換文字, i, 1, "") Next i セル.Cells(, 5).Value = 変換文字 Next セル

  • VB.NETの関数プロシージャの書き方を教えてください。

    Public A As String という変数にある文字列データが入っていて、 ボタンを押すとその時に変数X,Y,Zに入っている 文字列を検索してButton1_Clickプロシージャ内に戻って来て KekkaX,KekkaY,KekkaZに数値が入るような関数プロシージャなのですが、 呼び出す部分の書き方と関数のプロシージャ名の書き方を教えて頂きたいです。 検索する部分は自分で出来ると思うので大丈夫です。 Public A As String = "何かの文字列" Button1_Click Dim X,Y,Z As String Dim KekkaX,KekkaY,KekkaZ as Integer '変数Aの中からX,Y,Zを検索したい X="*****" Y="ABCDE" Z="#####" GetKensakuKekka(????????) ←この部分の書き方を教えて下さい。 'KekkaX,KekkaY,KekkaZ 内に検索結果を取得したい Console.WriteLine(KekkaX) Console.WriteLine(KekkaY) Console.WriteLine(KekkaZ) End Sub Function GetKensakuKekka (???????????) ←この部分の書き方を教えて下さい。 ・ ・ ・ End Function よろしくお願い致します。