I am trying to use vba to update a number value in a cell on another excel workbook by 1.
so if I have the value 466 in workbook 2 then when I run my code form workbook 1 this will update 466 to 467, and each time the code is run it gets incremented by 1.
the workbook will normally be closed but I want it to be able to work whether the workbook is open or closed, if that matters.
I am trying to use the following code but its not updating anything and I am not getting any errors.
Please can someone show me what I am doing wrong. Thanks
Dim ws1112 As Worksheet, ws2221 As Worksheet
Set ws1112 = Sheets("Statistics")
Set ws2221 = Workbooks("\\UKSH000-File06\Purchasing\New_Supplier_Set_Ups_&_Audits\Workbook 2.xls").Sheets("Dashboard")
ws2221.Range("C7").Value = ws2221.Range("C7").Value + 1
Now I'm going to go out on a limb and say that your error is "Error '9', subscript out of range" but that this error is somehow being suppressed or silently handled by the rest of your code (for example an On Error GoTo statement).
This is occurring because you can't open a workbook simply by including the full filepath in your Workbooks("<name>") statement. You'll need to use Workbooks.Open("<FilePath>") for that.
For example:
Dim wb As Workbook
Dim ws1112 As Worksheet
Dim ws2221 As Worksheet
Set ws1112 = Sheets("Statistics")
Set wb = Workbooks.Open("\\UKSH000-File06\Purchasing\New_Supplier_Set_Ups_&_Audits\Workbook 2.xls")
Set ws2221 = wb.Sheets("Dashboard")
ws2221.Range("C7").Value = ws2221.Range("C7").Value + 1
'Optional if you want to close the workbook afterwards
wb.Close SaveChanges:=True
Related
I'm trying to create a macro in Excel that allow me to copy and paste data from one workbook to another workbook. The workbook names are always going to be different, so I used GetOpenFilename() method and stored it in a variable, so I can specifically choose the workbook I want. However, I am getting a runtime 9 error.
Here's the code so far. (macro1 is just another macro I created that crunches numbers)
Sub Everything()
Dim f1 As String
Dim f2 As String
Dim wb1 As Workbook
Dim wb2 As Workbook
f1 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb1 = Workbooks.Open(f1)
f2 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb2 = Workbooks.Open(f2)
Call Macro1
Workbooks(wb2.Name).Worksheets("Sheet1").Range("D4:D25").Copy _
Workbooks(wb1.Name).Worksheets("Sheet1").Range("E11:E32")
End Sub
I get the run time error on the very last two lines of code.
Please help, thank you!
I want to loop through all open Excel workbooks to identify which one on which to perform operations. Problem is, the code exits the for loop after the active workbook and returns "Nothing" as a result, regardless of how many workbooks I have open.
I need to run this routine weekly to transfer working hours from a downloaded Excel workbook into an alternate workbook. The name of the file changes every week, but always begins with "Timesheets"
I used this routine every week from January through April without any problems. I tried to use it today and this problem cropped up. I've used the routine on several different computers with different operating systems (Windows 7, Windows 10).
I've saved, closed, and reopened the workbook I want to activate to no avail. I don't want to have to change the code every week to access a specific workbook, but use the first 4 letters in the file name to identify the file on which to perform operations.
Sub cmdImportHours_Click()
Dim ThisWB As Workbook
Dim ImportWB As Workbook
Dim WB As Workbook
Dim msg1 As String
Dim msg As Variant
' more variables
msg1 = "Required file not found. Open the import file and try again."
Set ThisWB = ThisWorkbook
ThisWB.Worksheets("Hours").Activate
'The following loop exits after one iteration (the active workbook),
'regardless of how many workbooks are open
For Each WB In Workbooks
If Left(WB.Name, 4) = "Time" Then
WB.Activate
Exit For
End If
Next WB
If WB Is Nothing Then
msg = MsgBox(msg1, vbOKOnly)
Exit Sub
End If
'more code
End Sub
I expect the loop to look at the name of every open Excel workbook, but it exits the For Loop after looking at the active workbook only.
Your For Each over all workbooks directly returns a usable variable, which references the wanted workbook, so you may even use the variable "ImportWB" here. Exiting the loop by Exit For, if you found the desired item, is good practice.
I introduced two variables for worksheets to use them for copy operations.
Sub cmdImportHours_Click()
Dim ImportWB As Workbook
Dim SourceSheet As Worksheet, DestSheet As Worksheet
Dim msg1 As String
Dim msg As Variant
For Each ImportWB In Workbooks
If Left(ImportWB.Name, 4) = "Time" Then Exit For
Next ImportWB
If ImportWB Is Nothing Then
msg1 = "Required file not found. Open the import file and try again."
msg = MsgBox(msg1, vbCritical + vbOKOnly, "Workbook not open")
Exit Sub
End If
Set DestSheet = ThisWorkbook.Worksheets("Hours")
'DestSheet.Activate is typically not necessary
Set SourceSheet = ImportWB.Sheets(1)
DestSheet.Range("A2:B10").Value = SourceSheet.Range("A2:B10").Value
' more code
End Sub
As ThisWorkbook is always the same (the workbook with your VBA-code), it's not necessary to give it an extra variable, and I omitted it.
If you don't get your already opened workbook by above code, then it's either opened within a protected view ...
For i = 1 To Application.ProtectedViewWindows.Count
Debug.Print "Protected Workbook: " & _
Application.ProtectedViewWindows(i).Workbook.Name
Next i
... or within another Excel instance. In that case you may reference it by e. g.
Set ImportWB = GetObject("Path\Filename.xlsx")
See here for more examples on that.
That because Exit For, just comment that line. And don't use WB outside for each use other variable instead, in this code variable count used to count matching workbooks
Dim count As String
count = 0
For Each WB In Workbooks
If Left(WB.Name, 4) = "Time" Then
count = count + 1
WB.Activate
'Exit For
End If
Next WB
If count < 1 Then
msg = MsgBox(msg1, vbOKOnly)
Exit Sub
End If
I'm conducting my very first VBA macro and I'm having some difficulties with this seemingly easy code to read data from a closed workbook into my currently opened one.
Sub KAuto()
Dim path As String
path = "C:\files\Utfall.xlsx"
Dim currentWb As Workbook
Set currentWb = ThisWorkbook
Dim openWb As Workbook
Set openWb = Workbooks.Open(path)
Dim openWs As Worksheet
Set openWs = openWb.Sheets("March")
currentWb.Sheets("Indata").Range("A1").Value = openWs.Range("A3").Value
End Sub
The problem I'm having is that I get a code 9, subscript out of range. But I've checked that A1 and A3 is existent for the current workbook and the imported one respectively.
What I have tried to do is to omit the ".Value" in all combinations, as that was what the original author did.
Googling this problem I've encountered that people misused functions which I do not use, for instance windows(), or omitted "" for referencing the worksheets, or simply misspelled things. I don't Think I have any of these, and so I need further help.
How can I correct my subscript out of range? Is there a better way to achieve this copying of cells? In the future I want to import 10 files, will this then be obsolete? (I recall someone posting something in the lines of openWb = [file1,file2,file3] and looping through them, but I cannot find it; does anyone have a link?
EDIT: I've copied the path to the file from its properties, so it ought to be correct.
EDIT2:
currentWb.Sheets("Indata").Range("A1").Value = openWs.Range("A3").Value
snippet gives the error
EDIT3: VB editor print screen:
Try using ActiveWorkbook instead of ThisWorkbook.
Set currentWb = ActiveWorkbook
ThisWorkbook refers to the workbook in which the code resides. ActiveWorkbook refers to the workbook that is currently active i.e. "on top" in the Excel application. It looks like your case, the code resides in a different workbook; so what you want is ActiveWorkbook.
And you can ommit the .value from last line.
currentWb.Sheets("Indata").Range("A1") = openWs.Range("A3")
Your code worked fine for me, that`s why I cannot be sure if it will help. There can be an issue, when opening the openWs. The error line can be evaluated before the openWs is actually open. Then maybe add a line :
Application.Wait (Now + TimeValue("00:00:03")) 'this is 3 seconds from now
after the Set openWb = Workbooks.Open(path).
I've been trying to write a macro to copy "Sheet1" from one workbook into another, but I keep getting a run-time error '9': Subscript out of range.
Sub CopySheetToOtherWbk()
Dim CopyFromBook As Workbook
Dim CopyToWbk As Workbook
Dim ShToCopy As Worksheet
Set CopyFromBook = Workbooks("AllBugs.xlsx")
Set ShToCopy = CopyFromBook.Worksheets("Sheet1")
Set CopyToWbk = Workbooks("YourFriendlyNeighborhoodTemplateWorksheet.xlsx")
ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)
End Sub
The highlighted line is "Set CopyFromBook = Workbooks("AllBugs.xlsx")". Not sure what I'm doing wrong here. Relatively new to VBA. Any help is appreciated.
The Workbooks collection refers to all currently open workbooks. If the workbook isn't open you will need to do this first.
Set CopyFromBook = Workbooks.Open("C:\Some Location\AllBugs.xlsx")
I have a directory with list of Workbooks, I want to loop through them withouth opening them and update a Cell in a certain Sheet
I have tried to use
Dim wb As Workbook
Set wb = Workbooks("Z:\dir\bla.xls") 'THIS WILL COME TRHOUGH WHEN I LOOP
Set ws2 = wb.Sheets("TestSheet") 'SHEET NAME
Set CurCell_2 = ws2.Range("A1")
CurCell_2.Value = 5
The Problem comes it only works when I have the Workbook already open. I can use:
Workbooks.Open
But then It opens up in the background and takes to long to run through them all.
Can anyone help please
You cannot do that without opening the workbooks. However, I have found in my case that using Application.EnableEvents and setting it to false sped up greatly the process because we have macros on workbook open event.