2

In my workbook I frequently need to activate some sheets by using their CodeName then search for some texts within that sheet and use the row or column number of the cell that contains the text I'm looking for.

In that situations, I am using below kind of codes:

Sheet16.Select '(Using codename)
Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate

   FirstRow= ActiveCell.Row

    Cells.Find(What:="SECOND TEXT I'M LOOKING FOR", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate

SecondRow = ActiveCell.Row

Rows(FirstRow & ":" & SecondRow + 1).EntireRow.Hidden = False

Eveything works well, but nowadays I am trying to improve my codes and I'd like to run my codes faster.

Now,

1- How can I reference my WorkSheets' CodeName easily?

(I'm looking for answer like ThisWorkbook.Worksheets("Sheet1") - not a function

Dim wb as Workbook, ws as Worksheet
set wb = ThisWorkbook
set ws = wb.CodeName(Sheet16) or wb.Sheet16 or sheet16
'then 
ws.Cells.Find(What ..........   rest of the code  ...... )

none of them working for CodeName property. Fully reference a worksheet by codename or Referencing sheets in another workbook by codename using VBA didn't answer my question.

2- How can I avoid using .Activate to use the result cell of Cells.Find() formula.

Again in that example I firstly search for specific text, which is :="FIRST TEXT I'M LOOKING FOR" in the first part of my code, and then I need to use that cell to get it's row number or use offset or anything , and because of that I feel myself obligated to use .Activate because,

FirstRow =  Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row

kind of codes do not work as well. How to avoid using Select in Excel VBA macros here in that answer there are couple of suggestions but none of them helps me in this case. I tried to get an answer from the owner of this answer to avoid any duplicate question but he suggested me to ask a new question. (And as long as both of my questions belong to my example code and I'll connect them, I asked them together in one question.)

5
  • 1. Why not a function? They're works well. Another option here: You can add a dictionary as member of ThisWorkbook and populate it with (ws.CodeName, ws) pair on events such as Open, NewSheet and at cetera. And after that you can pull your ws from that dict just with Set ws =ThisWorkbook.CodeNameWss("Codename"). 2.Best option to avoid an Activate - just not using it. Don't feel obligated to use it, if you need anything except "activating" the cell. May 24, 2017 at 7:58
  • @sktneer Thank you. This sentence is from SO help center to explain us When should we vote down? ==> Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. I am trying to solve this issue for a very long time and as I mentioned within the text, I really spent a lot of effort searched for many sources and I think it's not that egregiously sloppy and I wonder where is not clear enough..
    – Mertinc
    May 24, 2017 at 8:07
  • @CommonSense thanks for your interest. Because I use this kind of codes a lot and I don't want to run another function each time. But the question is answered now, the only thing I was missing is, CodeNames are only working in ThisWorkbook so when I was trying to define it one more time, it was occurring an error.
    – Mertinc
    May 24, 2017 at 8:31
  • @Mertinc, CodeName is a string property and you trying to use a hard-coded variable (in scope of thisworkbook), If your codenames are changed over time (and you doesnt handle it) - your code will fail, but with function approach there're less pain if it happen. Your question is misleading, because you never really use a codename. It's better to run a function each time than use a hard-coded stuff. But if you sufficent with that - who am I to blame you (: May 24, 2017 at 8:44
  • Oh no mate cheers thanks for your interest :) But I think you misunderstood me, my CodeNames will never change unless I want it. (I'm not talking about Sheet Names , which user can easily change. Actually the reason I'm using CodeNames is to avoid this problem.) And I really do use a codename :)
    – Mertinc
    May 24, 2017 at 8:47

1 Answer 1

2

While setting a sheet variable to the codename, the limitation is you can use the codename in ThisWorkbook only i.e. the workbook which contains the code.

Consider this code...

Dim ws As Worksheet
Set ws = wsData 'where wsData is the CodeName of a sheet.

Now in your code you can manipulate or perform actions on ws sheet without activating or selecting it. Actually in case of CodeNames, you don't need to declare a sheet variable, you can directly refer to the sheet by using it's codename irrespective of which sheet is currently active.

like...

wsData.Cells.Clear
Set Rng = wsData.Range("A1").CurrentRegion

e.g. with your another example code

Dim ws As Worksheet
Set ws = wsData 'where wsData is the CodeName of a sheet.

FirstRow = ws.Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row

'Or just (without declaring the ws sheet variable where wsData is the sheet code name)
FirstRow = wsData.Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
2
  • Oh, so the only thing I was missing: the limitation is you can use the codename in ThisWorkbook only information in this case. Anyway, that's solved now. But can you please underline that wsData is not a variable but really just Worksheet's CodeName. (I misunderstood it at first) Thanks @sktneer
    – Mertinc
    May 24, 2017 at 8:29
  • @Mertinc You're welcome. Glad you found it useful. I have added the comment in the solution to make is clear that wsData is the sheet code name. May 24, 2017 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.