Range(Cells(xxx,yyy)) not working when I reference another Workbook?

dm321321

New Member
Joined
Mar 8, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I isolated my issue to the following piece of code



VBA Code:
Dim MetOceanWorkbook As Workbook
Dim MetOceanSheet As Worksheet
Dim RowIni, InputNcol as Variant

Application.ScreenUpdating = False
Set TWorkbook = Workbooks.Open(Path & "\" & "T.xlsx")
Set TSheet = TWorkbook.Worksheets.Item(2)
    
For jj = InputNcol(0) + ii * 4 To InputNcol(2) + ii * 4
        
     RowIni = TSheet.Cells.Range(Cells(1, jj), Cells(Rows.Count, jj)).Find(What:="*", _
     After:=Cells(1, jj), LookAt:=xlWhole, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
        
Next

It basically opens another workbook and gives the first non-empty cell from column jj

In short, this part of the code:
- Does not work as it is.
- Works when no external workbook is involved (It looks in the same worksheet than the macro)
- Works if I replace Range(Cells(1, jj), Cells(Rows.Count, jj)) and After:=Cells(1, jj) for Range("E:E") and Range("E1"), respectively.

This last option is less than ideal since I need to loop through different columns in an specific order.

Anyone knows what might be the issue?

Regards,
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You need to specify the same sheet for the Cells properties as for the Range:

Code:
TSheet.Range(TSheet.Cells(1, jj), TSheet.Cells(TSheet.Rows.Count, jj))
 
Upvote 0
Try
You need to specify the same sheet for the Cells properties as for the Range:

Code:
TSheet.Range(TSheet.Cells(1, jj), TSheet.Cells(TSheet.Rows.Count, jj))
I was just about to post that same message. You missed qualifying the Cells call for the "After:=" argument also. Also, I do not believe the first qualification on the Range is needed.
VBA Code:
RowIni = Range(TSheet.Cells(1, jj), TSheet.Cells(Rows.Count, jj)).Find(What:="*", After:=TSheet.Cells(1, jj), _
     LookAt:=xlWhole, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
 
Upvote 0
It depends where the code is and I can't see any benefit in omitting it?
You are indicating something I might not be aware of. I always thought the qualifier in front of the main Range that contains two internal range references was not needed but your wording suggests there are locations when it is needed. Can you tell me when these times are or give me a reference that describes it? Thanks.
 
Upvote 0
Hi,​
Rick, yes it is not needed except when the VBA procedure is located in a worksheet module which is not the same as the range​
if I well remember, to be tested and reverted …​
 
Upvote 0
Correct. In a worksheet module, Range is really Me.Range unless otherwise specified, and that will fail if the Cells references are on a different sheet.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,231
Members
448,951
Latest member
jennlynn

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