Excel macro - Find different filenames

%3CLINGO-SUB%20id%3D%22lingo-sub-3525380%22%20slang%3D%22en-US%22%3EExcel%20macro%20-%20Find%20different%20filenames%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3525380%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I'm%20trying%20to%20create%20a%20tool%20used%20for%20work%20purposes%2C%20every%20time%20we%20download%20a%20file%20from%20our%20server%2C%20it%20is%20labelled%20as%20%22USCOTRN%22%20-%20however%2C%20when%20multiple%20of%20these%20files%20are%20downloaded%2C%20they%20automatically%20rename%20to%20%22USCOTRN%20(1)%22%20and%20go%20up%20incrementally%20-%20depending%20on%20the%20number%20of%20times%20the%20file%20has%20been%20downloaded.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20my%20question%20is%2C%20I%20want%20to%20create%20a%20macro%20that%20when%20%22USCTORN%22%20is%20open%2C%20the%20macro%20automatically%20moves%20the%20worksheet%20to%20the%20new%20workbook%2C%20and%20formats%20the%20data%20-%20I%20have%20the%20macro%20working%20and%20formatting%20correctly%2C%20what%20I%20can't%20seem%20to%20get%20around%20is%20a%20way%20of%20different%20file%20names.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20VBA%20shows%20below%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESpoiler%3C%2FA%3E%3CNOSCRIPT%3E(Highlight%20to%20read)%3C%2FNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-border%22%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3ESub%20USCOTRNCopy()%3CBR%20%2F%3E'%3CBR%20%2F%3E'%20USCOTRNCopy%20Macro%3CBR%20%2F%3E'%3CBR%20%2F%3E'%20Keyboard%20Shortcut%3A%20Ctrl%2BShift%2BT%3CBR%20%2F%3E'%3CBR%20%2F%3EWindows(%22USCOTRN.csv%22).Activate%3CBR%20%2F%3ESheets(%22USCOTRN%22).Select%3CBR%20%2F%3ESheets(%22USCOTRN%22).Move%20After%3A%3DWorkbooks(%22USCOTRNMaster.xlsm%22).Sheets(1)%3CBR%20%2F%3ESheets(%22USCOTRN%22).Select%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-container%22%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3ESub%20USCOTRNCopy()''%20USCOTRNCopy%20Macro''%20Keyboard%20Shortcut%3A%20Ctrl%2BShift%2BT'Windows(%22USCOTRN.csv%22).ActivateSheets(%22USCOTRN%22).SelectSheets(%22USCOTRN%22).Move%20After%3A%3DWorkbooks(%22USCOTRNMaster.xlsm%22).Sheets(1)Sheets(%22USCOTRN%22).Select%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3EWhere%20%22Windows%20(%22USCOTRN.csv%22).Activate%20is%20placed%2C%20is%20there%20a%20workround%20where%20the%20macro%20automatically%20finds%20excel%20workbooks%20that%20are%20open%2C%20and%20uses%20that%20spreadsheet%20to%20run%20the%20macro%20and%20format%20the%20data%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%20for%20any%20help%20you%20can%20give!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3525380%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3526177%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20macro%20-%20Find%20different%20filenames%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3526177%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1429200%22%20target%3D%22_blank%22%3E%40QCube680%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20may%20try%20something%20like%20this...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20USCOTRNCopy()%0ADim%20wbDest%20%20%20%20%20%20As%20Workbook%0ADim%20wbSource%20%20%20%20As%20Workbook%0ADim%20wb%20%20%20%20%20%20%20%20%20%20As%20Workbook%0ADim%20wsSource%20%20%20%20As%20Worksheet%0A%0ASet%20wbDest%20%3D%20ThisWorkbook%0A%0AFor%20Each%20wb%20In%20Workbooks%0A%20%20%20%20If%20wb.Name%20Like%20%22USCOTRN*.csv%22%20Then%0A%20%20%20%20%20%20%20%20Set%20wbSource%20%3D%20wb%0A%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20End%20If%0ANext%20wb%0A%0AIf%20Not%20wb%20Is%20Nothing%20Then%0A%20%20%20%20Set%20wsSource%20%3D%20wbSource.Worksheets(%22USCOTRN%22)%0A%20%20%20%20wsSource.Move%20after%3A%3DwbDest.Worksheets(1)%0AEnd%20If%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Hi there,

 

So I'm trying to create a tool used for work purposes, every time we download a file from our server, it is labelled as "USCOTRN" - however, when multiple of these files are downloaded, they automatically rename to "USCOTRN (1)" and go up incrementally - depending on the number of times the file has been downloaded.

 

So my question is, I want to create a macro that when "USCTORN" is open, the macro automatically moves the worksheet to the new workbook, and formats the data - I have the macro working and formatting correctly, what I can't seem to get around is a way of different file names.

 

The VBA shows below,

 

Spoiler
Sub USCOTRNCopy()
'
' USCOTRNCopy Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Windows("USCOTRN.csv").Activate
Sheets("USCOTRN").Select
Sheets("USCOTRN").Move After:=Workbooks("USCOTRNMaster.xlsm").Sheets(1)
Sheets("USCOTRN").Select

Where "Windows ("USCOTRN.csv").Activate is placed, is there a workround where the macro automatically finds excel workbooks that are open, and uses that spreadsheet to run the macro and format the data?

 

Many thanks for any help you can give!

1 Reply

@QCube680 

 

You may try something like this...

 

Sub USCOTRNCopy()
Dim wbDest      As Workbook
Dim wbSource    As Workbook
Dim wb          As Workbook
Dim wsSource    As Worksheet

Set wbDest = ThisWorkbook

For Each wb In Workbooks
    If wb.Name Like "USCOTRN*.csv" Then
        Set wbSource = wb
        Exit For
    End If
Next wb

If Not wb Is Nothing Then
    Set wsSource = wbSource.Worksheets("USCOTRN")
    wsSource.Move after:=wbDest.Worksheets(1)
End If

End Sub