-1

在过去的几周里,当我学习 excel vb 时,我一直在不断地搜索堆栈溢出,但最后我被难住了。

我需要使用 x 位置的变量和 y 位置的变量来激活单元格。

xval 是整数 yval 是字符串

我正在尝试使用匹配功能来做到这一点,但没有运气。

这是我尝试过的。

Sheets("Attributes").Select      'this is the sheet with the raw data
Range("C2").Activate
Dim xval As String                'this is the row I'm looking for
Dim yval As Integer               'this is the colum I'm looking for
Dim value As String               'when I find the exact right cell on the next sheet this is the value that needs to go in it.

' start loop here

xval = ActiveCell.Offset(0, -2)   'populate variables
yval = ActiveCell
value = ActiveCell.Offset(0, 2)
Sheets("output").Select              'switch to output sheet
Range("A1").Activate

Cells(WorksheetFunction.Match(xval, "A:A", 0), WorksheetFunction.Match(yval, "A1:A500", 0)).Activate    'this line doesn't work


ActiveCell = value

'end loop when data runs out using is empty function
4

1 回答 1

0
Sub Tester()

    Dim xval As String
    Dim yval As Integer
    Dim v As String
    Dim shtOut As Worksheet
    Dim f As Range

    Set shtOut = ThisWorkbook.Sheets("output")

    With ThisWorkbook.Sheets("Attributes").Range("C2")
        xval = .Offset(0, -2).value
        yval = .value
        v = .Offset(0, 2).value
    End With

    On Error Resume Next
    Set f = shtOut.Cells(WorksheetFunction.Match(xval, shtOut.Range("A:A"), 0), _
                      WorksheetFunction.Match(yval, shtOut.Range("A1:A500"), 0))
    On Error GoTo 0

    If Not f Is Nothing Then f.value = v

    'flag if not found
    ThisWorkbook.Sheets("Attributes").Range("C2").Font.Color = _
                               IIf(f Is Nothing, vbRed, vbGreen)


End Sub
于 2013-05-23T17:07:41.873 回答