Avatar of ferguson_jerald
ferguson_jerald

asked on 

Excel - variable to apply to active workbook?

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
Microsoft Excel

Avatar of undefined
Last Comment
ferguson_jerald
ASKER CERTIFIED SOLUTION
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ferguson_jerald
ferguson_jerald

ASKER

Excellent!  Thanks!
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo