我是excel VBA编码的新手。 我有一行A1中最后一个非空单元格的地址 现在我想转到下一个正确的单元格。
Dim ws As Worksheet
Dim cell1 As String
Dim cell2 As String
Dim rng1 As Range
Set ws = Sheets("ADS User Data")
Set rng1 = ws.Rows(1).Find("*", ws.[a1], xlFormulas, , xlByColumns, xlPrevious)
MsgBox rng1
If Not rng1 Is Nothing Then
'MsgBox "rng1 contains " & rng1.Address(0, 0)
cell1 = (rng1.Address(0, 0))
cell2 = ActiveCell.Offset(1, 0).Select()
MsgBox cell1
MsgBox cell2
Else
MsgBox ws.Name & " row A1 is completely empty", vbCritical
End If
输出单元格地址:API1
Req Is:向右移动到输出单元格地址(API1),即输出为APJ1
任何人都可以帮助我。
答案 0 :(得分:0)
您当前的代码正在将一个单元格向下移动,而不是将一个单元格移动到向右:
相反:
Sub dural()
Dim ws As Worksheet
Dim cell1 As String
Dim cell2 As String
Dim rng1 As Range
Set ws = Sheets("ADS User Data")
Set rng1 = ws.Rows(1).Find("*", ws.[a1], xlFormulas, , xlByColumns, xlPrevious)
MsgBox rng1.Address(0, 0) & vbCrLf & rng1.Value
If Not rng1 Is Nothing Then
cell1 = rng1.Address(0, 0)
rng1.Offset(0, 1).Select
cell2 = Selection.Address(0, 0)
MsgBox cell1
MsgBox cell2
Else
MsgBox ws.Name & " row A1 is completely empty", vbCritical
End If
End Sub