List a variable range of tabs - excel

I am trying to get a list of the names of the sheets starting from a specific sheet, as I don't need the names of the tabs before that sheet. The problem is that these sheets will have variable length and sheet name from quarter to quarter (they might change name and decrease or increase in number).
This is what I have done but I get it wrong, but mind that my VBA level is null.
Sub XX1()
Dim x As Integer
Dim wb As Workbook: Set wb = ThisWorkbook
Dim WKS As Worksheet: Set WKS = wb.Sheets("10")
x = 1
For Each WKS In Worksheets
Sheets("Directory").Cells(x, 1) = WKS.Name
x = x + 1
Next WKS
End Sub
I want to get, from the sheet called S (is the sheet number 10), all the sheet names and list them in another sheet, called 'Directory'.

You will need to use the Worksheets.Index attribute to do this.
Here it is:
Sub XX1()
Dim x As Integer
Dim wb As Workbook
Dim wks As Worksheet
Dim sh As Worksheet
Set wb = ThisWorkbook
Set wks = wb.Sheets("10")
x = 1
For Each sh In Worksheets
If sh.Index >= wks.Index Then
Sheets("Directory").Cells(x, 1).Value = sh.Name
x = x + 1
End If
Next sh
End Sub
Obviously the result will change if you manually move the worksheets in your workbook.

Related

Copy Sheet1 in Active Workbook to Sheet 2 in Workbook named in Cell of Active Workbook Sheet 2

I have a series of Workbooks that I continually need to copy Sheet1 of a workbook to Sheet2 of the new workbook. The names of the Workbooks will advance in number (name_May2011_2, name_May2011_3, name_May2011_5). The digit on the end will change, not necessarily in sequence. I have code that allows me to list the active workbooks in a new sheet. I need to reference a cell in that sheet as the name of the Workbook as the destination top copy. My code is as follows so far:
[code]
Sub Copy_Merge()
'Declare variables and data types
Dim Wb As Workbook
Dim Ws As Worksheet
Dim i As Single, j As Single
'Create a new worksheet and save to object ws
Set Ws = Sheets.Add
'Go through open workbooks
For j = 1 To Workbooks.Count
'Save workbook name to cell A1 and downwards
Range("A1").Cells(j, 1) = Workbooks(j).Name
'Iterate through worksheets in given workbook
For i = 1 To Workbooks(j).Sheets.Count
'Save worksheet names to cell B1 and cells further right
Range("A1").Cells(j, i + 1) = Workbooks(j).Sheets(i).Name
'Continue with next worksheet
Next i
'Continue with next workbook
Next j
'This is the part I'm having issues with
'I need to set variable in cell A2 of Sheet 2 of Active Workbook as a string
'and use that string as the Workbook destination name
Dim SB As Workbook
Dim Ss As Worksheet
Set SB = ThisWorkbook
Set Ss = ThisWorkbook.Sheet("Sheet2")
Set MyToday = SB.Ss.Range("A2").Value 'name of destination workbook
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Workbooks(MyToday).Sheets(3)
On Error Resume Next
ActiveSheet.Name = "Sheet2"
On Error GoTo 0
End Sub
[/code]
I've been working on this for a few days, I'm relatively new to writing macro's, and I'm at my whit's end. Can someone help with this code or suggest a better code to use?
In order to retrieve the target workbook's name from cell "A2" of "Sheet2" of your current workbook and use it to assign the respective workbook to an object, you could use:
Dim SourceWorkbook As Workbook ' Your SB
Dim SourceWorksheet As Worksheet ' Your Ss
Dim TargetWorkbook As Workbook
Dim TargetWorksheet As Worksheet
Dim TargetWorkbookName As String
Dim TargetSheetName As String
Set SourceWorkbook = ThisWorkbook
Set SourceWorksheet = SourceWorkbook.Sheets("Sheet2") ' the "s" in "Sheets" is important to access the Workbook's Sheet-Collection
TargetWorkbookName = SourceWorksheet.Range("A2").Value ' TargetWorkbookName is of type "String", thus "Set" is not allowed
Set TargetWorkbook = Workbooks(TargetWorkbookName)
TargetSheetName = SourceWorksheet.Range("B2").Value
Set TargetWorksheet = TargetWorkbook.Sheets(TargetSheetName)

Copying data from a list of multiple standardised workbooks to the active workbook

