VBA

2016-04-24 17 views
0
私はこのコードを持っている

にExcelシートのすべての行を反復処理する方法(このコードは、Excelファイルを読み込もうとするアクセスVBAであると確認した後、おそらくそれをインポート):今VBA

Set ExcelApp = CreateObject("Excel.application") 
Set Workbook = ExcelApp.Workbooks.Open(FileName) 
Set Worksheet = Workbook.Worksheets(1) 

Excelワークシートのすべての行を繰り返し処理したいと思います。

for each row in Worksheet.rows 
     ProcessARow(row) 
next row 

function ProcessARow(row as ????) 
    ' process a row 
    ' how Should I define the function 
    ' how can I access each cell in the row 
    ' Is there any way that I can understand how many cell with data exist in the row 

end function 

私の質問:

  1. それはデータを持っているすべての 行で正常に反復処理する各コードを定義するにはどのように私はこのような何かをしたいですか?
  2. ProcessARowを正しく定義する方法
  3. 行内の各セルの値を取得する方法。
  4. 行にデータがあるセルの数を確認するにはどうすればよいですか?
  5. 各セルのデータ型は何かを検出する方法はありますか?

    How to define the for each code that it iterate correctly on all rows that has data? 
    

    が、どのような他の質問について:

編集1つの

リンクが問題に解決しますか?

たとえば、ProcessARowを正しく定義するにはどうすればよいですか?あなたが行の値が必要な場合は

+1

