Home

Looking for a way to track and reference cells in VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-852810%22%20slang%3D%22en-US%22%3ELooking%20for%20a%20way%20to%20track%20and%20reference%20cells%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-852810%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20Day%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20searching%20for%20a%20way%20to%20track%20cells%20so%20that%20I%20know%20where%20they%20have%20been%20moved%20to%20if%20a%20user%20editing%20the%20workbook%20adds%20columns%2Frows%2C%20cuts%20%2F%20pastes%20cells.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20found%20that%20I%20can%20use%20a%20Range%20variable%20in%20a%20module%20to%20keep%20track%20of%20movements%20of%20cells%20within%20a%20sheet%2C%20but%20if%20those%20cells%20are%20cut%20and%20pasted%20onto%20another%20sheet%20I%20find%20that%20the%20fact%20that%20it%20has%20moved%20sheets%20is%20lost.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EConsider%20the%20following%20VBA%20in%20a%20module%3A%3C%2FP%3E%3CTABLE%20border%3D%221%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3E%3CP%3EDim%20MyRange%20As%20Range%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPublic%20Sub%20GetReference()%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Set%20MyRange%20%3D%20Sheet1.Range(%22A1%22)%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPublic%20Sub%20PrintReference()%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Debug.Print%20MyRange.Parent.Name%20%26amp%3B%20%22-%26gt%3B%22%20%26amp%3B%20MyRange.Address%20%26amp%3B%20%22-%26gt%3B%22%3B%20MyRange.Value2%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20we%20put%20a%20string%20lets%20say%20%22testing%22%20in%20A1%20if%20sheet1%20then%3A%3C%2FP%3E%3COL%3E%3CLI%3ERun%20GetReference%3C%2FLI%3E%3CLI%3ERun%20PrintReference%3CBR%20%2F%3Eyou%20will%20get%20%22Sheet1-%26gt%3B%24A%241-%26gt%3BTesting%22%20printed%20in%20the%20immediate%20window%3C%2FLI%3E%3CLI%3ECut%20Sheet1!A1%20and%20paste%20it%20on%20Sheet3!B2%3C%2FLI%3E%3CLI%3ERun%20PrintReference%3CBR%20%2F%3Eyou%20will%20get%20%22Sheet1-%26gt%3B%24B%245-%26gt%3B%22%3C%2FLI%3E%3C%2FOL%3E%3CP%3EIt%20seems%20that%20the%20address%20tracking%20part%20still%20works%2C%20yet%20the%20Range.Parent%20does%20not%20get%20updated%20when%20the%20paste%20occurs%2C%20and%20the%20Value2%20parameter%20is%20somehow%20is%20reinitialized%20as%20another%20instance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20questions%20are%20how%20can%20I%20identify%3A%3C%2FP%3E%3COL%3E%3CLI%3EThat%20the%20user%20cut%20and%20pasted%20the%20reference%20to%20another%20sheet%3C%2FLI%3E%3CLI%3Ewhat%20that%20new%20sheet%20is%3C%2FLI%3E%3C%2FOL%3E%3CP%3EAny%20other%20points%20would%20be%20helpful%20in%20understanding%20this%20automation%20challenge.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-852810%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-853121%22%20slang%3D%22en-US%22%3ERe%3A%20Looking%20for%20a%20way%20to%20track%20and%20reference%20cells%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853121%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F408029%22%20target%3D%22_blank%22%3E%40ScottGall%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20point%20is%20you%20set%20the%20variable%20MyRange%20in%20the%20code%20GetReference.%3C%2FP%3E%3CP%3EAnd%20in%20the%20code%20PrintReference%2C%20you%20are%20printing%20it's%20properties%20only%20without%20setting%20it%20again%20and%20since%20you%20declared%20the%20MyRange%20variable%20is%20the%20module%20level%20variable%2C%20it%20is%20still%20have%20a%20reference%20set%20in%20GetReference%20code.%20And%20you%20are%20not%20getting%20it's%20value%20in%20the%20Immediate%20Window%20because%20you%20cut%20that%20cell%20and%20pasted%20at%20another%20location%20and%20the%20range%20MyRange%20which%20was%20previously%20set%20to%20refer%20to%20the%20Sheet1!A1%20is%20still%20referring%20that%20cell%20which%20is%20now%20empty%20and%20therefore%20you%20are%20not%20getting%20its'%20value.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20have%20one%20such%20case%20where%20you%20want%20to%20track%20a%20cell%20and%20all%20it's%20properties%20even%20if%20it%20is%20cut%20and%20paste%20into%20another%20location%2C%20you%20may%20do%20something%20like%20this...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3ESelect%20the%20cell%20A1%20on%20Sheet1.%3C%2FLI%3E%3CLI%3EType%20MyRange%20in%20the%20Name%20Box%20to%20create%20a%20Named%20Range%20which%20refers%20to%20the%20cell%20A1%20on%20Sheet1.%3C%2FLI%3E%3CLI%3EEnter%20any%20value%20in%20A1%20on%20Sheet1%2C%20say%20'Testing'.%3C%2FLI%3E%3CLI%3ENow%20run%20the%20code%20PrintReference%20given%20below.%3C%2FLI%3E%3CLI%3ENow%20cut%20the%20cell%20and%20paste%20it%20into%20any%20available%20sheet%20in%20the%20file.%3C%2FLI%3E%3CLI%3ERun%20the%20code%20PrintReference%20again%20and%20you%20will%20get%20it's%20latest%20properties.%3C%2FLI%3E%3C%2FOL%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EDim%20MyRange%20As%20Range%0APublic%20Sub%20PrintReference()%0A%20%20%20%20Set%20MyRange%20%3D%20Range(%22MyRange%22)%0A%20%20%20%20Debug.Print%20MyRange.Parent.Name%20%26amp%3B%20%22-%26gt%3B%22%20%26amp%3B%20MyRange.Address%20%26amp%3B%20%22-%26gt%3B%22%3B%20MyRange.Value2%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20573px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131564i21C93DD56DA73AA2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22NamedRangeA1.jpg%22%20title%3D%22NamedRangeA1.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-853472%22%20slang%3D%22en-US%22%3ERe%3A%20Looking%20for%20a%20way%20to%20track%20and%20reference%20cells%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853472%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20response.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20named%20range%20approach%20you%20suggest%20could%20work%20for%20needing%20to%20track%20small%20numbers%20of%20cells%20%26lt%3B30K%2C%20the%20reason%20I%20was%20interested%20in%20using%20a%20reference%20in%20VBA%20was%20to%20make%20the%20tracking%20lighter%20(ultimately%20I'll%20be%20using%20C%23)%20however%20the%20object%20model%20is%20shared%20so%20I%20hoped%20to%20learn%20something%20here.%20Using%20named%20ranges%20to%20track%20the%20number%20of%20cells%20(~600K)%20I%20need%20would%20have%20performance%20impacts%20on%20the%20workbooks%2C%20not%20to%20mention%20with%20Named%20Ranges%20you%20will%20run%20into%20memory%20problems%20after%20about%2064K.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20struck%20me%20with%20the%20VBA%20approach%20was%20that%20as%20long%20as%20I%20am%20cutting%20and%20pasting%20on%20the%20same%20sheet%20it%20works%20as%20I%20expect%3A%3C%2FP%3E%3CP%3EMyRange.Parent.Name%20%3D%20%22Sheet1%22%3C%2FP%3E%3CP%3EMyRange.Address%20%3D%20%22%24B%245%22%20(or%20whatever%20address%20you%20paste%20to)%3C%2FP%3E%3CP%3EMyRange.Value2%20%3D%20%22Testing%22%20(or%20whatever%20you%20type%20in%20the%20cell%20before%20or%20after%20moving%20it)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehowever%20if%20I%20paste%20on%20another%20sheet%20I%20get%3A%3C%2FP%3E%3CP%3EMyRange.Parent.Name%20%3D%20%22Sheet1%22%20(no%20mater%20what%20sheet%20you%20pasted%20on%3C%2FP%3E%3CP%3EMyRange.Address%20%3D%20%22%24B%245%22%20(or%20whatever%20address%20you%20paste%20to)%3C%2FP%3E%3CP%3EMyRange.Value2%20%3D%20Empty%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt's%20almost%20like%20the%20object%20is%20reinitialized%20then%20some%20properties%20are%20cloned%20from%20the%20previous%20object.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEven%20if%20I%20could%20identify%20the%20ranges%20where%20this%20occurred%20in%20a%20trustworthy%20way%20I%20could%20deal%20with%20those%20cases%2C%20the%20problem%20I%20have%20is%20that%20the%20MyRange%20gives%20no%20indication%20of%20failure%2C%20just%20ambiguous%20wrong%20information.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20additional%20thoughts%20would%20be%20most%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E
ScottGall
New Contributor

Good Day,

 

I am searching for a way to track cells so that I know where they have been moved to if a user editing the workbook adds columns/rows, cuts / pastes cells.

 

I have found that I can use a Range variable in a module to keep track of movements of cells within a sheet, but if those cells are cut and pasted onto another sheet I find that the fact that it has moved sheets is lost.

 

Consider the following VBA in a module:

Dim MyRange As Range

 

Public Sub GetReference()
    Set MyRange = Sheet1.Range("A1")
End Sub

 

Public Sub PrintReference()
    Debug.Print MyRange.Parent.Name & "->" & MyRange.Address & "->"; MyRange.Value2
End Sub

 

If we put a string lets say "testing" in A1 if sheet1 then:

  1. Run GetReference
  2. Run PrintReference
    you will get "Sheet1->$A$1->Testing" printed in the immediate window
  3. Cut Sheet1!A1 and paste it on Sheet3!B2
  4. Run PrintReference
    you will get "Sheet1->$B$5->"

It seems that the address tracking part still works, yet the Range.Parent does not get updated when the paste occurs, and the Value2 parameter is somehow is reinitialized as another instance.

 

My questions are how can I identify:

  1. That the user cut and pasted the reference to another sheet
  2. what that new sheet is

Any other points would be helpful in understanding this automation challenge.

 

Thank you.

2 Replies

@ScottGall 

The point is you set the variable MyRange in the code GetReference.

And in the code PrintReference, you are printing it's properties only without setting it again and since you declared the MyRange variable is the module level variable, it is still have a reference set in GetReference code. And you are not getting it's value in the Immediate Window because you cut that cell and pasted at another location and the range MyRange which was previously set to refer to the Sheet1!A1 is still referring that cell which is now empty and therefore you are not getting its' value.

 

If you have one such case where you want to track a cell and all it's properties even if it is cut and paste into another location, you may do something like this...

 

  1. Select the cell A1 on Sheet1.
  2. Type MyRange in the Name Box to create a Named Range which refers to the cell A1 on Sheet1.
  3. Enter any value in A1 on Sheet1, say 'Testing'.
  4. Now run the code PrintReference given below.
  5. Now cut the cell and paste it into any available sheet in the file.
  6. Run the code PrintReference again and you will get it's latest properties.
Dim MyRange As Range
Public Sub PrintReference()
    Set MyRange = Range("MyRange")
    Debug.Print MyRange.Parent.Name & "->" & MyRange.Address & "->"; MyRange.Value2
End Sub

 

NamedRangeA1.jpg

@Subodh_Tiwari_sktneer 

 

Thank you for your response.

 

The named range approach you suggest could work for needing to track small numbers of cells <30K, the reason I was interested in using a reference in VBA was to make the tracking lighter (ultimately I'll be using C#) however the object model is shared so I hoped to learn something here. Using named ranges to track the number of cells (~600K) I need would have performance impacts on the workbooks, not to mention with Named Ranges you will run into memory problems after about 64K.

 

What struck me with the VBA approach was that as long as I am cutting and pasting on the same sheet it works as I expect:

MyRange.Parent.Name = "Sheet1"

MyRange.Address = "$B$5" (or whatever address you paste to)

MyRange.Value2 = "Testing" (or whatever you type in the cell before or after moving it)

 

however if I paste on another sheet I get:

MyRange.Parent.Name = "Sheet1" (no mater what sheet you pasted on

MyRange.Address = "$B$5" (or whatever address you paste to)

MyRange.Value2 = Empty

 

It's almost like the object is reinitialized then some properties are cloned from the previous object.

 

Even if I could identify the ranges where this occurred in a trustworthy way I could deal with those cases, the problem I have is that the MyRange gives no indication of failure, just ambiguous wrong information.

 

Any additional thoughts would be most appreciated.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies