Setting the name of each cell to its content/value - excel

I'd like to create a macro that selects a rectangular range of cells and sets the name of every one of those cells to the value/contents of the cell.
In terms of what I've thought so far, I get an error though with the cell.Name line.
Public Sub NameCell()
Dim rng As Range
Dim cell As Range
Set rng = Range("A1:D1")
For Each cell In rng
cell.Name = CStr(cell.Value)
Next
End Sub

Is this what you meant?
Sub setVal()
Range("A1:C6").Select
Selection = "value"
End Sub

I believe this may work for you unless I also misunderstood the question.
Dim r As Range
Dim cell As Range
Set r = Sheet1.UsedRange
For Each cell In r
Sheet1.Names.Add Name:=cell.Value, RefersTo:=cell
Next
Keep in mind, though, that you would want to check that the cell.Value is valid (no spaces, etc.) for a named range.

To replace a range of cells with their values (removing any formulas from the range), you would use something like this.
Public Sub NameCell()
Dim rng As Range
Set rng = Range("A1:D1")
rng.Value = rng.Value
End Sub

Related

How to select the active cell if it has a value not the formula

In Range ("A2:A10") I have a vlookup equation.
I want to select the active cell in that range, if it has a value and ignore the other cells with the formula in the same range
This will Select the first cell in the range that does not have a formula:
Sub FindValue()
Dim rng As Range, r As Range
Set rng = Range("A2:A10")
For Each r In rng
If r.HasFormula Then
Else
r.Select
Exit Sub
End If
Next r
End Sub
If all the cells in the range have formulas, Selection is not changed.
If in range "A2:A10" you have formulas, but also cells without any formula, you can put the last one category in another range in this way:
Dim rng As Range, c As Range
Set rng = Range("A2:A10")
Set rng = rng.SpecialCells(xlCellTypeConstants) 'only cells without formula
For Each c In rng.cells
c.Select
Debug.Print c.Address 'if you want the first one, put here Exit For
Next
If no cells without formula, the above code will raise an error. It can be caught with some error handling, but I only tried showing a shor way of doing it.

Sum of range in a specific cell with predefined range in vba

I have a selected range that I defined as my range. I want to get the Sum of this selection in a specific cell.
The makro shall find "x", select the cell below and put in "Sum" + the range I defined in "myrange"
Sub more_twelve_months()
Dim myrange As Range
Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
Set more_twelve_months = Range("A1:ZZ10000").Find("x")
more_twelve_months.Select
FormularCell = ActiveCell.Offset(1, 0).Select
Selection.Resize(Selection.Rows.Count, _
Selection.Columns.Count).Select
ActiveCell.Sum (myrange)
I tried several ways to get the sum, ActiveCell.Sum (myrange) is just the last thing I tried.
Any ideas how I can solve this?
Is this what you want?
You should avoid using Select as far as possible.
Sub more_twelve_months()
Dim myrange As Range
Dim more_twelve_months As Range 'declare your variables
Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
Set more_twelve_months = Range("A1:ZZ10000").Find("x")
If Not more_twelve_months Is Nothing Then 'check you've found something to avoid errors
more_twelve_months.Offset(1).Value = Application.Sum(myrange)
End If
End Sub

In an excel range - ignore empty cells

I'm concatenating using an excel range:
Set rng = Range("A1:A8")
If one of the cells withing the range is empty it adds a single space.
How do you stop this space from being added?
Assuming you have got constants in those non-empty cells:
Set rng = Range("A1:A8").SpecialCells(2)
Unfortunately, non-contiguous ranges make that we can't put rng into an array or use it in a Join operation. You'll have to loop each cell in the range.
However, I would utilize Application.Trim instead of setting a range. This way we can work through memory (array) instead of a more sluggish Range object reference:
Sub Test()
Dim arr As Variant: arr = [TRANSPOSE(A1:A8)]
Debug.Print Application.Trim(Join(arr, " "))
End Sub
Or, if you don't want to utilize .Evaluate:
Sub Test()
Dim arr As Variant: arr = Range("A1:A8")
With Application
Debug.Print .Trim(Join(.Transpose(.Index(arr, 0, 1)), " "))
End With
End Sub
Note-1: We need Transpose to return a 1D-array to be able to use in Join.
Note-2: If one has got access to TEXTJOIN function, there is no need for all this, but you can utilize that function in an array form.
If you are concatenating range then a simple loop may help:
Dim rng As range, cell As range
For Each cell In range("A1:A8") ' looking through the desired range
If Not cell = "" Then ' if the cell is not empty we have to add it to range
If Not rng Is Nothing Then ' if the range already has some cells in it
Set rng = Union(rng, cell) ' we have to add the cell to an existing range
Else ' if the range does not yet exists
Set rng = cell ' create it and add the first cell
End If
End If
Next

Loop through range and find empty cells

I have a problem with the following code. I would like it to loop through a range (AK2 until end of data) and then any time it finds an empty cell it changes its value to Account receivable. Its not doing it, I also dont get any error notification. My guess is that I´m doing something wrong with setting the variables:
Option Explicit
Private Sub Leere()
Dim rng As range
Dim rcell As range
Dim WS As Worksheet
Set WS = Worksheets("Sheet2")
Set rng = WS.range("AK2", range("AK2").End(xlDown))
For Each rcell In rng
If rcell.Value = " " Then
rcell.Value = "Accounts Receivable"
End If
Next
End Sub
Any ideas?
use SpecialCells()
With ws
.Range("AK2", .Cells(.Rows.Count, "AK").End(xlUp)).SpecialCells(XlCellType.xlCellTypeBlanks).Value = "Accounts Receivable"
End With
You should be able to replace the empty (no formula) cells with something like this:
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set rng = ws.Range("AK2", ws.Cells(ws.Rows.Count, "AK").End(xlUp))
rng.Replace "", "Accounts Receivable", xlWhole
Another non-VBA option can be Conditional Formatting for blank cells. The cell values will still be blank, but the displayed text change will be dynamic.

How do I, using a macro, decrease all the values in a selected range by 1

I've searched for similar questions, but the problem I'm having is that I'm adding what I'm looking for to an existing recorded macro that uses commands like:
Range(Selection, Selection.End(xlUp)).Select
to select a range.
This uses the Counta function, assuming there aren't any blanks in the data. I also added IsNumeric in case the value is text. If you wanted to go a step further to make sure everything is neat, you can add this right before the If statement:
Sub Minus_One()
Dim Cell As Range
Dim Cells As Range
Set Cells = Sheets(WhateverSheet).Range("A1:A" & Application.WorksheetFunction.CountA(Range("A:A")))
For Each Cell In Cells
If IsNumeric(Cell.Value) = True Then Cell.Value = Cell.Value - 1
Next Cell
End Sub
Cell.Value = Trim(Cell.Value)
Sub test()
Dim rngTarget As Range
Set rngTarget = Sheet1.Range("A1")
Set rngTarget = Range(rngTarget, rngTarget.End(xlDown))
Dim rngIndex As Range
For Each rngIndex In rngTarget
With rngIndex
.Value = .Value - 1
End With
Next rngIndex
End Sub

Resources