[Excel 2007ワークシートの最後の行を検索するための[Access 2007 vba]の可能な複製](http://stackoverflow.com/questions/17771620/access-2007-vba-to-find-lastrow-in-excel- 2007-ワークシート) –

+0

データ型については、 'Vartype'または' Typename'関数を見てください。値については、セルプロパティのHELPを参照してください。これらはすべて、このフォーラムにすでに掲載されている多くの回答と例で見つけることができます。 –

答えて

0

は、あなたが「値」プロパティを使用する必要があり、それぞれの値を取得するためのサイクルを行った後

for each row in Worksheet.rows 
     Values=row.Value 
     For each cell in Values 
      ValueCell=cell 
     next cell 
next row 
+0

col値を使用してセル値にアクセスできますか?私がセル1にアクセスしたいとします(列1に対応しています)、どうすればそれにアクセスできますか? – mans

+0

@mans Cellsオブジェクトのプロパティを見てください。 'Cells(row、column)' –

+0

@RonRosenfeld row.Valuesの型は何ですか?残念ながら、アクセス2007の助けはあまり明確ではありません。 – mans

0

は、残念ながら、あなたの質問は非常に広いですが、私は以下のサブ・ルーチンが信じていますあなたの後に何を達成するためのいくつかの方法を示すことができます。どんなデータ型であれ、どのデータ型に依存するかによって、各セルがより関わり合いますが、私はそれを比較したいと思っています。

sub hopefullyuseful() 
    dim ws as worksheet 
    dim rng as Range 
    dim strlc as string 
    dim rc as long, i as long 
    dim lc as long, j as long 
    dim celltoprocess as range 
    set ws = activeworkbook.sheets(activesheet.name) 
    strlc = ws.cells.specialcells(xlcelltypeLastCell).address 
    set rng = ws.range("A1:" & lc) 

    rc = rng.rows.count() 
    debug.print "Number of rows: " & rc 

    lc = rng.columns.count() 
    debug.print "Number of columns: " & lc 
    ' 
    'method 1 looping through the cells' 
    for i = 1 to rc 
     for j = 1 to lc 
      set celltoprocess = ws.cells(i,j) 
      'this gives you a cell object at the coordinates of (i,j)' 
      '[PROCESS HERE]' 
      debug.print celltoprocess.address & " is celltype: " & CellType(celltoprocess) 
      'here you can do any processing you would like on the individual cell if needed however this is not the best method' 
      set celltoprocess = nothing 
     next j 
    next i 
    'method 2 looping through the cells using a for each loop' 
    for each celltoprocess in rng.cells 
     debug.print celltoprocess.address & " is " & CellType(celltoprocess) 
    next celltoprocess 

    'if you just need the data in the cells and not the actual cell objects' 
    arrOfCellData = rng.value 

    'to access the data' 
    for i = lbound(arrOfCellData,1) to ubound(arrOfCellData,1) 
     'i = row' 
     for j = lbound(arrOfCellData,2) to ubound(arrOfCellData,2) 
      'j = columns' 
      debug.print "TYPE: " & typename(arrOfCellData(i,j)) & " character count:" & len(arrOfCellData(i,j)) 

     next j 
    next i 
    set rng=nothing 
    set celltoprocess = nothing 
    set ws = nothing 
end sub 

Function CellType(byref Rng as range) as string 
    Select Case True 
     Case IsEmpty(Rng) 
      CellType = "Blank" 
     Case WorksheetFunction.IsText(Rng) 
      CellType = "Text" 
     Case WorksheetFunction.IsLogical(Rng) 
      CellType = "Logical" 
     Case WorksheetFunction.IsErr(Rng) 
      CellType = "Error" 
     Case IsDate(Rng) 
      CellType = "Date" 
     Case InStr(1, Rng.Text, ":") <> 0 
      CellType = "Time" 
     Case IsNumeric(Rng) 
      CellType = "Value" 
    End Select 
end function 

sub processRow(byref rngRow as range) 
    dim c as range 
    'it is unclear what you want to do with the row however... if you want 
    'to do something to cells in the row this is how you access them 
    'individually 

    for each c in rngRow.cells 
     debug.print "Cell " & c.address & " is in Column " & c.column & " and Row " & c.row & " has the value of " & c.value 
    next c 
    set c = nothing 
    set rngRow = nothing 

あなたが他の質問はあなたが私は信じてい@krazynhazyが提供するソリューションを好む一方で

0

を達成しようとしているものに、より具体的にする必要があります答えたい場合

サブ終了次の解決策はあなたが求めているものにやや近づくかもしれません。それでも、私はIif私は現在以下のコードで持っているすべてではなく、Krazynhazyによって提供されるCellType関数を使用したいと思います。

Option Explicit 

Sub AllNonEmptyCells() 

Dim rngRow As Range 
Dim rngCell As Range 
Dim wksItem As Worksheet 

Set wksItem = ThisWorkbook.Worksheets(1) 

On Error GoTo EmptySheet 
For Each rngRow In wksItem.Cells.SpecialCells(xlCellTypeConstants).EntireRow.Rows 
    Call ProcessARow(wksItem, rngRow.Row) 
Next rngRow 

Exit Sub 

EmptySheet: 
    MsgBox "Sheet is empty." & Chr(10) & "Aborting!" 
    Exit Sub 

End Sub 

Sub ProcessARow(wksItem As Worksheet, lngRow As Long) 

Dim rngCell As Range 

Debug.Print "Cells to process in row " & lngRow & ": " & wksItem.Range(wksItem.Cells(lngRow, 1), wksItem.Cells(lngRow, wksItem.Columns.Count)).SpecialCells(xlCellTypeConstants).Count 
For Each rngCell In wksItem.Range(wksItem.Cells(lngRow, 1), wksItem.Cells(lngRow, wksItem.Columns.Count)).SpecialCells(xlCellTypeConstants) 
    Debug.Print "Row: " & lngRow, _ 
       "Column: " & rngCell.Column, _ 
       "Value: " & rngCell.Value2, _ 
       IIf(Left(rngCell.Formula, 1) = "=", "Formula", IIf(IsDate(rngCell.Value), "Date", IIf(IsNumeric(rngCell.Value2), "Number", "Text"))) 
Next rngCell 

End Sub 

注意、あなたはまた、行が処理されるべきでシートを含んでいなければならない行を呼び出すために、サブを呼び出す必要があること。