help with vba

22newbie

New Member
Joined
Jun 11, 2022
Messages
13
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a code that pulls all the files from a folder to the spreadsheet. It works perfectly fine. However, I would like to add a column the first 2 letters of the filenames so I can keep track of where the data is from.
Here is the code. Hopefully someone can help
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWB As Workbook
    Set desWS = ThisWorkbook.Sheets("Master")
    Dim LastRow As Long
    Const strPath As String = "C:\Users\test\Desktop\New folder\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        With srcWB.Sheets("Sheet2")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A2:D" & LastRow).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
        End With
        srcWB.Close False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
open visual basic editor (Alt+F11) in your active workbook, go to the insert menu, and select module. This will insert a standard module into your current project.
View attachment 66867


They look like this (Module1, 10, 11 etc.)
View attachment 66866
Paste the code I gave you into your module (or simply add it to an existing module. If you add to existing, it must not be a module behind a sheet or userform, or anything else for that matter.

I entered the function call into A1. If you drag it down like any other formula, it should automatically refer to the next row in the column for each value in A. If there is no value, B cell should be blank. Enter into whatever column you like, as long as A1 is replaced with the cell address you need.

View attachment 66869
Thank you soooo much! Really appreciate your patience and time! those worked for me.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Glad I could help. Perhaps mark this thread as solved?
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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