Trying to find last column with spaces in between data

ian0886

New Member
Joined
Dec 10, 2016
Messages
42
Hi all,

I'm fairly new to VBA and i'd need some help. I'm trying look for a code that finds the last blank cell after my data. Problem is within the data, there's are spaces in between. so what i'm trying to find is column J 1st row. Can anyone please help me?

ABCDEFGHIJ
1
21st leg 2nd leg1st leg 2nd leg1st leg 2nd leg
312/6/201613/6/201613/6/201614/6/201614/6/201615/6/2016
412/6/201613/6/201613/6/201614/6/201614/6/201615/6/2016
512/6/201613/6/201613/6/201614/6/201614/6/201615/6/2016
612/6/201613/6/201613/6/201614/6/201614/6/201615/6/2016

<colgroup><col><col><col span="9"></colgroup><tbody>
</tbody>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,825
Office Version
  1. 365
Platform
  1. Windows
Can you clarify... why is it row 1? Row 1 is before your data, isn't it?

Also, why is the last blank cell in column J? Why not K? Or I?
 
Upvote 0

ian0886

New Member
Joined
Dec 10, 2016
Messages
42
Hi ruddles,
Yes you're right! It should be row 2 instead of row 1.

Secondly, basically Thia is an archive file. I'm trying to copy the dates, from another worksheet to this file. And becos it's for each day, there's a GAP in between each data set
 
Upvote 0

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,832
Here are two VBA lines of code.

For the number of the very last used column in row 2:
Code:
Row2LastCol = Cells(2, Columns.Count).End(xlToLeft).Column

For the number of the very last used column in the entire sheet:
Code:
SheetLastCol = Columns(ActiveSheet.UsedRange.Column + ActiveSheet.UsedRange.Columns.Count - 1).Column
 
Upvote 0

ian0886

New Member
Joined
Dec 10, 2016
Messages
42
ADVERTISEMENT
Hi Tetra,

The below is working but I've encounter a rather weird problem. The data spans some time ago and runs till somewhere column UA. When i run the macro once, somehow it got saved to column Z and AA. On running the second time, it seems to copy when i wanted it to be.

Row2LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
 
Upvote 0

ian0886

New Member
Joined
Dec 10, 2016
Messages
42
Hi,

Below is what i've done, but something pretty funny happened. When i run the macro on the 'date archive' tab it correct paste the data at the very last column. Somehow or rather when i just ran it on the 'working sheet' tab. it got paste in between the previous data set. is there something i can do?

I've even tried to add activate in each line so that its identifying correctly. It didn't work.

Code:
Dim Row2LastCol As LongRow2LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Worksheets("Working sheet").Range("a:c").EntireColumn.Copy
Worksheets("Date Archive").Cells(1, Row2LastCol + 2).PasteSpecial xlPasteAll
Worksheets("Working sheet").Range("c1:c100").Copy
Worksheets("Date Archive").Cells(1, Row2LastCol + 4).PasteSpecial xlPasteValues
 
Upvote 0

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,825
Office Version
  1. 365
Platform
  1. Windows
I think rather than:-

Cells(1, Columns.Count).End(xlToLeft).Column

you need to code

Worksheets("Date Archive").Cells(1, Worksheets("Date Archive").Columns.Count).End(xlToLeft).Column

otherwise VBA doesn't know which sheet you're trying to find the last column of.
 
Upvote 0

Forum statistics

Threads
1,195,648
Messages
6,010,911
Members
441,572
Latest member
keobongda8812

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