Using LastRow properly, or setting range first

hg1027

Board Regular
Joined
Apr 28, 2008
Messages
56
I've been working on a macro to convert estimates to a usable form, and so far I've been using LastRow for Autofill functions. For example:

Code:
    'Makes column A a compilation of K, L, M
    Range("A2").Value = "=RC[10]&"" ""&RC[11]&"" ""&RC[12]"
    Range("A2").Select
    LastRow = Range("B" & Rows.Count).End(xlUp).Row - 1
    Selection.AutoFill Destination:=Range("A2").Resize(LastRow), Type:=xlFillDefault

That works fine, but I'm having trouble translating that to work with a text to columns bit (just before the above):

Code:
'Text to Columns on column A to remove excess and prettify
    Range("A2:A5000").Select
    Selection.TextToColumns Destination:=Range("J2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
       :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

I tried
Code:
    Range("A2").Select
    LastRow = Range("B" & Rows.Count).End(xlUp).Row - 1
but that just does the first row (2) and not the rest of the range.

I know the way I have it now (I should never get a file with over 500 lines) will work, but I also know it's not the best way. Is there a way to use LastRow with my text to columns?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Does this work?

Code:
Range("B" & Rows.Count).End(xlUp).Row.TextToColumns Destination:=Range("J2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
       :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
 
Upvote 0

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Without knowing exactly what you're doing, I don't know if this would be preferable, but I usually use the following for writing formulae/text to a range of cells...

Code:
LastRow = Range("B" & Rows.Count).End(xlUp).Row - 1
Range("A2:A" & LastRow).FormulaR1C1 = "=RC[10]&"" ""&RC[11]&"" ""&RC[12]"

For the text to columns, I would use:

Code:
Range("A2:A" & Last_Row).TextToColumns Destination:=Range("J2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
       :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
 
Upvote 0

hg1027

Board Regular
Joined
Apr 28, 2008
Messages
56
njimack - your text to columns section doesn't work, throws a debug. The autofill part does though.

vogii - yours gives "invalid qualifier" at

Rich (BB code):
Range("B" & Rows.Count).End(xlUp).Row.

I guess text to columns uses a different method of identifying the range that autofill? I'll have a look at a with statement Peter_SS sent me yesterday.
 
Upvote 0

Forum statistics

Threads
1,191,717
Messages
5,988,257
Members
440,146
Latest member
rgomes8

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
Top