End of column text vba

ash59

New Member
Joined
Jul 29, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
I have an convoluted macro that takes 3 columns of info from one sheet, rearranges it on the same sheet, cuts it and then dumps into another sheet. I am not that confident with VBA so the second sheet has a straight copy from the first, including 3 columns down to row 671. The problem that I have is that the written data could be anything from 5 rows to 671. If it is less than 671 ctrl end goes to row 671 even though there is no written data in it. How do I set the spreadsheet, via vba, so that ctrl end goes to the end of the written data?
 
on the 3rd option, I am getting Method of "Range" of object "_global" failed notification

VBA Code:
Range("C1").Select
    Dim rng As Range
    Set rng = Range("C1:C" & lastRow)
    ActiveWorkbook.Save
and the same with this

VBA Code:
Sheets("Sheet1").Select
    Range("C1").Select
    Range("C" & lastRow).Select
    ActiveWorkbook.Save
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
on the 3rd option, I am getting Method of "Range" of object "_global" failed notification

VBA Code:
Range("C1").Select
    Dim rng As Range
    Set rng = Range("C1:C" & lastRow)
    ActiveWorkbook.Save
Where is your "lastRow" calculation?
Since you are calling "lastRow", you still need that calculation.

I was just showing you code snippets of what you can do with "lastRow" after you calculate it. So you still need the calculation in those cases, i.e.
VBA Code:
Dim lastRow As Long
Dim rng As Range
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Range("C1:C" & lastRow)
 
Upvote 0
Where is your "lastRow" calculation?
Since you are calling "lastRow", you still need that calculation.

I was just showing you code snippets of what you can do with "lastRow" after you calculate it. So you still need the calculation in those cases, i.e.
VBA Code:
Dim lastRow As Long
Dim rng As Range
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Range("C1:C" & lastRow)[/CODE
[/QUOTE]
 
Upvote 0
If (big if) I am reading this right, it should be set as

lastRow = Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Range("C1:C" & lastRow)

which should give me a highlighted row down to the last piece of data? It does not seem to work

I am getting to the point of just doing it the old fashion way, scroll down and ctrl up as I can't seem to get it working
 
Upvote 0
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Range("C1:C" & lastRow)

which should give me a highlighted row down to the last piece of data? It does not seem to work
Once again, these are just setting variables, it is not doing anything with them. It is up to you to determine what you want to do with it once you have it.

If you wanted to select the range, you could then use:
VBA Code:
rng.Select

If you wanted to cut or copy the range, you would use:
VBA Code:
rng.Cut
or
VBA Code:
rng.Copy

If you wanted to change the color (or other formatting) of the range, you could do something like:
VBA Code:
rng.Interior.Color = 65535

The methods I have showed you in the previous posts are just ways to build a dynamic range based on the data size.
Once you have that, you can do whatever you want with it.
 
Upvote 0
Ok, finally got my head around what you are saying and it does what you say it does but I refer back to my original question. The selection does not only select cells with data in them. It also selects all the rows below it that show as blanks. However, I think I know what the problem is. The values are being created by a formula that contains =if ("","") and the columns are being copied on from another spreadsheet and pasted using values. Would the paste carry over the "" and set the cell as formatted?
 
Upvote 0
The values are being created by a formula that contains =if ("","") and the columns are being copied on from another spreadsheet and pasted using values. Would the paste carry over the "" and set the cell as formatted?
Yes, I believe that is true.

If I recall correctly, I believe the "Find" method described in the link below may work on data created in such a way:

Give it a test, and see if that method works for you.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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
Back
Top