Solved

Get rid of selecting hard-coded cell

Posted on 2016-09-01
8
61 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 18
Comment Utility
one way would be to put the value in a Name (as opposed to defining a range for the Name)
0
 
LVL 23

Author Comment

by:NVIT
Comment Utility
Hi Crystal,

Would you please give an example?
0
 
LVL 18
Comment Utility
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
 
LVL 23

Author Comment

by:NVIT
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 18
Comment Utility
>"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 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
Comment Utility
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 23

Author Closing Comment

by:NVIT
Comment Utility
Hi Subohd... This works great! Thank you.
Have a nice day/night.
0
 
LVL 28

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now