简体   繁体   中英

vba excel increment number value in cell on another workbook?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM