简体   繁体   中英

Not Able to Select a Range in Excel VBA

I have written a macro in a sheet "A" ,

on change in sheet A , i want to copy a range from sheet "C" and paste in sheet "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.

rownum= 83 (I get it dynamically)

colnum = 31 (Number of columns in rownum)

Now I want to copy the 83rd Row till 31st column.

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 .

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 .

Let's have a look at

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

The Cells(rownum, 1) is not fully qualified. It is not clearly given from which Sheet the Cells should be taken. So ActiveSheet.Cells(rownum, 1) is assumed. Thats why the above command will fail if Sheets("Attendance") is not the active sheet.

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

In my opinion it is just a matter of .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)

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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