Select data on worksheet when worksheet name is not constant

RFLundgren

New Member
Joined
Mar 28, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi there, back again!

I have some code that allows a user to select a workbook to open. Once opened, I need to select all the data on the active tab and then copy it to another workbook.

The workbook being opened only contains a single tab, and if I rename the tab and reference it directly in the code, it of course works, however each time the original report is run, the tab has a different name, although this name does have some constant elements.

The code below allows me to select a workbook to open, and works as long as the sheet name matches. how do I use a wildcard to manage this scenario?

VBA Code:
Public Sub GetFreshDeskData()

    'Open raw data file saved from Freshdesk to utilise in the monthly report

    Dim targetWorkbook As Workbook
    Set targetWorkbook = Application.ThisWorkbook
      
   
    'Dim Filter As String
    'Filter = "Text files (*.xlsx),*.xlsx"

    Dim Caption As String
    Caption = "Please Select the Freshdesk Monthly Tickets Report! "

    Dim Ret As Variant
    Ret = Application.GetOpenFilename '(Filter, , Caption)

    If VarType(Ret) = vbBoolean And Ret = False Then Exit Sub

    Dim wb As Workbook
    Set wb = Workbooks.Open(Ret)
    
    targetWorkbook.Worksheets("RawData").Cells.Clear
        
    wb.Worksheets("Tickets").UsedRange.Copy targetWorkbook.Worksheets("RawData").Range("A1")
        
    wb.Close SaveChanges:=False
    
End Sub

This is probably relatively easy to do, but it has been 20 years since I last coded, so lots to try and remember!

Many thanks!

Richard
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
how about wb.sheets(1). Think that should reference the first worksheet of the open workbook so if you only have one sheet i think it might work.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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