Solved

Excel - variable to apply to active workbook?

Posted on 2014-12-04
2
72 Views
Last Modified: 2014-12-04
Hello Experts,

I have two workbooks - one with a macro and a button to trigger it (wkbk1), and one with data (wkbk2 - it's actually a csv file).  The following works just fine when I run it from within wkbk2:

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
'open the csv file and begin formatting the data
'the following line is commented out when running it from within the csv file
'but is un-commented when I run it from the external workbook
'Workbooks.Open Filename:="F:\Folder\File.csv"

'shift column M to the right
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

'insert formula into M2
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=0,""No"",""Yes"")"

'paste the formula down column M to the last populated cell
Range("M2").AutoFill Destination:=Range("M2:M" & LR)

'copy header from L1 to M1
Range("L1").Select
Selection.Copy
Range("M1").Select
ActiveSheet.Paste
Range("A2").Select

'turn off alerts to save over the existing file
Application.DisplayAlerts = False

'save the csv file as an xlsx file
ChDir "F:\Folder"
ActiveWorkbook.SaveAs Filename:="F:\Folder\File.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

'turn on alerts again
Application.DisplayAlerts = True

'close the workbook
Workbooks("File.xlsx").Close

'exit Excel
Application.Quit

Open in new window


But I need it to run from another workbook (wkbk1).  However, it places the formula in M2, copies it to M1, and then moves on to the next step (copying the column header from L1 to M1) without pasting the formula down column M.  It appears that LR is based on column A of wkbk1 and not wkbk2.  I need LR to be based on column A in wkbk2, the active workbook, not wkbk1.  

Any help would be greatly appreciated.

I am using Excel 2010 on Windows 7.

Thanks,
J
0
Comment
Question by:ferguson_jerald
2 Comments
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
ID: 40481204
Move line 2 of your code to just below line 6, so that the row count isn't done until the csv file is opened.

Like this:
Dim LR As Long

'open the csv file and begin formatting the data
'the following line is commented out when running it from within the csv file
'but is un-commented when I run it from the external workbook
Workbooks.Open Filename:="F:\Folder\File.csv"
LR = Range("A" & Rows.Count).End(xlUp).Row --Count the used rows in the newly opened file.

'shift column M to the right
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Open in new window

0
 

Author Closing Comment

by:ferguson_jerald
ID: 40481217
Excellent!  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 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

10 Experts available now in Live!

Get 1:1 Help Now