I would like to convert an array which contains the name of my sheets, but as Object and not as String
For example, I have this :
arr=(Sheet01, Sheet02, Sheet03)
When I add a Watches on my array, I get the type Variant/Object/Sheet01 and that's I need for.
But I would like to not hardcoding my namesheet and use a Range like :
arr = Worksheets("Data").Range("NameSheet")
But when I use this solution I get the type Variant/Variant but I need the object type for after use something like :
Set SheCurrent = arr(i)
Is there possible to do something like that ? Thanks for the help and sorry for the approximate english
This is one way to convert a array of strings into an array of worksheet objects:
Sub WhatsInAName()
arr = Array("Sheet1", "Sheet2", "Sheet3")
Dim oArr(1 To 3) As Worksheet
i = 1
For Each a In arr
Set oArr(i) = Sheets(a)
i = i + 1
Next a
End Sub
Worksheets are part of a Workbook. Every workbook has a collection of worksheets that you can access either by name of by number. If you write Worksheets("Data"), it accesses the worksheet "Data" of the current active workbook.
So the simple answer would be
Set SheCurrent = worksheets(arr(i))
But I would advice to read a little bit about how to deal with Workbooks and Worksheets in VBA, for example 10 ways to reference Excel workbooks and sheets using VBA. As a general advice, you should (nearly) never assume a specific workbook to be active.
Related
I have this formula that looks at various criteria across multiple columns and checks to see that if all the all the criteria match, it will paste data from one column to another. I've tried a couple ways to get it into VBA, but I can't seem to get anything to work. Thanks!
=IFERROR(INDEX(Sheet1!A$2:A$205,SMALL(IF(ISNUMBER(SEARCH("ECR Approval",Sheet1!$C$2:$C$205)),ROW(Sheet1!$A$2:$A$205)-ROW(Sheet1!$A$2)+1),ROWS(A$2:A2))),"")
This is an Array Formula and you can place this formula on the Sheet by using the following code...
Dim ws As Worksheet
Set ws = Sheets("Sheet2") 'Sheet where formula would be placed
ws.Range("A2").FormulaArray = "=IFERROR(INDEX(Sheet1!A$2:A$205,SMALL(IF(ISNUMBER(SEARCH(""ECR Approval"",Sheet1!$C$2:$C$205)),ROW(Sheet1!$A$2:$A$205)-ROW(Sheet1!$A$2)+1),ROWS(A$2:A2))),"""")"
ws.Range("A2").AutoFill ws.Range("A2:A205"), xlFillDefault
To use a function in VBA, you need to use before each function Application.WorksheetFunction.
x = Application.WorksheetFunction.Sum(y,z)
To reference a cell in a sheet in VBA you can use Rage
x = Application.WorksheetFunction.Sum(Range("A1:A2"))
Put this to things Together and it would look like this:
=Application.WorksheetFunction.IFERROR(Application.WorksheetFunction.Index(Worksheet(1).Range("A2:A205"),Application.WorksheetFunction.SMALL(Application.WorksheetFunction.IF(Application.WorksheetFunction.ISNUMBER(....
Im trying to open and copy the cells from a sheet from a different excel file. I have no problem in opening, copying and closing the excel file that I need.In this case, I have With x.Sheets("Documents").UsedRange. Most files that I needed to copy the cells from have "Documents" as a sheet name, but some have "Documents" + other different characters (example, "DocuemntsEX"). When I tried to copy, it shows 'Subscript Out-of-range' since the "DocumentEX" is different from "Document". Is there any way that I can retain the specific name of the sheet, since most of the files have that name? Is there any code that can help me to access those sheet with a different sheetname? Just hit me up if you need clarifications.
Use a wildcard character to get the sheet first:
Function GetDocumentSheet(ByRef wb As Workbook) As Worksheet
For Each ws In wb.Sheets
If LCase$(ws.Name) Like "documents*" Then
Set GetDocumentSheet = ws
GoTo SheetFound:
End If
Next
Set GetDocumentSheet = Nothing
SheetFound:
End Function
In your code:
Set mySheet = GetDocumentSheet(x) '// where 'x' is your workbook object
Then reference
mySheet.UsedRange
As i said in comment, you can use sheets by number, or you can use something like this
Sub findSheet()
Dim sheetSubName As String
sheetSubName = "fluff"
Dim currentSheet As Worksheet
For Each currentSheet In Sheets
If currentSheet.Name Like sheetSubName & "*" Then
MsgBox "do some stuff"
End If
Next currentSheet
End Sub
When you comparing sheet name to string and regular character (in my example * so its anything). So this macro will work with sheet fluff, fluffy fluffiest etc.
And everything to looping via Sheets (all sheet in workbook) and comparing their names via Like (something like = ) but it can use some basic regular expression.
I basically created an Excel VBA application that manipulate Excel worksheets, so in the code, I use the string "Sheet1" to refer to the first sheet of a workbook, but when I try to use this application with the same code with a french version of Excel, it doesn't work until I translate "Sheet1" to "Feuil1". So my question is, is there a way to automatically adapt the code to any version of Excel ?
You can use the following ways to get a sheet from code:
(1) using by Sheets(sheet_index)
This way cannot be adapt because it take the sheet by sheet index (sheet index are start from 1). When sheet are change place, it cannot access the right sheet.So, it should not use.
For example: Set Feuil1Sheet = Sheets(1)
(2) using by (Name) of VBA editor
I think this way should not use never, because it takes the sheet by code name which can only visible by VBA editor(it shows as (Name) field in sheet properties). I think you are using this way for getting the first sheet. So, you not get the right sheet. One thing you need to know is that code name of every first sheet may not be Sheet1 always. It can be Sheet2 or Sheet4, etc.
For example: Set Feuil1Sheet = Sheet1
(3) using Worksheets("sheet-name") or Sheets("sheet-name")
This last way is a very compatible way and can be adapt in anywhere Excel because it take the sheet by its name. So, If names are equal, you will get the right sheet. So, use this for getting the sheet.
For example: Set Feuil1Sheet = Worksheets("Feuil1") or Set Feuil1Sheet = Sheets("Feuil1")
The only possible way I can think of to always reference "sheet1" in the local language is the following code.
Option Explicit
Public Sub GetLocalNameForNewSheets()
Dim strSheetName As String
Dim i As Long
i = ActiveWorkbook.Sheets.Count
ActiveWorkbook.Sheets.Add After:=Worksheets(i)
strSheetName = ActiveWorkbook.Worksheets(i + 1).Name
Application.DisplayAlerts = False
ActiveWorkbook.Worksheets(i + 1).Delete
Application.DisplayAlerts = True
Debug.Print strSheetName
For i = 1 To Len(strSheetName)
While IsNumeric(Mid(strSheetName, i, 1))
strSheetName = Replace(strSheetName, Mid(strSheetName, i, 1), "")
Wend
Next i
Debug.Print strSheetName
Debug.Print strSheetName & "1"
End Sub
Basically, I am asking Excel to create a new sheet and name it for me. Then, I am getting the new name which is "sheet" in the local language and remove from the string the number part. At the end, you can add the number "1" to reference the first sheet.
I am new to VBA and am having problems learning the rules of variables (I think that's the problem here).
I have two worksheets in a spreadsheet. I need to make code that automatically hides a row on worksheet 2 if that same value in column a is on worksheet 1, column a.
Here's one of the variations of code I've tried:
Dim Sheet2Value As Variant
Dim Sheet1Value As Variant
'
Sheet2Value = Sheets("Sheet2").Range("A:A").Value
Sheet1Value = Sheets("Sheet1").Range("A:A").Value
'
If Sheet2Value = Sheet1Value Then
Sheets("BMAC=N").EntireRow.Hidden = False
Else
Sheets("BMAC=N").EntireRow.Hidden = True
End If
I get a type mismatch error but I'm not sure exactly why. I chose variant because I don't know what I'm doing, but both columns in excel will be set to "General".
Can anyone help with this? What concept am I missing?
Thanks so much for your time.
Few things:
you cannot compare entire column:
Sheet2Value = Sheets("Sheet2").Range("A:A").Value
you need to loop through the collection of cells, see this: Fast compare method of 2 columns
you cannot hide row without defining a range to hide
Sheets("BMAC=N").Range("Some_address").EntireRow.Hidden
Finally, i'd suggest to change your code to shortest way:
Sheets("BMAC=N").Range("A1").EntireRow.Hidden = (value1<>value2)
Good luck!
This question already has an answer here:
How can I get worksheet code name to activate a specific worksheet?
(1 answer)
Closed 3 years ago.
I am attempting to copy data from one workbook to my current workbook using VBA. InputBook is a workbook object referring to the file from which I would like to extract data. The main issue has to do with referencing particular worksheets in the InputBook workbook. In InputBook, I have a worksheet named "Lines" with the codename LINES. I would prefer to reference this worksheet by its codename, for example:
NumItems = WorksheetFunction.CountA(InputBook.LINES.Columns(1))
This clearly doesn't work and I know I can make it function by using either of the following:
NumItems = WorksheetFunction.CountA(InputBook.Sheets("Lines").Columns(1))
NumItems = WorksheetFunction.CountA(InputBook.Sheets(2).Columns(1))
I would, however, rather not use either of those methods as they seem to be less robust. Is there any way to reference the codename of a worksheet object in another open workbook? Thanks.
You can "hack" a reference to another workbook sheet code name by:
Sub UseCodeNameFromOutsideProject()
Dim WS As Worksheet
With Workbooks("InputBook .xls")
Set WS = _
.Worksheets(CStr(.VBProject.VBComponents("Lines").Properties(7)))
debug.print WS.Name
End With
End Sub
Your WS object is now set to the sheet which has the codename "Lines" in this example.
Original inspiration is here.
I could call a workbook "I LIKE TEA" but InputBook.I LIKE TEA.Columns(1) wont cut it.
Abstract the name away with a const or:
public enum InputSheets
LINES = 2,
GARMENTS = 4
end enum
to allow:
InputBook.Sheets(InputSheets.LINES).Columns(1)
or
MyGetSheetFunction(InputBook, InputSheets.LINES)
You could take this further and use a wrapper class.