简体   繁体   English

无法在Excel VBA中选择范围

[英]Not Able to Select a Range in Excel VBA

I have written a macro in a sheet "A" , 我已经在工作表“ A”中写了一个宏,

on change in sheet A , i want to copy a range from sheet "C" and paste in sheet "D" 在工作表A更改时,我要从工作表“ C”复制一个范围并将其粘贴到工作表“ D”

This flow is fine ,and I am able to acheive it . 这个流程很好,我能够实现。 The problem is when I am trying to select the range in sheet B. 问题是当我尝试在工作表B中选择范围时。

rownum= 83 (I get it dynamically)

colnum = 31 (Number of columns in rownum)

Now I want to copy the 83rd Row till 31st column. 现在,我要将第83行复制到第31列。

Problem Begins : 问题开始:

 Sheets("B").Range("A83:AO83").Select   ~ works like Charm 

As the above is not dynamic ,I want to use 由于上面不是动态的,我想用

Sheets("B").Range(Cells(rownum,1),Cells(rownum,colnum)).Select  ~ Object Defined Error

Any Ideas !!! 有任何想法吗 !!! I am stuck like for 2 hrs.. its not happening . 我被卡了2个小时..没有发生。

This is some part of code , to cause less confusion , I am not posting all the code , The code stops at the last line !! 这是代码的一部分,为了减少混乱,我并没有发布所有代码,代码在最后一行停止! OBJECT DEFINED ERROR . 对象定义的错误。

Dim rownum As Integer
   Dim colnum As Integer

 rownum = Application.Match(searchFE, Sheets("Attendance").Range("D:D"), 0)
 colnum = Cells(rownum, Columns.Count).End(xlToLeft).Column
Sheets("Attendance").Range(Cells(rownum, 1), Cells(rownum, colnum)).Select

both rownum and colnum are getting values . rownum和colnum都在获取值。

Let's have a look at 让我们来看看

Sheets("Attendance").Range(Cells(rownum, 1), Cells(rownum, colnum)).Select

The Cells(rownum, 1) is not fully qualified. Cells(rownum, 1)不完全合格。 It is not clearly given from which Sheet the Cells should be taken. 没有明确给出应Sheet获取Cells So ActiveSheet.Cells(rownum, 1) is assumed. 因此假设使用ActiveSheet.Cells(rownum, 1) Thats why the above command will fail if Sheets("Attendance") is not the active sheet. 因此,如果Sheets("Attendance")不是活动工作表,则上述命令将失败。

...
With Sheets("Attendance")
 .Range(.Cells(rownum, 1), .Cells(rownum, colnum)).Select
End With
...

In my opinion it is just a matter of .address . 我认为这只是.address When you are selecting your range, you need to give the address as input, so basically your only error was to write: 选择范围时,需要提供地址作为输入,因此基本上,您唯一的错误是写:

Sheets("B").Range(Cells(rownum,1),Cells(rownum,colnum)).Select

Instead of: 代替:

Sheets("B").Range(Cells(rownum,1).Address(0,0),Cells(rownum,colnum).Address(0,0)).Select

I like to write .address(0,0) because it reminds me that it is the absolute address (starting from row and col = 0) 我喜欢写.address(0,0)因为它提醒我它是绝对地址(从row和col = 0开始)

I'm also not sure about: 我也不确定:

Sheets("Attendance").Range(Cells(rownum, 1), Cells(rownum, colnum)).Select

Normally I'd write: 通常我会写:

Worksheets("Attendance").Range(Cells(rownum, 1).address(0,0), Cells(rownum, colnum).address(0,0)).Select

And be careful to refer to a specific Workbook if you are working with more than one! 如果要使用多个Workbook ,请小心参考特定的Workbook

If you want to get to that last row then use this code 如果要转到最后一行,请使用此代码

    Sub GetThere()

    Dim sh As Worksheet
    Dim r As Long
    Dim c As Long
    Dim rng As Range

    Set sh = Sheets("Attendance")

    With sh
        r = .Cells(.Rows.Count, "D").End(xlUp).Row
        c = .Cells(r, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(r, 1), .Cells(r, c))
    End With

    Application.Goto Reference:=rng

End Sub

If you actually want to do something with the range such as copy it to a different location, then use this code. 如果您实际上想对范围进行操作(例如将其复制到其他位置),请使用此代码。

Sub GetThere2()

    Dim sh As Worksheet
    Dim r As Long
    Dim c As Long
    Dim rng As Range

    Set sh = Sheets("Attendance")

    With sh
        r = .Cells(.Rows.Count, "D").End(xlUp).Row
        c = .Cells(r, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(r, 1), .Cells(r, c))
    End With

    rng.Copy Sheets("Sheet2").Range("A1")

End Sub

If you really want to select the range, then you have to make sure the sheet is selected as well. 如果您确实要选择范围,则必须确保也选择了工作表。

Sub GetThere3()

    Dim sh As Worksheet
    Dim r As Long
    Dim c As Long
    Dim rng As Range

    Set sh = Sheets("Attendance")

    With sh
        r = .Cells(.Rows.Count, "D").End(xlUp).Row
        c = .Cells(r, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(r, 1), .Cells(r, c))
        .Select
    End With

    rng.Select

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM