Solved

Get rid of selecting hard-coded cell

Posted on 2016-09-01
8
68 Views
Last Modified: 2016-09-06
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

0
Comment
Question by:NVIT
  • 3
  • 3
  • 2
8 Comments
 
LVL 19
ID: 41780706
one way would be to put the value in a Name (as opposed to defining a range for the Name)
0
 
LVL 24

Author Comment

by:NVIT
ID: 41780721
Hi Crystal,

Would you please give an example?
0
 
LVL 19
ID: 41780728
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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 24

Author Comment

by:NVIT
ID: 41780744
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.
0
 
LVL 19
ID: 41780765
>"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
0
 
LVL 29

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41781021
See if this is what you are trying to achieve.

Sub Add_Col_L()
Dim c As Long, lr As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
c = Cells.Find(What:="Project Title", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Column
Columns(c).Insert

Cells(1, c).FormulaR1C1 = "L"

With Columns(c)
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    .ColumnWidth = 20
    .NumberFormat = "General"
End With
    Range(Cells(2, c), Cells(lr, c)).FormulaR1C1 = "=LEFT(RC[1])"
End Sub

Open in new window

0
 
LVL 24

Author Closing Comment

by:NVIT
ID: 41786675
Hi Subohd... This works great! Thank you.
Have a nice day/night.
0
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41786691
You're welcome. Glad to help.
Thanks and same to you.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question