编码工作如下,但值显示在错误的工作表上。你能帮我指导一下这个价值能出现在另一张纸上。我需要在Sheet2上显示该值。
Sub showRandomWord()
Dim ws As Worksheet
Dim stRow As Long, endRow As Long, dataCol As Long
Dim dispRow As Long, dispCol As Long
Set ws = Sheets("Sheet1")
stRow = 2
dataCol = 1
dispRow = 2
dispCol = 4
With ws
endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
.Cells(dispRow, dispCol).Value = _
.Cells(Application.RandBetween(stRow, endRow), dataCol).Value
End With
End Sub
答案 0 :(得分:1)
您需要为Sheet2设置另一个变量并设置单元格值。目前,您正在访问同一工作表以设置单元格值并确定随机填充的随机值。以下内容应该有效:
Sub showRandomWord()
Dim ws As Worksheet, ws2 As Worksheet
Dim stRow As Long, endRow As Long, dataCol As Long
Dim dispRow As Long, dispCol As Long
Set ws = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
stRow = 2
dataCol = 1
dispRow = 2
dispCol = 4
With ws
endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
End With
ws2.Cells(dispRow, dispCol).Value = ws.Cells(Application.RandBetween(stRow, endRow), dataCol).Value
End Sub