Importing dynamic named worksheets Using a condition

Gautham A

Board Regular
Joined
May 25, 2020
Messages
107
Office Version
  1. 2016
Platform
  1. Windows
I have a situation where a data source (xlsx) file has a changing sheet name.

The excel file has multiple sheets like Region,Location,ABC 1.1,ABC 2.2, ABC 2.1

I have a requirement where I need to get a single sheet based on a condition.

The condition are

1. It should contain 'ABC'

2. Now there are three sheets with name ABC. It should pick only ABC 2.2 because 2.2 is greater than 2.1 and 1.1.



So next week if the data gets refreshed and if I find a sheet ABC 3.1, it should pick that sheet and load the data in that sheet.

excel_sc.png




How to write a M code for this condition. Can someone please help?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Below code is sample to : select the max-sheet, or paste value to max-sheet.
From this, you can do every thing with that sheet.
VBA Code:
Option Explicit
Sub test()
Dim i&, ws As Worksheet, id As Double, max As Double, sht As String
For i = 1 To Worksheets.Count
    If Worksheets(i).Name Like "ABC*" Then
        id = CDbl(Mid(Worksheets(i).Name, 4, 255))
        If id > max Then
            max = id
            sht = Worksheets(i).Name
        End If
    End If
Next
Worksheets(sht).Activate ' to select the max-sheet
Worksheets(sht).Range("A1").Value = 100 ' to paste value into the max-sheet
End Sub
 
Upvote 0
Hello,
thanks for the response. But I'm working this requirement in power bi. So I require a M code for this
 
Upvote 0
Change C:\path and filename as appropriate and try

Power Query:
let
    Source = Excel.Workbook(File.Contents("C:\path\filename.xlsx")),
    SheetName = List.Last(List.Sort(List.Select(Source[Name], each Text.Start(_,3)="ABC"))),
    Result = Source{[Name = SheetName]}[Data]
in
    Result
 
Upvote 0

Forum statistics

Threads
1,223,663
Messages
6,173,652
Members
452,525
Latest member
DPOLKADOT

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