Changing two cells in multiple files

jhightowerisc

New Member
Joined
Mar 3, 2016
Messages
11
Office Version
  1. 2019
Hello everyone

I want to be able to change two cells in hundreds of files.

One cell is M2 and is currently a dropdown box i want to replace with the NPI

The other cell is little different

C1 current has a part number that is in this format 100xxxx i want to be able to make that 1000xxxx without changing the last four digits.


Thanks for listening
 
Hello dear thank you for providing the feedback, I am also a learner, I checked with input you gave, that was an error, so I changed my code and and providing you with the new one , I hope it works for you, please check and provide the feedback

VBA Code:
Sub mycode()

Dim ws As Worksheet
Dim i As Integer
Dim search_x() As String

Dim cvalue As String
Dim firstpart As String
Dim lastpart As String
On Error GoTo leave

For Each ws In ActiveWorkbook.Worksheets
    ws.Range("M2").Value = "npi"
    ReDim search_x(1 To Len(ws.Range("c1").Value) - 1)
    For i = 1 To Len(ws.Range("c1").Value) - 1
     search_x(i) = Mid(ws.Range("c1").Value, i + 1, 1)
     Next i
     For i = 1 To Len(ws.Range("c1").Value) - 1
        If search_x(i) <> "0" Then
            cvalue = ws.Range("c1").Value
            firstpart = Mid(cvalue, 1, i)
            lastpart = Mid(cvalue, i + 1, Len(cvalue) - i)
        ws.Range("c1").Value = firstpart & "0" & lastpart
        Exit Sub
        End If
    Next i
Next
leave:
End Sub
Perfect. Just need to be able to do that to 200 more files in a folder.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Perfect. Just need to be able to do that to 200 more files in a folder.
Hello glad to see it worked for you. okay to be specific this code will work only when the numbers in c1 cell has the value of type 10xxx, 100xxxx and similar, but wont work well if your value is like 525005 or similar. because as the criteria mentioned by you in the query i wrote accordingly, below is the code for multiple files. a pop up to select a folder will appear where you have all you 200 files stored. and the actions will happen behind the screen in background.

Do provide a feedback if it works for you

VBA Code:
Sub mycode()

Dim filedir As String
Dim filetolist As String
Dim openbook As Workbook

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Please select the folder"
    .ButtonName = "Pick Folder"
    If .Show = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    Else
        filedir = .SelectedItems(1) & "\"
    End If
End With

filetolist = Dir(filedir & "*.xlsx")

Do Until filetolist = ""
DoEvents
    
    Set openbook = Workbooks.Open(filedir & filetolist)

Dim ws As Worksheet
Dim i As Integer
Dim search_x() As String

Dim cvalue As String
Dim firstpart As String
Dim lastpart As String
On Error GoTo leave

For Each ws In openbook.Worksheets
    ws.Range("M2").Value = "npi"
    If Not IsEmpty(ws.Range("c1").Value) Then
        ReDim search_x(1 To Len(ws.Range("c1").Value) - 1)
        For i = 1 To Len(ws.Range("c1").Value) - 1
         search_x(i) = Mid(ws.Range("c1").Value, i + 1, 1)
         Next i
         For i = 1 To Len(ws.Range("c1").Value) - 1
            If search_x(i) <> "0" Then
                cvalue = ws.Range("c1").Value
                firstpart = Mid(cvalue, 1, i)
                lastpart = Mid(cvalue, i + 1, Len(cvalue) - i)
            ws.Range("c1").Value = firstpart & "0" & lastpart
            GoTo solution
            End If
        Next i
    End If
solution:
Next
    openbook.Save
    openbook.Close
    filetolist = Dir
Loop

Application.ScreenUpdating = True

leave:
End Sub
 
Upvote 0
Solution
Hello glad to see it worked for you. okay to be specific this code will work only when the numbers in c1 cell has the value of type 10xxx, 100xxxx and similar, but wont work well if your value is like 525005 or similar. because as the criteria mentioned by you in the query i wrote accordingly, below is the code for multiple files. a pop up to select a folder will appear where you have all you 200 files stored. and the actions will happen behind the screen in background.

Do provide a feedback if it works for you

VBA Code:
Sub mycode()

Dim filedir As String
Dim filetolist As String
Dim openbook As Workbook

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Please select the folder"
    .ButtonName = "Pick Folder"
    If .Show = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    Else
        filedir = .SelectedItems(1) & "\"
    End If
End With

filetolist = Dir(filedir & "*.xlsx")

Do Until filetolist = ""
DoEvents
    
    Set openbook = Workbooks.Open(filedir & filetolist)

Dim ws As Worksheet
Dim i As Integer
Dim search_x() As String

Dim cvalue As String
Dim firstpart As String
Dim lastpart As String
On Error GoTo leave

For Each ws In openbook.Worksheets
    ws.Range("M2").Value = "npi"
    If Not IsEmpty(ws.Range("c1").Value) Then
        ReDim search_x(1 To Len(ws.Range("c1").Value) - 1)
        For i = 1 To Len(ws.Range("c1").Value) - 1
         search_x(i) = Mid(ws.Range("c1").Value, i + 1, 1)
         Next i
         For i = 1 To Len(ws.Range("c1").Value) - 1
            If search_x(i) <> "0" Then
                cvalue = ws.Range("c1").Value
                firstpart = Mid(cvalue, 1, i)
                lastpart = Mid(cvalue, i + 1, Len(cvalue) - i)
            ws.Range("c1").Value = firstpart & "0" & lastpart
            GoTo solution
            End If
        Next i
    End If
solution:
Next
    openbook.Save
    openbook.Close
    filetolist = Dir
Loop

Application.ScreenUpdating = True

leave:
End Sub

It works and it's just what i needed. thank you
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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