2017-04-19 4 views
1

Sie fügt eine neue Spalte aus einer Vorlage einer anderen Seite hinzu, so dass Formeln und Formatierungen kopiert werden. Es wird in 1 Spalte vor der letzten Spalte hinzugefügt. Die letzte Spalte wird als Anker verwendet. Ich möchte es auswählen und gehen Sie auf die myCol & "5", aber ich bin nicht in der Lage, es zur Arbeit zu bekommen.Wählen Sie die Spalte aus, sobald sie hinzugefügt wurde

Function GetColumnLetter(colNum As Long) As String 
    Dim vArr 
    vArr = Split(Cells(1, colNum).Address(True, False), "$") 
    GetColumnLetter = vArr(0) 
End Function 

Sub NewPlate_F() 'Insert Column Button 
    Application.ScreenUpdating = False 

    Dim sht8 As Worksheet 
    Set sht8 = ThisWorkbook.Worksheets("Add") 
    Dim sht2 As Worksheet 
    Set sht2 = ThisWorkbook.Worksheets("Foundation Plates") 

    Call Unprotect 
    sht2.Activate 

    Dim lastCol As Long 
    Dim myCol As String 
    Dim rng As Range 
    Dim cell As Range 

    With sht2 
     Set rng = Cells 

    lastCol = sht2.Cells(5, sht2.Columns.Count).End(xlToLeft).Column 
    myCol = GetColumnLetter(lastCol) 

    Set rng = sht2.Range(myCol & "5") 

    'MsgBox rng.Address 

     With sht2 
     Columns(myCol).EntireColumn.Insert 
     Columns(myCol).ColumnWidth = 13 
      Application.Wait (Now + 0.000005) 
     sht8.Range("H7:H48").Copy Range(myCol & "1") 
     Range(myCol & "5").Select 
     End With 
    End With 


    Call Format_Foundation 
    Call Unprotect 

    With sht2 
     Range(myCol & "5").Select 
    End With 
    sht2.Activate 

    Set sht2 = Nothing 
    Set sht8 = Nothing 
    Application.CutCopyMode = False 
    Application.ScreenUpdating = True 
End Sub 

Antwort

2

Probieren Sie es wie

With sht2 
    .Activate 
    .Range(myCol & "5").Select 
    '... or, 
    .Cells(5, lastCol).Select 
End With 

meine Verwendung eines prefixing . mit .Cells(...) und .Range(...) Hinweis. Wenn Sie sich in einem With ... End With befinden, übergibt der Präfixzeitraum (z. B..) Die Referenz des übergeordneten Arbeitsblatts an die .Range- oder .Cells-Eigenschaft.

Es würde sich lohnen, How to avoid using Select in Excel VBA macros zu lesen.

Verwandte Themen