?
Solved

Get rid of selecting hard-coded cell

Posted on 2016-09-01
8
Medium Priority
?
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 22
ID: 41780706
one way would be to put the value in a Name (as opposed to defining a range for the Name)
0
 
LVL 25

Author Comment

by:NVIT
ID: 41780721
Hi Crystal,

Would you please give an example?
0
 
LVL 22
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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 25

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 22
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 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 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 25

Author Closing Comment

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

Expert Comment

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

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
New style of hardware planning for Microsoft Exchange server.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

752 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