2016-10-18 3 views
0

Ich bin neu, VBA-Codierung zu übertreffen. Ich habe die Adresse der letzten nicht leeren Zelle in einer Zeile A1 Und jetzt möchte ich zur nächsten rechten Zelle bewegen.Wechseln zur nächsten Zelle in einer Zeile

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 

Ausgangszelle Adresse: API1

REQ: Recht auf die Ausgabezelle Adresse verschieben (API1), dh der Ausgang APJ1 ist

mir jemand plz helfen kann.

+2

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

+0

das funktioniert gut. Danke Scott. – Dhiman

Antwort

0

Ihr aktueller Code bewegt eine Zelle nach unten statt einer Zelle zur rechts:

Statt:

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

Verwandte Themen