I have hard coded file name (old.xls) in my macro (in another workbook). old.xls is already open when I execute the macro. Even then the below line prompts me to choose a file.
.Formula = "=IF(AND(ISNUMBER(VALUE(MID(Q1, 2, 1))),LEFT(TRIM(Q1), 1) = ""R""),VLOOKUP(Q1 & "" *"",[old.xls]Sheet1!$D:$V,19,0),VLOOKUP(Q1,[old.xls]Sheet1!$E:$V,18,0))"
Please advise. Why isn't the macro referring old.xls which is already open? Why does it ask me to choose old.xls every time?
Every time I run the macro Excel prompts me with an open file menu.
I am pretty sure you should test full reference:
Dim test As String
test = ThisWorkbook.Path & "\"
'your WITH block code...
.Formula = "=IF(AND(ISNUMBER(VALUE(MID(Q1, 2, 1))),LEFT(TRIM(Q1), 1) = ""R""),VLOOKUP(Q1 & "" *"",'" & test & "[old.xls]Sheet1'!$D:$V,19,0),VLOOKUP(Q1,'" & test & "[old.xls]Sheet1'!$E:$V,18,0))"
If still there is a prompt I would close old.xls file and check simple file open:
Sub openTest()
Dim test As String
test = ThisWorkbook.Path & "\old.xls"
Workbooks.Open (test)
End Sub
Related
I'm new to VBA coding. I'm trying to open workbooks, read and write to them.
I have to open the first workbook to start the code.
This code opens opens other workbooks and writes into them.
This worked fine for testing purposes. However, when I have two workbooks open and start the code, incorrect value is stored.
Here is the code:
Sub Workbook_test ()
WB_path = "C:\Users\az54\Macro_files\"
Source_File = WB_path & "Test_Source.xlsm" ' Source file name
File_Test = Dir(Source_File)
If (File_Test = "") Then
MsgBox "Source file not found at " & Source_File & vbCr & vbCr & "Check for Source file and RESTART"
GoTo FINISH
Else
Set Source_WB = Workbooks.Open(Source_File)
MsgBox "Source Workbook name = " & Source_WB.Name & vbCr & "Source Sheet name = " & setup_tab
Set Src_Sheet = Source_WB.Worksheets(setup_tab)
End If
FINISH:
End Sub
The variable Source_WB is not getting the workbook name from variable Source_File, instead it is getting loaded with another open workbook.
Any ideas?
Thanks
Change
Src_Sheet = Source_WB.Worksheets(setup_tab)
to
Src_Sheet = Source_WB.Sheets("setup_tab")
I've got a need to open some Excel files and "pause" then close them. In this process I run one macro on opening, and another on closing. The opening one works fine because it is done as each file is opened. But the closing part of the code I can't get it to run the correct macro. They have the same names, but the file contests are different, and what the macro does per file is different.
This is the gist of what I'm doing now
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
path = "\\Gaalpa1cdfile19\north_sa_staff\Reports\Rpt-ProductionCurves\"
filename2018P1 = "2018 P1.xlsm"
Set xlbook2018P1 = xlApp.WorkBooks.Open(path & filename2018P1)
' Run Macro
xlApp.Run "AutoRefresh"
filename2018P3 = "P3 2018 HRR.xlsm"
Set xlbook2018P3 = xlApp.WorkBooks.Open(path & filename2018P3)
'Run Macro
xlApp.Run "AutoRefresh"
'My "pause"
WScript.Echo ("All Files were" & Chr(013) & _
"opened and refreshed, update ppt before OK" & Chr(013) & _
" DO NOT CLICK OK" & Chr(013))
'==========================
'Below is the trouble spot.
'==========================
xlapp.Run "'" & filename2018P1 & "'" & "!AutoPublish"
xlbook2018P1.Close False
Set xlbook2018P1 = Nothing
xlapp.run "'" & filename2018P3 & "'" & "!AutoPublish"
xlbook2018P3.Close False
Set xlbook2018P3 = Nothing
The first part works fine, but trying to run the file's respective AutoPublish macro does not. The code works fine if I leave out that Run line. (The real file names have spaces and I had to add the single quotes to get it to accept the filename.)
What it appears to be doing is using the macros from the last file opened, not the one it's directed to use it the run line. I think I need a way to "select" the correct file, or give it focus so the macro could run without an explicit filename argument, which it appears to be ignoring anyway.
EDIT:
Solution was:
xlbook2018P1.Activate ' This fixed it, I think
xlapp.Run "'" & filename2018P1 & "'" & "!AutoPublish"
xlbook2018P1.Close False
Set xlbook2018P1 = Nothing
xlbook2018P3.Activate
xlapp.run "'" & filename2018P3 & "'" & "!AutoPublish"
xlbook2018P3.Close False
Set xlbook2018P3 = Nothing
When tackling similar tasks, I usually work around by implementing a master Excel file first, and call a sub in this master file via VBS. The advantage to me seems it is way easier to fullfill all tasks in the VBA of the master file rather than having to code all that in VBS.
Create a master file, e.g. "Master.xlsm", list all your files you need to open on a sheet named "Files" in column A, starting in row 1.
Insert a module and place the following sub in this module:
Sub Main()
Dim strPath As String
Dim strFile As String
Dim lRow As Long
Dim i As Long
Dim k As Integer
Dim n As Long
Dim wb(1 To 3) As Workbook
Dim wbTest As Workbook
Set wbMaster = ThisWorkbook
strPath = "\\Gaalpa1cdfile19\north_sa_staff\Reports\Rpt-ProductionCurves\"
'Check how many files you need to open
With Sheets("Files")
lRow = Sheets("Files").Range("A" & .Rows.Count).End(xlUp).Row
End With
'open all available files
For i = 1 To lRow
Workbooks.Open (wbMaster.Sheets("Files").Range("A" & i).Value)
Next
'now run the two macros in each open file
For k = 2 To Workbooks.Count 'this will work only if your master file is the only one open when starting the sub!
Workbooks(k).Run "'" & Workbooks(k).Name & "'!AutoRefresh"
DoEvents
Workbooks(k).Run "'" & Workbooks(k).Name & "'!AutoPublish"
DoEvents
Next
'and close all files previously opened except for the master file
For n = Workbooks.Count To 2 Step -1
Workbooks(n).Close False
Next
End Sub
It seems like a possible explanation for what you're seeing is that your AutoPublish macro refers to ActiveWorkbook and not the safer ThisWorkbook. If another workbook is active when it's called that could lead to unexpected results.
I'm trying to save documents called "Client Advice - " then value of cell=A1 which has a dropdown of clients... I cannot copy and paste this value into the Save As Dialog box and I have tried multiple workbooks and have restarted both Excel and the computer
Is there either a way to fix this please? I'd rather not have to type hundreds of client names when the information is already there...
-A
How about a macro?
See here : How to use workbook.saveas with automatic Overwrite
Set xls = CreateObject("Excel.Application")
xls.DisplayAlerts = False
Set wb = xls.Workbooks.Add
fullFilePath = importFolderPath & "\" & "A.xlsx"
wb.SaveAs fullFilePath, AccessMode:=xlExclusive,ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
wb.Close (True)
And then you would edit this line
fullFilePath = importFolderPath & "\" & "A.xlsx"
So that it has your cell with its value in there
Im trying to run autoitscript from excel vba using a button click.
I wanted to run notepad1.au3 script from an excel sheet.
i copied the script into the same directory as excel sheet.
I wrote the following vba code to run the script.Everything seems to work fine, it takes the path file name etc accurately.
But instead of just running the script, an explorer window pops up ,asking me to locate the script i want to run.
I can browse to the location of the script through the explorer window and select the script file and it will run.
But i want it to run without opening an explorer window.
Any idea where could be the problem?
Thanks
VBA Code:
Sub Autoit()
Dim AutoItPath
Dim FileName As String
Dim FileName1 As String
FileName = ThisWorkbook.Path & "\notepad1.au3"
MsgBox (FileName)
AutoItPath = "C:\Program Files (x86)\AutoIt3" & "\AutoIt3.exe "
MsgBox (AutoItPath)
FileName1 = """" & AutoItPath & """" & """" & FileName & """"
MsgBox (FileName1)
runscript = Shell(FileName1)
End Sub
Your call is wrong. See the help file:
Run a script using the interpreter:
AutoIt3.exe [/ErrorStdOut] [/AutoIt3ExecuteScript] filename [params ...]
Execute the AutoIt3 script 'filename' with optional parameters
example command: "'" & "C:\Program Files (x86)\AutoIt3\AutoIt3.exe " /AutoIt3ExecuteScript "..path\notepad1.au3" & "'"
I have this little VBA module that I call from one workbook to update all Excel Workbooks in a given folder. By update I mean it copies a module called GetActiveXControlValues and then runs this macro on each workbook in that folder. Now when I run this on my machine everything works fine. When my co-worker runs this same code with the same files, they gets a surprise after copying the module. When you go to look at the workbook that should have the new module called 'GetActiveXControlValues', instead there is no module by that name, instead it is called 'Module1'. In addition, when you look inside the new module it says 'Attachment has been removed' in red. I checked and my co-worker has the exact same Security Settings in Excel 2010 as I have.
I have enable all Macros and Trust VBA Project Object Model. I have Prompt me for enabling all ActiveX controls. I have Disable Trusted Documents unchecked and all the boxes on the Protected View tab. Anyone seen this before or have an idea what I can try to troubleshoot?
Sample Code:
Sub CopyModuleAndExecuteIt()
Dim wb As Workbook
Dim sFile As String
Dim sPath As String
Dim sFullMacroName As String
SetFolder
sPath = sExcelFolder
ChDir sPath
sFile = Dir("*.xls") ' File Naming Convention
Do While sFile <> "" ' Start of LOOP
' Open each Excel File in the specified folder
Set wb = Workbooks.Open(sPath & "\" & sFile) ' SET BP HERE!
Sleep (1000)
' Unprotect the Documents using SendKeys Hack
UnprotectVBADocument
' Import the GetActiveXControlValues Module into the Workbook
wb.VBProject.VBComponents.Import ("D:\GetActiveXControlValues.bas") ' SET BP HERE!
sFullMacroName = "'" & wb.Name & "'" & "!" & wb.VBProject.VBComponents.Item("GetActiveXControlValues").Name & ".GetActiveXControlValues"
' Run the GetActiveXControlValues Macro
Application.Run (sFullMacroName)
' Close the Workbook Saving Changes
wb.Close True
sFile = Dir
Loop ' End of LOOP
End Sub
If your co-worker has the exact same Security Settings in Excel 2010 as you have then the next thing that comes to my mind is the "Firewall". Check his firewall settings.
I was working to create an AddIn trough VBA code, i wrote the code in a Excel worksheet when i save it, i saved as text like this:
Attribute VB_Name = "Module_Name"
And you have to be sure that you .bas file is actualy is plain text.
I was working to create an AddIn with VBA code, i wrote the code in a Excel worksheet when i save it, i saved as text like this:
Sub Superheroes()
Dim sBeg as string, sEnd as String, sCatwoman as String, sAntMan as String
Dim vCode As Variant
'' Here is where i put the name i want to call my module
sBeg = "Attribute VB_Name = ""VBA_BasFile""" + vbCrLf + _
"Private Function fMix(sAnimal as String)as String "
sCatwoman = "Select case sAnimal"+ vbCrLf+ vbTab+"case ""cat"""+ _
vbCrLf+ vbTab+ "fMix = ""Catwoman"""
sAntMan = vbCrLf+ vbTab+"case ""Ant"""+ vbCrLf+ vbTab+ "fMix = ""AntMan"""+ _
vbCrLf+ "End Select"
sEnd = vbCrLf+ "End Sub"
vCode = Array(sBeg, sCatwoman, sAntMan, sEnd)
Workbooks.add
Range("A1").Resize(UBound(vCode) + 1, 1) = Application.Transpose(vCode)
With ActiveWorkbook
.SaveAs path + "VBA_BasFile.bas", xlTextPrinter
.Close False
End With
End Sub
With this i can Call any procedure or function in the VBA_BasFile when i importe to another Excel Workbook.