0

I had similar question, that I posted over 1y ago, and it was basically solved but... Here is the link for more infos:

VBA CopySheetsFromClosedWorkbook but overwrite (update) existing sheets with new values

Recently I bumped on some issue where I am facing WBs with different Sheet names but with the same content. Every time I am having this issue I need to tweak a code a bit, to adjust it for right Sheet names. is there any function or additional array that I can include in the code so that it recognises if the Sheet1 had name XYZ and now ZYX to still proceed with code and get me the data?

In my case, also you can see in the code, it is only matter of two different names with numbering. Original Sheet name is "CH_or_Recipe_1 to 8", but sometimes I will have cases with "Chamber 1 to 8". I would like to have defined those Sheet names within my code so that I dont need to adjust it manually every time I wanna copy the data.

And last additional question or favor is, to export the exact WB but without .xlsm (without macro) in .xlsx with all the data. So Macro WB would be like an intermediar to gather the data and export them...

This is coding part:

Sub CopySheetFromClosedWorkbook2()
    
    'Prompt to choose your file in the chosen locatioon
    Dim dialogBox As FileDialog
    Dim FilePath As String
    Set dialogBox = Application.FileDialog(msoFileDialogOpen)
    Application.StatusBar = "Choose older PDS Form!"

    dialogBox.AllowMultiSelect = False
    dialogBox.Title = "Select a file"
    If dialogBox.Show = -1 Then
        FilePath = dialogBox.SelectedItems(1)
        
    'If nothing selected then MsgBox
    Else
       MsgBox "No PDS Form selected!"
       Exit Sub
    End If
    
    'Here are sheets defined which you are going to copy/paste (reference update) but to keep formatting.
    ''Sheets should be defined from right to left to have your sheets sorted from the beginning
    Dim shNames As Variant: shNames = Array("CH_or_Recipe_8", "CH_or_Recipe_7", "CH_or_Recipe_6", "CH_or_Recipe_5", "CH_or_Recipe_4", _
    "CH_or_Recipe_3", "CH_or_Recipe_2", "CH_or_Recipe_1", "Customer Details", "Instructions")
    
    
    '"Chamber 8", "Chamber 7", "Chamber 6", "Chamber 5", "Chamber 4", "Chamber 3", _
    "Chamber 2", "Chamber 1"
    
    
    Dim tgt As Workbook: Set tgt = ThisWorkbook
    Application.ScreenUpdating = False
    Dim src As Workbook: Set src = Workbooks.Open(FilePath)
    Dim ws As Worksheet, rng As Range, i As Long
    For i = 0 To UBound(shNames)
        On Error Resume Next
        Set ws = src.Sheets(shNames(i))
        If Err.Number = 0 Then
            tgt.Worksheets(shNames(i)).Cells.Clear
            Set rng = ws.UsedRange
            rng.Copy tgt.Worksheets(shNames(i)).Range(rng.Address)
        End If
    Next i
    src.Close False
    Application.ScreenUpdating = True
    MsgBox "Copy&Paste successful!"
End Sub
MmVv
  • 553
  • 6
  • 22
  • You can reference sheets by their CodeName shown in brackets (name) on the properties tab, or by their position (ws.Index). Would they be the same in each workbook ? Or use the last character (number) if that is a common feature. – CDP1802 Feb 15 '22 at 11:20
  • @CDP1802 Yeah, always the same...from 1 to 8 but only name changes... – MmVv Feb 15 '22 at 11:29
  • What are the sheet names of the active workbook that you are copying to. – CDP1802 Feb 15 '22 at 11:46
  • @CDP1802 These are all with CH_or_Recipe_1 to 8..... – MmVv Feb 15 '22 at 12:00

1 Answers1

1

Change the sheets names for those ending in a number 1 to 8

    Dim src As Workbook: Set src = Workbooks.Open(FilePath)
    Dim ws As Worksheet, rng As Range, i As Long
    ' add code here
    For Each ws In src.Sheets
        If ws.Name Like "*[1-8]" Then
            ws.Name = "CH_or_Recipe_" & Right(ws.Name, 1)
        ElseIf ws.Name = "Customer_Details" Then
            ws.Name = "Customer Details"
        End If
    Next
    ' existing
    For i = 0 To UBound(shNames)

Save as XLSX


Sub SaveNoMacro()

    Dim fn As String
    With ThisWorkbook
        fn = Replace(.FullName, ".xlsm", ".xlsx")
         Application.DisplayAlerts = False
        .SaveAs fn, FileFormat:=xlWorkbookDefault
         Application.DisplayAlerts = True
    End With
    MsgBox "Saved as " & fn
    
End Sub
CDP1802
  • 13,871
  • 2
  • 7
  • 17
  • It works! I tried on both examples, very nice! The last thing that I wrote here was to save it as normal .xlsx file...it can be as a second macro also. – MmVv Feb 15 '22 at 12:45
  • @mirza Ok see update for xlsx – CDP1802 Feb 15 '22 at 13:23
  • Sorry last thing, WB and Sheets are password protected (same password), and I would like to have this steps: unprotect wb/sheets > run the code > protect wb/sheets. I found as a separate SUB but would be handy to run everything together... – MmVv Feb 15 '22 at 15:11
  • I had to answer here instead to open new topic, because it is just small thing I guess...if you do see a code, and line: Dim shNames As Variant: shNames = where I deifned all my SHeets.....I bumped out in reality on Sheet that has "underscore"....for instance "Customer_Details" instead of "Customer Details"....and when I include in my array it doesnt read/copy and infos from that sheet.....how to tweak that? idea maybe? thanks! – MmVv Mar 18 '22 at 12:05
  • @Mirza see update with added `ElseIf ws.Name = "Customer_Details" Then` – CDP1802 Mar 18 '22 at 15:55
  • Thanks it works. Question, what if I got like some files that are having 2x sheets only (with different names), can I also include them in =if statement? Because I realize I will be having sometimes different sheet names instead of "CH_1-8" and so on. Not always but there are some older files. Is there any efficient way to solve that instead of adding always if clauses? – MmVv Mar 21 '22 at 11:40
  • UPDATE! Or if there is change in a sheet name just to assign CH1 to CH8 depending how many is there...but if doesnt go it is also fine. You helped me anyways, I can tweak with IF THEN – MmVv Mar 22 '22 at 09:09
  • @mirza Hard to say without knowing all the different names. You need find some part of the filename that is common to all the variations that uniquely identifies. – CDP1802 Mar 22 '22 at 12:21
  • Yeah I agree, this is when you have "organised" Excel users....making jungle of data. Well in this case it might differ, without common part (like thos with CHs), it can be once ABC and next XYZ-1 and so on...i though it might be possible by sheet position, so sheet1 sheet2 and not sheet name...so sheet 1 = CH1...that can be assigned. But as I say, it works better now and this little tweak is also fine. – MmVv Mar 23 '22 at 11:17
  • Maybe it is a bit older post but it is one last thing I am asking if that is possible at all. Summary of code is: choosing one external workbook, taking all the data from it and copying them in 1:1 ratio on another workbook, saving file from .xlsm to .xlsx. There is exactly my question, it saves with the same name as my original template, but is there a possibility to save the file in the same name as external workbook that I wanna upload? So, the workbook from where I am taking (copying) right data got a name of ABC, my template XYZ and I wanna to have it saved as ABC_DateTime? – MmVv May 03 '22 at 13:38