mrn
mrn

Reputation: 425

How to check for a specific text in excel worksheet name using VB.NET? (What is wrong in this code?)

I want to achieve following,

I have an excel file that contains worksheets with different names. Some of the sheets are named as A1, A2, B1, B2, C1, C2 etc.

If the sheet name contains "A" then export A3:A70 to a different workbook called "range.xlsx"

If the sheet name contains "B" then export range B3:B70 to "range.xlsx"

If the sheet name contains "C" then export range C3:C70 to "range.xlsx"

Any help will be really appreciated.

This is what I tried:

Created a list of sheet names and looped through this list to do what I want to do.

Thanks

Upvotes: 1

Views: 2413

Answers (3)

mrn
mrn

Reputation: 425

Following code works.

Dim sheets As Excel.Worksheet
Dim slist As String = ""

For Each sheets In oWB1.Worksheets
slist += sheets.Name
Next

RichTextBox1.Text = slist

For i = 1 To num
If slist.Contains("C" & i) Then
'loops to export values
End If
Next

Thanks.

Upvotes: 0

drew.cuthbert
drew.cuthbert

Reputation: 1015

Untested code, but should get you going in the right direction...

Dim wkbk As Excel.Workbook = ...
Dim regxp As New Regex("[abc]")
For Each wkst In wkbk.Worksheets
  If Not regxp.Match(wkst.Name) = "" Then
    ...
  End If
Next

Upvotes: 1

mrn
mrn

Reputation: 425

I think, now I know how to deal with this issue. Apparently, if I can create a list of all excel worksheet names then I will be able to loop through this list. I will try to write it and post it.

Thanks

Upvotes: 0

Related Questions