2014-11-01 70 views
2

我写的VBA脚本,选择单元格范围在Excel表中,但它选择在这种格式$ G $ 1:$ K $ 1,但我只需要这种格式$ G:$ K选择消除数字或任何其他方法消除数字后由$。如何选择excel单元格范围只有字母表跟着?

Sub vloos() 

Dim rnge As Range 
Dim Rng As Range 
Dim user As String 
Dim file As String 
file = "E:\output4.xls" 
Workbooks.Add 
ActiveWorkbook.SaveAs file 
    Set nb = Application.Workbooks.Open(file) 
    Set ns = nb.Worksheets("Sheet1") 

    'get workbook path 
    filename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xls", Title:="Please select a file") 


    'set our workbook and open it 
    Set wb = Application.Workbooks.Open(filename) 
    'set our worksheet 
    Set ws = wb.Worksheets("Sheet1") 

    'set the range for vlookup 

On Error GoTo En 
    Set rnge = Application.InputBox(Prompt:="Range to format:", Title:="Format Range", Type:=8) 

    Set Rng = ws.Range(rnge.Address) 

    MsgBox Rng.Address 
En: 
End Sub 

回答

3

可以使用Range对象的EntireColumn属性来获取列

On Error GoTo En 
    Set rnge = Application.InputBox(Prompt:="Range to format:", Title:="Format Range", Type:=8) 

    Set Rng = rnge.EntireColumn 

    MsgBox Rng.Address 
+0

我越来越没有输出当我使用rnge.EntireColumn节目结束,从该行。 – 2014-11-02 00:30:03

+0

我收到错误“应用程序定义或对象定义的错误”。 – 2014-11-02 00:39:40

+0

@Cathirvtv当我运行'Debug.Print Range(“$ A $ 3:$ C $ 20”)。EntireColumn.Address'在立即窗口中,然后它打印'$ A:$ C'。确保输入的范围表达式的语法是正确的 – xmojmr 2014-11-02 08:40:17

相关问题