VBA to assign "id number" into all files in folder?

Rookeby7

New Member
Joined
Jul 5, 2019
Messages
18
Hi all,
I am new to VBA but am learning, and have had some very useful help on here previously, I think what I am looking to do should be possible, but am not sure where to start.

I have a batch of 600 files in a folder and need to assign a id/reference number (starting at #1261 ) in to cell B4 in each

In my head this seems like it should be straightforward, but I can't figure it out and don't seem to be using the right terminology to find previous questions relating to similar.

I hope someone will be able to help, thanks in advance
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
this should work:
Code:
Sub addID()
    Dim path As String, idLoc As String, fn As String
    Dim id As Long, idStart As Long
    
    path = "C:\MyPath" '<--Change this
    
    idStart = 1261
    idLoc = "$B$4"
    
    If Right(path, 1) <> "\" Then path = path & "\"
    id = idStart
    fn = Dir(path & "*.xls*")
    Application.ScreenUpdating = False
    While LenB(fn)
        With Workbooks.Open(fn)
            .Sheets(1).Range(idLoc).Value = id
            .Close True
        End With
        id = id + 1
        fn = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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