macro change back to "thisworkbook" from "active workbook"

CsJHUN

Active Member
Joined
Jan 13, 2015
Messages
360
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. Mobile
Hi guys, i have this (snipet):
Code:
Private Sub readreqdb()
Sheets("allrequest").Activate
ActiveSheet.Unprotect Password:="asdqwe"
dblink = Sheets("settings").Range("AG11").Value
Workbooks.Open dblink, ReadOnly:=True, Password:="asdqwe"
Set reqdb = ActiveWorkbook
For i = 1 To Sheets.Count
    Sheets(i).Activate
    asn = ActiveSheet.Name
    lraas = Range("A" & Rows.Count).End(xlUp).Row
....
this should open a write protected shared workbook on lan (the dblink value in a cell) loop thru every sheet and copy the data on that sheet to macroworkbook (thisworkbook) "Allrequest" sheet, and... (the rest is not important i guess).
The problem is:
in the for loop the "Sheet(i).activate" select the sheet in thisworkbook and not the database.
Ideas?

Cheers
John
 

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.
Try
Code:
Set reqdb = Workbooks.Open(dblink, ReadOnly:=True, Password:="asdqwe")
For i = 1 To reqdb.Sheets.Count
    reqdb.Sheets(i).Activate
 
Upvote 0
You don't need to select worksheets to copy their contents.

But, if you qualify the sheet that you are activating, that should work.
Code:
For i = 1 To reqdb.Sheets.Count
    reqdb.Sheets(i).Activate
...
 
Upvote 0
Thx Fluff & Mike, i thought about that, and it's working ofcourse.
later there is a "asn = activesheet.name" which gives the "thisworkbook" sheet(i).name (I changed to reqdb.sheets(i).name, but still...)
I think this could casue problem later. Do you have any idea which should cause this?
 
Last edited:
Upvote 0
Are you sure it's giving the sheet name from ThisWorkbook, do you have sheets in both workbooks that have the same name?
this line
Code:
reqdb.Sheets(i).Activate
activates the sheet in the reqdb workbook, so that is the name you should be getting
 
Upvote 0
Are you sure it's giving the sheet name from ThisWorkbook, do you have sheets in both workbooks that have the same name?
this line
Code:
reqdb.Sheets(i).Activate
activates the sheet in the reqdb workbook, so that is the name you should be getting
Yep, i'm sure, because thisworkbook.sheet(1).name = "OwnRequest" and the reqdb.sheet(1).name = "Munka1" and the macro result for sheet(1).name is "ownreaquest" without the reqdb.
(the "Munka1" is the Sheet1 in Hungarian, so its the default, its contain all previous request, which must be read into "allrequest" sheet.
And yes again, activating with
Code:
reqdb.Sheets(i).Activate
then dim the Activesheet.Name to "asn" is working (that was before the addition of "reqdb.")
 
Upvote 0
Are you saying it's now working, or is there still a problem?
 
Upvote 0
works with "reqdb." added,

Thanks again.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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