defing a range with a varying size...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I need to define a range on a sheet in column A, eg. A1:A15 (15 values)

However, the number of values varies depending upon what has been inputted. Is there a way to define the range up to the last cell with a value and discount anything after?

If you can point me in the right direction, that's be brilliant. Thanks
 

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).
Thanks for that Trouble, I'm trying to automatically remove blank cells from this range for which I am using:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

[FONT=arial][COLOR=#007F00]Dim rng As Range

'Store blank cells inside a variable
  On Error GoTo NoBlanksFound
    Set rng = Range("A1:A15").SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0

'Delete blank cells and shift upward
  rng.Rows.Delete Shift:=xlShiftUp

Exit Sub

'ERROR HANLDER
NoBlanksFound:
  MsgBox "No Blank cells were found"[/COLOR][/FONT]

End Sub
so how can I incorporate
Code:
[COLOR=#333333]For i = Range("A" & Rows.Count).End(xlUp).Row
into it?

Really appreciate your help mate :)[/COLOR]
 
Upvote 0
this is how i delete blanks

Code:
Option Explicit
 
Sub DeleteBlanks()
    Dim intCol As Integer
     
    For intCol = 1 To 1 'cols A to change for number of columns
        Range(Cells(2, intCol), Cells(353, intCol)). _
        SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    Next intCol
End Sub
 
Upvote 0
Hello again Trouble, I thought I had it in the bag, but I've just come across a problem...

This code works fine if you run it on the page in question (sheet7), however, I'm trying to run the macro from a different page and it comes up with an error: runtime "1004" application-defined or object-defined run-time error

I've tried:
Code:
Sub DeleteBlanks()
    Dim intCol As Integer
     
    For intCol = 1 To 2 'cols A to change for number of columns
        Worksheets("Sheet7").Range(Cells(1, intCol), Cells(353, intCol)). _
        SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    Next intCol
End Sub

but that gives me the same error...
 
Upvote 0
can you paste the exact code you are using place between [ code ] [ /code ] tags without the spacing and tell us where you placed the code
 
Upvote 0
ok you changed the original code to 1 to 2 columns this change means it will delete blanks from column 1 "A" and column 2 "B"

it will do this on the currently active sheet

you also changed to sheet 7 thisd change is not needed
 
Last edited:
Upvote 0
if you want seperate macros for each sheet we can do that but the macro i originally posted only work on currently active worksheet
 
Upvote 0
Is it not possible to change the code to run from a different sheet? The which is 'holding' the data, is a different sheet to where the macro is being called from... sorry
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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