Link to home
Start Free TrialLog in
Avatar of NVIT
NVITFlag for United States of America

asked on

Get rid of selecting hard-coded cell

Basically, this macro adds a new column to the left of Project Title column. Then, it adds  a formula to the new column.

Originally, I recorded it. I need help towards the end. Change it so it doesn't matter what cell, regardless how many rows the sheet has. Right now, it uses C1770.

Sub Add_Col_L()
'
' Add_Col_L Macro
'
'
    Cells.Find(What:="Project Title", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "L"
    Columns("C:C").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.ColumnWidth = 20
    Selection.NumberFormat = "General"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[1])"
    Range("C2").Select
    Selection.Copy
    Range("D3").Select
    Selection.End(xlDown).Select
    ' Fix: Change below to Go 1 cell left instead of specific cell
    Range("C1770").Select
    Range(Selection, Selection.End(xlUp)).Select
    Range("C3:C1770").Select
    Range("C1770").Activate
    ActiveSheet.Paste
End Sub

Open in new window

Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

one way would be to put the value in a Name (as opposed to defining a range for the Name)
Avatar of NVIT

ASKER

Hi Crystal,

Would you please give an example?
sure -- Formulas ribbon tab, Name Manager, New... command button

Name: MyValue (obviously you want to name this better)
Scope: Workbook (or change to specific sheet)
Refers to: =999 (or whatever value you want)

then in a cell, you can use:
=MyValue+3
(or whatever is your formula)

optionally, you can Name the cell, if you want to keep the value in the sheet, and as columns or rows are added, the reference should adjust -- as should formulas that refer to a cell address without dollar signs ($) ... but your code is specifying a particular address which is not adjusted

to Name a cell:
1. select the cell
2. in the Name box that shows the address, type the name, starting with a letter, without space or special characters and then press ENTER
3. you can then use this Name in formulas instead of a cell reference
Avatar of NVIT

ASKER

Seems like your solution is giving a formula, which is not what I need. Correct me if I'm wrong.

I need a way to move the cell to the left vs. picking the cell that was recorded.
>"move the cell to the left vs. picking the cell that was recorded"

perhaps before you start recording, set "Use Relative References" on the developer ribbon

to get the last row and column:
   With xlWs
      nLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row  'xlUp=-4162
      nLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 'xlToLeft=-4159
   End With

Open in new window

WHERE
xlWs is a worksheet object -- ie:Activeworkbook.sheets(1) -- or sheets("sheetname")
nLastRow and nLastCol are dimensioned as Long
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NVIT

ASKER

Hi Subohd... This works great! Thank you.
Have a nice day/night.
You're welcome. Glad to help.
Thanks and same to you.