VBA Excel:如何使用两个变量作为位置将值放入单元格

时间:2018-11-16 18:03:59

标签: excel vba excel-vba

我正在尝试使用变体“行”和“列”,并使用它们在单元格中输入“ x”。 “行”和“列”中存储有数字。 IMG1

IMG2

Private Sub CheckInButton_Click()

Dim found_name As Range
Dim name_to_find As String
Dim row As Variant
Dim column As Variant
Dim ColLetter As Variant
Dim xLocation As Excel.Range

row = 0
column = 0

name_to_find = Worksheets("Forms").Range("N5").Value

Set found_name = Worksheets("Updated 1.0").Range("A:A").Find(what:=name_to_find,LookIn:=xlValues, lookat:=xlWhole)

If Not found_name Is Nothing Then
    MsgBox (name_to_find & " found in row: " & found_name.row)
    row = found_name.row
    MsgBox (row)
Else
    MsgBox (name_to_find & " not found")
End If

event_to_find = Worksheets("Forms").Range("N3").Value
Set found_event = Worksheets("Updated 1.0").Range("A1:DZ1").Find(what:=event_to_find, LookIn:=xlValues, lookat:=xlWhole)
MsgBox (found_event)

If Not found_event Is Nothing Then
    ColLetter = Split(found_event.Address, "$")(1)
    MsgBox (event_to_find & " found in column: " & ColLetter)
    column = found_event.column
    MsgBox (column)
Else
    MsgBox (event_to_find & " not found")
End If

Worksheets("Updated 1.0").Cells(column & row).Value2 = "x"

End Sub

2 个答案:

答案 0 :(得分:2)

您可以只使用Cells()

Cells(row, column).Value = "X"

所以,就您而言:

Worksheets("Updated 1.0").Cells(row, column).Value = "x"

答案 1 :(得分:0)

.Cells接受两个参数,行和列。 &运算符连接两个值,所以您只给.Cells一个参数,该行附加到该列。

只需将Worksheets("Updated 1.0").Cells(column & row).Value2 = "x"更改为Worksheets("Updated 1.0").Cells(row, column).Value2 = "x"