I have a sheet with one cell that is = the name of a folder that I want to dim as a variable. what I want to do is set that cell = the filename variable. It will probably be easier to look at my code. I am currently getting the "object required error on my "set Input 1" and my way to set the variable is presumably wrong as well
Dim WbkA As Workbook
Dim Input1 as string
Set Input1 = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls").worksheets("Sheet4").Range("A4").Value
Set wbkA = Workbooks.Open(FileName:"Input1")
You try to assign a reference of an object with the keyword Set to a data type (String).
Remove the keyword Set and it's gonna be okay.
The code needs to be reordered slightly in order to breakout the steps.
Get the file path and name from the workbook and store it as a string
variable (Input1).
Open the file using the value stored in the string variable (Input1).
Set a reference to the open file as an object variable (WbkA).
Listed below is the code
Sub test()
Dim Input1 As String
Dim WbkA As Workbook
Input1 = Worksheets("Sheet4").Range("A4").Value 'Get the path and file name
Workbooks.Open Filename:=Input1 'Open the file
Set WbkA = ActiveWorkbook 'Set the reference to the workbook
MsgBox WbkA.Name 'Show the name value from the object.
End Sub
Related
I'm currently working on developing a macro that will input various forms into an access database.
Due to the nature of the beast of this program, I've had to split my main program into two sub programs and call them, but I need to use getobject to call a file path twice now.
I use getobject to open a file, and then use myrec.fields(~column name~) = xlsht.cells(1, "a") to populate various column values. I'm unsure if there are other "efficient" ways to accomplish this.
I was wondering if it is possible to use a variable in place of the filepath with the GetObject command, instead of needing to manually replace the file path in the code.
I've tested a fair amount of different code, including the path, class functionality but I don't think I understand VBA enough to truly make the best use of that.
I can make it work using this
Dim XL As Variant
Dim XLApp As Variant
Dim XLsht As Variant
Dim XLwrkbk As Variant
Set XL = CreateObject("Excel.Application")
Set XLwrkbk = GetObject(~file path~)
Set XLsht = XLwrkbk.Worksheets(1)
Set MyRec = CurrentDb.OpenRecordset("database name")
Ideally I would like it to be
Dim filename As String
Dim XL As Variant
Dim XLApp As Variant
Dim XLsht As Variant
Dim XLwrkbk As Variant
filename = " ~insert file path~ "
Set XL = CreateObject("Excel.Application")
Set XLwrkbk = GetObject(filename)
Set XLsht = XLwrkbk.Worksheets(1)
Set MyRec = CurrentDb.OpenRecordset("database name")
I receive a run time error
Run-time error '5':
Invalid procedure call or argument.
Try something like this:
Dim XL As New Excel.Application, Filename As String
Filename = "~ your file ~"
XL.Workbooks.Open (Filename)
myrec.fields(~column name~) = XL.Worksheets(1).Range("A1").value
I'm having trouble creating a dropdownlist that contains a name and a value for each column.
What I want to achieve is to use the dropdownlist that shows a bunch of names, but I want those names to have a
value, so that I can use it (value) in macro.
For exmple, when I select a name called "Joe" it will consists of the value 25 and will
be outputted as 25.
I've Googled around for some tutorials, but most of it wasn't useful. I've pasted the the img. I want to create a drown from this
Bakers name and value. But have no clue to how I can make this possible.
Some examples or tips would be great! I would love to hear from you.
Here is the macro code:
Sub findValue()
Dim s As String
Dim value As Integer
Dim namesList As Range
Set namesList = Range("G3:G8") 'here you have to specify your range with names
s = InputBox("Enter the name: ")
'now you find the entered value (held in s variable) and get the value of a cell which
'is right to cell found (Offset function, then applied Value property)
value = namesList.Find(s).Offset(0, 1).Value
MsgBox "Found value is " & value
'save found value into the file
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile(yourPath) 'don't forget to enter full path to the
'text file here!!
oFile.WriteLine value
oFile.Close
Set fso = Nothing
Set oFile = Nothing
End Sub
Now, the value is stored under value variable, you can do what you want with it :)
EDIT: I wanted to clarify my earlier question. I have existing name formula in my name manager. I would like to set these variables listed below to reference different columns from another document. These variables are referenced in my other spreadsheet.
My question is
1) How do I set the variables to reference another document's column?
2) How can I make it so the Macro allows me to choose which document each time?
Here's my code
Sub UpdateReport()
Dim wbk As Workbook
Set wbk = Workbooks.Open("U:\user\Destination.xls")
Dim x As Variant
Dim FY123 As Range
Dim FY134 As Range
Dim FY145 As Range
Dim FY156 As Range
Dim FY167 As Range
Dim FINCC As Range
Set FINCC = wbk.Worksheets("Worksheet2").Column("D")
Set FY123 = wbk.Worksheets("Worksheet2").Columns("N")
Set FY134 = wbk.Worksheets("Worksheet2").Columns("O")
Set FY145 = wbk.Worksheets("Worksheet2").Columns("P")
Set FY156 = wbk.Worksheets("Worksheet2").Columns("Q")
Set FY167 = wbk.Worksheets("Worksheet2").Columns("R")
End Sub
Thanks,
GS
EDIT: Clarified question
I have already a one workbook open but I am running a macro from another workbook. I would like to activate the first workbook using its name.
The code:
FileName = input_path_1 & input_file_1
Workbooks(FileName.xls).Activate
When I try to do so, it is giving me "Subscript out of range" error. How do I solve it?
Check if your variable Filename contains the correct filename. (e.g. Sample.xls)
Also check if input_path_1 and input_file_1 have correct values.
If they have it should be like this:
Workbooks(Filename).Activate
Now, if you need to append the extension name (e.g. Filename value is just Sample):
Workbooks(Filename & ".xls").Activate
The argument should always be in the form of string and should be the complete filename (with extension). Although numerals (index) is also accepted, you can't be sure what index refer to what workbook. Better yet, assign it to a variable.
Dim otherWB As Workbook
Set otherWB = Workbooks(Filename)
'Set otherWB = Workbooks(Filename & ".xls") '~~> for second scenario above
Edit1: From comment, if Filename contains the fullpath, then this might work.
Dim Filename1 As String
Filename1 = Split(Filename, "\")(UBound(Split(Filename, "\")))
Workbooks(Filename1).Activate
Only way to access the window of the specific workbook is by below method
Vba
Dim filename as string
set filename = Path.GetFileName(fullFilename)
set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlMinimized
set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlNormal
' You can also use Worksheet.Activate() here if you want
C#
string filename;
filename = Path.GetFileName(fullFilename);
Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlMinimized;
Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlNormal;
// you can also use Worksheet.Activate() here if you want
Set OutsideWb = Workbooks("path + Filename.xlsm") wont work if workbook already open
set a global wb variable to the opened file and use that
eg.
Set oXLBook = oXLApp.Workbooks.Open("path + Filename.xlsm") '
Set OutsideWb = oXLBook 'prolly dont need oxlbook todo
In Excel 2019,
Workbooks(Filename).Activate may not work if ".xlsx" is part of the variable name.
Example: Filename = "123_myfile.xlsx" may not activate the workbook.
In this case, try:
Filename = left(Filename,len(Filename)-5) 'Filename now = "123_myfile"
Workbooks(Filename & ".xlsx").Activate
I have some code below, It works like a charm but I'm curious about some things.
Why can't I make a Set xWorkb = new Workbook statement? Instead I use the Dim xWorkb as new Workbook, which works. But I've learned (hopefully correct) that using the new statement within a Dim is bad practice, and that you should create the object seperately. So why doesn't it work? I get a ActiveX component can't create object error, but the xWorkb is still being created later as an object right due to the new statement in the Dim section? Makes me confusing.
Why can't I use the excel.application.workbooks when defining variable xApp? Is it because I have to specify a workbook and can't just leave the workbooks empty like that? I get a type mismatch error when I'm trying to change excel.application to excel.application.workbooks.
Sub tester()
Dim xWorkb As New Workbook
Dim xApp As Excel.Application: Set xApp = New Excel.Application
Dim xFiles_target() As Variant
Dim file_path As String
xFiles_target = Array("Bella.xls", "Fizz.xls", "Milo.xls", "Jake.xls")
file_path = Dir("C:\Users\hans\Desktop\")
Do While Len(file_path) > 0
Debug.Print file_path
If UBound(Filter(xFiles_target, file_path)) >= 0 Then
Debug.Print "found " & file_path
Set xWorkb = xApp.Workbooks.Open("C:\Users\hans\Desktop\" & file_path)
xApp.ActiveSheet.Cells(2, 2) = "tester"
xWorkb.Save
xWorkb.Close
End If
file_path = Dir
Loop
End Sub
You cannot create new workbooks with New because workbooks are coupled with Application and must be created with Workbooks.Add or Workbooks.Open.
Dim xWorkb as new Workbook does not work - it appears to work because you don't access xWorkb between declaring it and assigning it with Workbooks.Open. If you did, you would get the same ActiveX component can't create object error.
The error is because Excel.Workbook does not have any public constructors.
You cannot define a variable as excel.application.workbooks because that is not a type. It is a property named Workbooks, of type Excel.Workbooks, that belongs to an object named Application of type Excel.Application.
You can declare the variable as Excel.Workbooks, but you probably don't want to, because you will need to create an Excel.Application to use it anyway.