Excel range issue

tmccar

New Member
Joined
Apr 7, 2016
Messages
22
Set xlbook = GetObject("C:\07509\LB_RACKTMC.xlsx")
Set xlSheet = xlbook.Sheets("LB RACK")
Set src = xlSheet.Range("f1..f3").CurrentRegion.SpecialCells(xlCellTypeVisible)
Set sh = xlbook.Sheets.Add

src.Copy sh.Range("a1")
m = sh.Range("a1").CurrentRegion

For ro = LBound(m) + 1 To UBound(m)

Debug.Print LBound(m), UBound(m)


I want to copy a range "f1 to f3" from sheet "LB RACK" to a new sheet ("Sheet1")
For some reason, the Ubound(m) value in this VBA code is showing as 1245. Should this not be 3, as the range I am copying is ("f1..f3")?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
If all you want to copy is F1:F3 why are you using currentregion (and specialcells visible for that matter)?
 
Last edited:
Upvote 0
Do you understand what CurrentRegion and SpecialCells(xlCellTypeVisible) do?
 
Upvote 0
Just in case you aren't clear on what they do, open a new workbook and copy the below into Sheet1

Excel Workbook
ABCDEFGHIJK
1aaabbbaaaaaa
2aaaaaaaaa
3aaaaaaaaa
4bbb
5bbb
6
Sheet1


The CurrentRegion is from your starting range until the last cell contiguous to the range, basically the region is a range bounded by blank rows and blank columns.

with the layout above run the code below and hopefully you will see what it does.

Rich (BB code):
Sub test()
    Dim X As Range, Y As Range, Z As Range
    
    Set X = Range("A1:A3").CurrentRegion
    X.Select
    MsgBox X.Address

    Set Y = Range("F1:F3").CurrentRegion
    Y.Select
    MsgBox Y.Address

    Set Z = Range("J1:J3").CurrentRegion
    Z.Select
    MsgBox Z.Address

End Sub

I will try and demonstrate specialcells visible in the next post to save messing up the screenshot
 
Upvote 0
Ok, SpecialCells(xlCellTypeVisible)...

Basically it does what it says on the tin. It references only the cells you can see. So for instance it doesn't reference any rows or columns hidden either manually or by code including any hidden by using a filter.

As a basic demonstration copy the screenshot below to Sheet2

Excel Workbook
ABCDEF
11111
22222
33333
4
5
6
Sheet2


then run the code below

Rich (BB code):
Sub VISCHECK1()
Sheets("Sheet3").Cells.Delete
Sheets("Sheet2").Rows(2).Hidden = False
Sheets("Sheet2").Rows(2).Hidden = True
Sheets("Sheet2").Range("A1:D3").Copy Sheets("Sheet3").Range("A1")
End Sub

now look at Sheet3 (in particular row 2).
Now run the code below and then look at Sheet3.

Rich (BB code):
Sub VISCHECK2()
Sheets("Sheet3").Cells.Delete
Sheets("Sheet2").Rows(2).Hidden = False
Sheets("Sheet2").Rows(2).Hidden = True
Sheets("Sheet2").Range("A1:D3").SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet3").Range("A1")
End Sub
 
Upvote 0
Ok, SpecialCells(xlCellTypeVisible)...

Basically it does what it says on the tin. It references only the cells you can see. So for instance it doesn't reference any rows or columns hidden either manually or by code including any hidden by using a filter.

As a basic demonstration copy the screenshot below to Sheet2

Sheet2

ABCDEF
11111
22222
33333
4
5
6

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

then run the code below

Rich (BB code):
Sub VISCHECK1()
Sheets("Sheet3").Cells.Delete
Sheets("Sheet2").Rows(2).Hidden = False
Sheets("Sheet2").Rows(2).Hidden = True
Sheets("Sheet2").Range("A1:D3").Copy Sheets("Sheet3").Range("A1")
End Sub

now look at Sheet3 (in particular row 2).
Now run the code below and then look at Sheet3.

Rich (BB code):
Sub VISCHECK2()
Sheets("Sheet3").Cells.Delete
Sheets("Sheet2").Rows(2).Hidden = False
Sheets("Sheet2").Rows(2).Hidden = True
Sheets("Sheet2").Range("A1:D3").SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet3").Range("A1")
End Sub


Mark - Thanks for your helpful explanations
Tom
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top