SOLVED

Get Macro to select Last Row entry rather than a fixed one

%3CLINGO-SUB%20id%3D%22lingo-sub-1501973%22%20slang%3D%22en-US%22%3EGet%20Macro%20to%20select%20Last%20Row%20entry%20rather%20than%20a%20fixed%20one%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1501973%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20There.%3C%2FP%3E%3CP%3EI%20made%20a%20macro%20which%20polishes%20some%20data%20I%20regularly%20import%20from%20the%20Web.%20However%2C%20its%20is%20hard-coded%20to%20select%20rows%20till%20894.%20I%20wish%20to%20make%20it%20flexible%2C%20such%20that%20any%20entry%20which%20is%20the%20last%20in%20that%20data%20gets%20selected%20and%20all%20the%20data%20gets%20converted%20to%20a%20table.%20I%20have%20attached%20a%20picture%20of%20the%20part%20of%20Macro%20where%20894%20figure%20comes%20up.%20I%20am%20not%20able%20to%20make%20it%20flexible%20though.%26nbsp%3B%3C%2FP%3E%3CP%3EKindly%20help.%3C%2FP%3E%3CP%3EThanks.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1501973%22%20slang%3D%22en-US%22%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-1502094%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20Macro%20to%20select%20Last%20Row%20entry%20rather%20than%20a%20fixed%20one%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1502094%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F545848%22%20target%3D%22_blank%22%3E%40Jodhvir%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt's%20hard%20to%20tell%20unless%20we%20see%20what's%20the%20layout%20of%20your%20data.%20Is%20there%20any%20blank%20row%20or%20column%20in%20between%20the%20data%20set%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESelect%20the%20cell%20A1%20and%20press%20%3CSTRONG%3ECtrl%2B*%3C%2FSTRONG%3E%20and%20if%20it%20selects%20all%20the%20desired%20data%20then%20you%20can%20use%20CurrentRegion%20Property%20like%20this%20instead%20of%20hard%20coding%20the%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ERange(%22A1%22).CurrentRegion%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1502184%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20Macro%20to%20select%20Last%20Row%20entry%20rather%20than%20a%20fixed%20one%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1502184%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F545848%22%20target%3D%22_blank%22%3E%40Jodhvir%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20identifying%20the%20last%20row%20by%20adding%20the%20following%20code%20(the%20example%20uses%20column%20A%20for%20finding%20last%20row%20but%20use%20a%20column%20which%20you%20know%20will%20always%20be%20populated%20in%20your%20data)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20Lrow%20As%20Long%3C%2FP%3E%3CP%3EWith%20ActiveSheet%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20Lrow%20%3D%20.Cells(.Rows.Count%2C%20%22A%22).End(xlUp).Row%3C%2FP%3E%3CP%3EEnd%20With%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20can%20address%20the%20range%20dynamically%20using%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERange(%22%24A%241%3A%24G%24%22%20%26amp%3B%20Lrow)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

Hi There.

I made a macro which polishes some data I regularly import from the Web. However, its is hard-coded to select rows till 894. I wish to make it flexible, such that any entry which is the last in that data gets selected and all the data gets converted to a table. I have attached a picture of the part of Macro where 894 figure comes up. I am not able to make it flexible though. 

Kindly help.

Thanks. 

4 Replies

@Jodhvir 

It's hard to tell unless we see what's the layout of your data. Is there any blank row or column in between the data set?

 

Select the cell A1 and press Ctrl+* and if it selects all the desired data then you can use CurrentRegion Property like this instead of hard coding the range.

 

Range("A1").CurrentRegion

Best Response confirmed by Jodhvir (Contributor)
Solution

@Jodhvir 

 

Try identifying the last row by adding the following code (the example uses column A for finding last row but use a column which you know will always be populated in your data):

 

Dim Lrow As Long

With ActiveSheet

    Lrow = .Cells(.Rows.Count, "A").End(xlUp).Row

End With

 

Then you can address the range dynamically using:

 

Range("$A$1:$G$" & Lrow)

 

@Charla74 

Thnku so much. This worked like a charm.

Hello Sir. The data is in a table and no empty rows are there in the columns i am selecting. Anyways I tried the method suggested by someone else here and it worked. Thank you for the response.