Add a 'go to next cell down' line in macro

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi guys,
I have a simple macro:

Sub CellValue_Add_1()
ActiveCell.Value = ActiveCell.Value + 1
End Sub

I would like it to automatically move down to the next visible line (its autofiltered, so it can't go to the next hidden line, incase one is there).

Alternatively, I would love my macro to add 1 to all selected visible cells at once - if that is possible.
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
Assume that the filter is in Column A and row 1 is a header row :-

Code:
Dim cel As Range, rng As Range
Set cel = Cells(Rows.Count, 1)
Set rng = Range([A2], cel.End(xlUp)).SpecialCells(xlCellTypeVisible)
cel = 1
cel.Copy
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
cel.EntireRow.Delete
ActiveSheet.UsedRange
 

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi Boller,
This looks impressive, but I'm really sorry, I don't know enough VB to be able to know what its saying! :(
Is this a macro which I add to the one I have, or is it the macro that will add 1 to everything I select in one hit?

Sorry
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
It is a macro that will add 1 to each visible cell in Column A starting from A2 to the last visible cell within the filter range. (It has been assumed that all the column A cells will contain numerical data only.)

If you want it to add 1 to selected cells only, it needs to be re-written.
 

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
That is awesome.
I added "Sub add1()" to the beginning
and 'end sub' at the bottom, like the other macros seem to have.
It seems to work now.

Is that what I was supposed to do? :rolleyes:
 

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
oh no!
It killed my sheet!!!
I told it I wanted 1 added to column I, but changing the A to an I
But now its trying to add 1 to EVERY column in my table - even the ones that aren't visible.

Why?
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
In addition to changing the A to an I, you need to change this :-

Set cel = Cells(Rows.Count, 1)

to this :

Set cel = Cells(Rows.Count, 9)
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328
Here's an explanation of what each line does :-

Code:
'Define variables
Dim cel As Range, rng As Range

'Assign the last cell of the sheet in column 1 to the variable "cel"
Set cel = Cells(Rows.Count, 1)

'Set the visible cells in column A from A2 to the last cell in the filter range
Set rng = Range([A2], cel.End(xlUp)).SpecialCells(xlCellTypeVisible)

'Put the value "1" in cel
cel = 1

'Copy the value of cel
cel.Copy

'Add the value of cel to rng
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd

'Delete the sheet's last row (which contains cel)
cel.EntireRow.Delete

'Reset the sheet's last used cell
ActiveSheet.UsedRange
 

Forum statistics

Threads
1,081,793
Messages
5,361,316
Members
400,625
Latest member
Asraful Alam

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top