Is there a way in Excel VBA to get the name of the next sheet following the ActiveSheet ?
For example let's presume that my spreadsheet has 4 sheets and I would like to create a macro which inserts in the current worksheet (Sheet1 in this case) the name of the next spreadsheet (Sheet2). Like this:
Activesheet.Index will give you the position of the active sheet, so:
ActiveSheet.Range("A1")= ActiveSheet.Parent.Sheets(Activesheet.Index + 1).Name
You should add a check that the activesheet isn't the last one...
Just a little sample here.
Dim sh As Worksheet, shprv As Worksheet, shnext As Worksheet
//make sure that use "as Worksheet" after every variable you declare
Set sh = ActiveSheet //always use "Set" before since these variables are "Object type", only by this their values can be set correctly
Set shprv = sh.Previous
Set shnext = sh.Next
In this case, the value of sh will be Sheet1 as the picture you gave,
and the value of shnext will be Sheet2.
But shprv will have no value since there is no sheet before Sheet1.
Related
please let me know if I need to be more specific. I am entering a weeknum() formula on my workbook isum on sheet stock. I want this formula to be evaluated for everything in the "X" column starting at row 2 since the top row will be a label. I used a macro recorder, but I am getting an error 1004 because my "select method of range class failed". There is probably a better way of writing this code. After, I want to make any weeknum value that is less than 27 in this column "Past". isum is my workbook and stock is my sheet in this workbook. I just made the range up to a certain amount rather than the used amount that I want to have. Here is my code so far: isum.Sheets("stock").Range("X2").Select
ActiveCell.FormulaR1C1 = "=WEEKNUM(RC[-9])"
Selection.AutoFill Destination:=Range("X2:X900").Select
Range("X2:X900").Select
Your code could be simplified to:
'Declare variables
dim wb as Workbook
dim ws as Worksheet
dim rng as Range
'Set variables
set wb = Workboooks("isum.xlsx") 'check the file extension & name is correct
set ws = wb.Worksheets("stock")
set rng = ws.range("X2:X900")
'Fill range with formula
rng.FormulaR1C1 = "=WEEKNUM(RC[-9])"
(Link - Here's a great reference on how/why to avoid using SELECT in VBA)
Edit: another way to set the workbook:
set wb = ThisWorkbook
I have been trying to get this to work, but I am not sure how to get it to work, any help would be appreciated.
I have 2 workbooks, workbook 1 has multiple sheets, each one labelled with a different name. Workbook 2 has one summary sheet with a column of values for each individual.
What I am trying to achieve is:
on workbook 1 check the sheet name
switch to workbook 2 and find the column with the same name. All the names are on row 6, from column I to DD. Also, each name is in 2 cells merged together, I don't know if this affects it.
Once the name on row 6 is found, I want it to go down 6 cells, and copy the value.
switch back to workbook 1 and paste it into cell B37.
Repeat this process but this time go down 7 cells, copy the value and paste it into cell B102 OF Workbook 1. I have about 30 cells to copy and past like that.
once complete repeat everything again for the next sheet on workbook 1.
Another Important issue is that, not all sheet names on workbook 1 exists on workbook 2, I have a feeling the below code will throw an error as soon as it doesn't find a match. So I would like to be able to skip the sheets on workbook 1 that it doesn't find a matching name for on workbook 2 summary sheet.
I have put the code I have below, but I keep getting the error "Method or data member not found"
Sub Measures()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Dim wb2 As Workbook
Dim cell As Range
Dim ws As Worksheet
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("November Stream 1.xlsm")
Set Sht = wb1.Worksheets("Summary")
Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Column.Count, "A").End(xlUp).Column)
For Each cell In Rng
Set ws = wb2.Sheets(cell.Text)
ws.Range("B37").Value = cell.Offset(6, 0).Value
ws.Range("B102").Value = cell.Offset(7, 0).Value
Next cell
End Sub
Thank you for any help!
I have this problem with an excel file.
I have in this file 200 sheets in which always in the same cells (range O2:O6) I have certain values to copy in a unique sheet (named "Final") in the same column C.I
I would like also to order this values that I copy and paste; I would (maybe) use the command if else to understand the order: in fact in cell A1 of all the sheets there is the name of reference of that sheet (some sheets have name A, others B, others C);I must put in order in the sheet Final, before the values of sheets with name A then values with name B and then values of sheets with name C. so in practice I would do, "If (in the cell A1 of each sheet is present letter A) then (copy the relative values)"
then all values of sheets with name B in that cell, and then name C.
Could you help me?
This will get you started, it loops through the sheets and copies the range to "Final" sheet.
It does not copy the sheets in order, you can use my example and create a loop that will do that.
Sub Do_Something()
Dim sh As Worksheet
Dim ws As Worksheet
Set ws = Sheets("Final")
With ws
For Each sh In Sheets
If sh.Name <> ws.Name Then
sh.Range("O2:O6").Copy .Cells(.Rows.Count, "C").End(xlUp).Offset(1)
End If
Next sh
End With
End Sub
But my problem is the order, to copy is ok. i would want to copy that range but before from some sheet and then from others, regarding the name contained in that cell
Within an Excel workbook I have 5 specific worksheets (different names) that I want concatenate the data from into a different worksheet (master) within the same workbook. Simply taking the data from each sheet and appending it to the bottom of the data in the "Master" sheet. Also removing blank rows if possible. Is there a macro that can do this?
There are several choices. If you only need to do this once, don't bother using a macro. Simply go to each sheet, copy the rows, move to the master sheet, scroll down to the first empty row, and paste.
Assuming that you actually need a macro for this, something like this might work:
Sub CombineSheets()
Dim Wksht As Worksheet, MasterSht As Worksheet, R As Integer
Set MasterSht = Worksheets.Add
R = 0
For Each Wksht In ThisWorkbook.Worksheets
If Not Wksht Is MasterSht Then
Wksht.UsedRange.Copy Destination:=MasterSht.Cells(R + 1, 1)
R = MasterSht.UsedRange.Rows.Count
End If
Next Wksht
End Sub
Cells(4, x) = Application.WorksheetFunction.COUNTA(Workbooks(""DB_Report.xls"").Sheets(x).Range(A:A))
I am trying to get the above function to work.
I am calling the script from the workbook DB_report.xls
This creates a new workbook ("month") and starts filling in the values.
What I am trying to get to is where
cell 4,1 in months has the counta of sheet 1 from DB_report
cell 4,2 in months has the counta of sheet 2 from DB_report
Can anyone reword the line above so when "months is the active work sheet I can call counta from DB_Report
The line before this is
NameSH = Workbooks("DB_Report.xls").Sheets(x).Name and this works fine and returns the name of work sheet x
Thanks
Aaron
Ok for a bit further explicanation
the steps I want to do go some thing like this
select workbook months.xls
select sheet(1)
cell (x,y) = counta( of range A:A , in worksheet("DB_Report") of worksheet (DB_report.xls)
Now I know
Cells(4, x) = Application.WorksheetFunction.COUNTA(sheet(3).range(a:A)
will work with in the active work sheet. So if the active sheet is sheet 1 then that would count up he numbe of cells in sheet 3 of the same workbook. I wanted to know if as well as refrenced sheet and cells in the function I can also refrence a workbook by name.
of course i could swqap to book "DB_Report" save the value to a varible and then swap back to book "Month" and copy it to the cell.
or could I do workbook("month").sheet(y).cells(a,b) = Application.WorksheetFunction.COUNTA(sheet(3).range(a:A)
while in workbook "month"
so really what i need is how do you refrence workbook,sheet and cells all with in a function?
I don't think this is exactly what you were trying to do, but it comes close and is a bit more generalized. It counts up the worksheets in what would be DB_Report.xls and uses that to specify that number of cells in months.xls
If you are running the macro from the DB_Report.xls you don't need to specify anything about that workbook or sheets.
Sub counts()
Dim sheetcounts As Integer
Dim countas() As Integer
Dim index As Integer
Dim wksht As Worksheet
Dim newbook As Workbook
sheetcounts = ActiveWorkbook.Sheets.Count
ReDim countas(sheetcounts)
For Each wksht In ActiveWorkbook.Sheets
countas(index) = Application.WorksheetFunction.CountA(wksht.Range("A:A"))
index = index + 1
Next
Set newbook = Workbooks.Add
newbook.Activate
newbook.ActiveSheet.Range(Cells(4, 1), Cells(4, sheetcounts)) = countas
newbook.SaveAs ("months.xls")
End Sub
It will require any error checking or verification that you need to put into it.
Hi Cheers for the comments, but i finaly worked out what the problem was.
it was simple really I was just missing some formatting
the line below works correctly
cell(x,y) = Application.WorksheetFunction.CountA(Workbooks("DB_Report.xls").Sheets(x).Range("A:A"))