VBA Loop to change value from a Cell

12Rev79

New Member
Joined
Mar 2, 2021
Messages
48
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Dear Expert.

I have a file "Extract.xlsx" in the sheetname "voucher" with a list of voucher number as below
Column D2
CEGS-XS-AF-1370
CEGS-XS-AF-1375
CEGS-XS-AF-1401

The code below will open this file "Key support_DIV-CEGS-XS-AF-Fabrication.xlsx"
and rename base on the listed in Column D and change the Value of in Cell A5 base also in Column D

The code below works but in cell A5 it only write the value of the first voucher in Column D
I want that each file should be have the filanename base in listed in Column D2 up to the last and change the value Cell A5 each for listed voucher in Column D

Please may be someone is kind to help me figure out how to modify the code below.

Thanks in advance

Sub Macro1()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim ws As Worksheet, KeyW As Workbook, KeyS As Worksheet, ls As Long, i As Integer
Dim path As String, keypath As String

path = "C:\Desktop\Fabrication"
keypath = "Key support_DIV-CEGS-XS-AF-Fabrication.xlsx"

Set ws = ThisWorkbook.Sheets(1)
ls = ws.Cells(Rows.Count, "D").End(xlUp).Row
Set KeyW = Workbooks.Open(path & "\" & keypath)

Set KeyS = KeyW.Sheets(1)
For i = 2 To Application.CountA(ws.Range("D2:D" & ls))+1
With KeyS
.Range("A5").Value = Mid(ws.Range("D2").Value, 5, 20)
KeyW.SaveCopyAs path & "\" & "Key support_" & ws.Range("D" & i).Value & ".xlsx"

End With

Next i
KeyW.Close False 'Close file "Key support_DIV-CEGS-XS-AF-Fabrication.xlsx"

MsgBox "Done"

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I think you need to change this line:
VBA Code:
.Range("A5").Value = Mid(ws.Range("D2").Value, 5, 20)
to
VBA Code:
.Range("A5").Value = Mid(ws.Range("D" & i).Value, 5, 20)
Note: Please use the code formating for your code it does make it easier to read
 
Upvote 0
I think you need to change this line:
VBA Code:
.Range("A5").Value = Mid(ws.Range("D2").Value, 5, 20)
to
VBA Code:
.Range("A5").Value = Mid(ws.Range("D" & i).Value, 5, 20)
Note: Please use the code formating for your code it does make it easier to read

Dear offthelip thanks for the help, I appreciate so much it works will.
Noted, in my next post if any I will be using the Code formatting.

Thanks again offthelip and God Bless!
 
Upvote 0

Forum statistics

Threads
1,225,969
Messages
6,188,111
Members
453,460
Latest member
Cjohnson3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top