I'd like to for each object in a table, open the workbook from a file path in column 4 and copy about 52 cells (which will be in the same place in each workbook) into my active spreadsheet.
Table looks like this
Code is rudimentary because I thought if I could solve the first copy, I could replicate it further
Set tbl = Sheet1.ListObjects("OTJ")
For Each cell In tbl.DataBodyRange.Columns(4).Cells
WB = cell.Value
Workbooks.Open Filename:=WB
Set x = Workbooks.Open(WB)
Set y = ActiveWorkbook
v = x.Sheets("Sheet2").cell("D70")
Cells(2, 5) = v
x.Close
I keep getting the subscript out of range error, please help
It's not really clear where you want to put the extracted data, but something along these lines should work:
Dim tbl As ListObject, cell As Range, wb As Workbook
Dim wsData As Worksheet
Set wsData = ThisWorkbook.Worksheets("Data") 'or wherever...
Set tbl = Sheet1.ListObjects("OTJ")
For Each cell In tbl.DataBodyRange.Columns(4).Cells
Set wb = Workbooks.Open(cell.Value)
wsData.Cells(2, 5).Value = wb.Sheets("Sheet2").Range("D70").Value
wb.Close False 'no save
Next cell

Need help adding a loop to count all hidden sheets or sheets beginning "S."

I want to remove the list here by replacing it with some sort of automatic count function which counts either the number of hidden sheets within the workbook or counts all sheets named "S." (would give the same result so whichever easier)
The goal is to automate the function so "22" is not used as some workbooks have fewer sheets
I've tried a few functions but so far can't integrate the counter into the second part of the code (For ii = 1 to 22)
Dim list(22) As Variant>
list(1) = "S.xxx "
list(2) = "S.xx "
etc
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim Filename As String
Dim GetFile As Variant
Workbooks.Open GetFile(i)
With ActiveWorkbook
For ii = 1 To 22
Set ws1 = Sheets.(list(ii))
Set ws2 = ThisWorkbook.Sheets(list(ii))
ws1.Cells.Copy ws2.Cells
Next ii
End with
ActiveWorkbook.Close
you can use a For Each ... Next loop ad check the first character of current sheet name:
Dim ws As Worksheet
Dim GetFile As Variant
' some code to define GetFile...
With Workbooks.Open(GetFile(i)) ' open and reference wanted file
For Each ws In .Worksheets ' loop through referenced workbook worksheets
If UCase(Left(ws.name, 1) = "S") Then ' if current worksheet name begins with "S"
ThisWorkbook.Worksheets(ws.name).UsedRange.Clear ' clear "target" worksheet (i.e. worksheet in ThisWorkbook with the same name) used range
ws.UsedRange.Copy ThisWorkbook.Worksheets(ws.name).Range("A1") ' copy current worksheet used range to "target" worksheet
End If
Next
.Close
End With

Setting a variable based on the number of highlighted tabs in a workbook?

I am trying to find the number of workbooks whose tab colors are the standard automatic color.
I have a way to do this but it requires looping through each worksheet in the workbook and increasing the count this way. Ie like so:
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
Dim n As Long
Set wb = Application.ThisWorkbook
n = 0
For Each ws In wb.Sheets
If ws.Tab.ColorIndex = -4142 Then
n = n + 1
End If
Next ws
Debug.Print (n)
End Sub
I know there is a worksheet function, one that can count the total number of worksheets in a workbook... ie:
n = wb.Worksheets.Count
Can one adjust this to include an if clause?
ie n=wb.worksheets.countif ? And if so can this be adjusted to fit my problem?
Thanks!

How to hide multiple columns in Excel sheet?

I'm using 4 sequential columns from sheet 1 to generate charts in sheet 2. I want to hide those 4 columns in sheet 1.
I've tried the below code to hide the columns:
Set allColumns = dataSheet.Columns("J:M")
allColumns.Hidden = True
Constants and References
Sub AllCol()
Const cVntSheet As Variant = "Sheet1" ' Worksheet Name/Index
Const cStrRange As String = "J:M" ' Range Address
Dim dataSheet As Worksheet ' Worksheet
' Create a reference to the worksheet.
Set dataSheet = Worksheets(cVntSheet)
' Hide the range.
dataSheet.Columns(cStrRange).Hidden = True
End Sub
Swap Sheet1 with your actual sheet name.
Sub HideMe()
ThisWorkbook.Sheets("Sheet1").Columns("J:M").Hidden = True
End Sub
Notice this can be done in one line. If you want to use variables (workbook or worksheets), they should build the above string when combined
Something like:
Dim wb as Workbook: Set wb = ThisWorkbook
Dim ws as Worksheet: Set ws = wb.Sheets("Sheet1")
Dim hm as String: hm = "J:M"
ws.Columns(hm).Hidden = True
If you substitute your variables into the last line, you will end up with the exact same line of code shown in the first sub.

Resources