VBA工作列?

时间:2015-03-12 21:16:38

标签: excel vba excel-vba

我的代码工作正常,但不是我想要的!目前,我已设法让它在一个单元格上工作。

我需要处理一列数据 - 列'G'(只有6行),然后在数据结束时结束。

我知道我必须改变代码所说'target = cells(2,7)'的部分,而不是我需要写的代替这个。

编辑我正在编写此代码来更新库存表,因此col G中的数据是已售出的商品,我需要这样才能匹配商品A中的商品代码,从中扣除它当前股票的col E列表。我有那么多工作,但仅限于单元格G2中的数据,或代码中的(2,7)。 修改

Sub Order()
rowdata = 1
Do While Cells(rowdata, 1) <> ""
    rowdata = rowdata + 1
Loop
dataend = rowdata - 1
rowwrite = rowdata + 2
Cells(rowwrite, 1) = "Item Code"
For col = 1 To 3
    Cells(rowwrite, col) = Cells(1, col)
Next col
rowwrite = rowwrite + 1

target = Cells(2, 7)

For rowdata = 2 To dataend
    If Cells(rowdata, 1) = target Then
        Cells(rowdata, 5) = Cells(rowdata, 5) - 1
    End If
Next 

If Cells(rowdata, 5) = Cells(rowdata, 4) Then
    For col = 1 To 3
        Cells(rowwrite, col) = Cells(rowdata, col)
    Next col
    rowwrite = rowwrite + 1
End If
End sub

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

Dim rowDataMax As Long

 'yields the number of "rows" in column "A"
rowDataMax = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To rowDataMax
 'Do code here
 'i.e, Cells(i, 7) = ...
Next i