2016-10-18 76 views
0

我是新的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

詢價是:向右移動到輸出單元格地址(API1),即輸出APJ1

任何人可以幫助我PLZ。

+2

'CELL2 = rng1.Offset(0, 1).Address(0,0)' –

+0

這工作正常。感謝斯科特。 – Dhiman

回答

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 

enter image description here

相關問題