I am struggling with fine tuning my VBA for an automatic generated worksheet menu. So far I have the following;
Dim objSheet As Worksheet
Worksheets("General Information").Activate
Range("W14").Select
For Each objSheet In ActiveWorkbook.Worksheets
If ActiveSheet.Name <> objSheet.Name Then
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & objSheet.Name & "'" & "!A1", TextToDisplay:=objSheet.Name
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireColumn.AutoFit
End If
Next objSheet
This works perfectly. However, I would like to have a fixed starting point as the first few sheets are always the same.
In other words I need this to work as of the 12th worksheet onwards.
Can you please help me out with this addition, thanks
Try like this:
Const intSheetStart As Integer = 12
Dim intCount As Integer
Dim objSheet As Worksheet
Worksheets("General Information").Activate
Range("W14").Select
intCount = 0
For Each objSheet In ActiveWorkbook.Worksheets
intCount = intCount + 1
If intSheetStart <= intCount Then
If ActiveSheet.Name <> objSheet.Name Then
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & objSheet.Name & "'" & "!A1", TextToDisplay:=objSheet.Name
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireColumn.AutoFit
End If
End If
Next objSheet
This change sets the starting point in a constant, then counts the sheets and only runs the rest of the code if the current sheet is the starting point or after.
A different way of controlling it, is using another kind of loop, and set a starting point in the code.
For i = 12 To ActiveWorkbook.Worksheets.Count
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & Worksheets(i).Name & "'" & "!A1", TextToDisplay:=Worksheets(i).Name
ActiveCell.Offset(1).Select
ActiveCell.EntireColumn.AutoFit
Next i
This starts the count at the 12th sheet as noted by i= 12 and then goes to the last sheet Worksheets.Count.
Also it's generally good practice to not select things, so here is a version without selecting a cell:
Sub links()
Dim ws As Worksheet
Set ws = Worksheets("General Information")
For i = 12 To ActiveWorkbook.Worksheets.Count
ws.Hyperlinks.Add Anchor:=ws.Cells(i + 2, 23), Address:="", SubAddress:="'" & Worksheets(i).Name & "'" & "!A1", TextToDisplay:=Worksheets(i).Name
Next i
Cells(1, 23).EntireColumn.AutoFit
End Sub
And for a more dynamic approach, as inspired by sbgib:
Sub menu()
Dim ws As Worksheet, printRow As Long, startCol As Long
Const startSheet As Long = 12 '- Which sheet number to start from
printCol = 23 '- Column "W" is column number 23
printRow = 14 '- First row to add hyperlinks to
Set ws = Worksheets("General Information")
printRow = printRow - startSheet
For i = startSheet To ActiveWorkbook.Worksheets.Count
ws.Hyperlinks.Add Anchor:=ws.Cells(printRow + i, printCol), Address:="", SubAddress:="'" & Worksheets(i).Name & "'" & "!A1", TextToDisplay:=Worksheets(i).Name
Next i
Cells(1, printCol).EntireColumn.AutoFit
End Sub
Related
I need to fill down a value in Sheet 1 Cell A2 with =Sheet 2 Cell A2 until the linked value is blank. I dont really know what to do.
I got so far so that I only need zeros in the fields:
Sub Test1()
Dim x As Integer
Dim i As Integer
Dim wsh As Worksheet
Set wsh = Worksheets("List with Weights")
Application.ScreenUpdating = False
i = 2
While (wsh.Cells(i, 1)) <> ""
wsh.Cells(i, 1).FormulaR1C1 = "='Sample Weight'!RC[0]"
wsh.Cells(i, 2).FormulaR1C1 = "='Sample Weight'!RC[0]"
wsh.Cells(i, 3).FormulaR1C1 = "='IS Weight'!RC[-1]"
i = i + 1
Wend
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select
Columns("A:C").Select
Columns("A:C").EntireColumn.AutoFit
Application.ScreenUpdating = True
Range("A1").Select
End Sub
If I have understood you correctly, there is no need for a loop. Is this what you are trying?
Option Explicit
Sub Sample()
Dim wsThis As Worksheet
Dim wsThat As Worksheet
Dim wsOther As Worksheet
'~~> Set your relevant worksheets
Set wsThis = ThisWorkbook.Sheets("List with Weights")
Set wsThat = ThisWorkbook.Sheets("Sample Weight")
Set wsOther = ThisWorkbook.Sheets("IS Weight")
'~~> Find the last row in Col A of Sample Weight worksheet
Dim wsThatLRow As Long
wsThatLRow = wsThat.Range("A" & wsThat.Rows.Count).End(xlUp).Row
'~~> Insert the formula in 1 go in the relevant range
With wsThis
.Range("A2:A" & wsThatLRow).Formula = "='" & wsThat.Name & "'!A2"
.Range("B2:B" & wsThatLRow).Formula = "='" & wsThat.Name & "'!B2"
.Range("C2:C" & wsThatLRow).Formula = "='" & wsOther.Name & "'!B2"
End With
End Sub
I'm trying to copy all rows with data from one sheet into another.
I get a runtime error at the selection.paste line.
Sub move_rows2()
Dim i As Integer, countSheets As Integer, rowCount As Integer
countSheets = Application.Sheets.Count
For i = 1 To countSheets
Worksheets(i + 1).Select
Range("A" & Rows.Count).End(xlUp).Select
rowCount = ActiveCell.Row
Rows("1:" & rowCount).Select
Range("A" & rowCount).Activate
Selection.Copy
Worksheets(1).Select
Range("A" & Rows.Count).End(xlUp).Select
Selection.Offset(2, 0).Select
Selection.Paste
Next i
End Sub
Without the select/activate, and using an explicit workbook reference.
Dim i As Long, wb As Workbook
Set wb = ActiveWorkbook
For i = 2 To wb.Sheets.Count
With wb.Sheets(i)
.Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row).Copy _
wb.Sheets(1).Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0)
End With
Next i
I'm trying (and failing) to get some code to run on each worksheet except one specific sheet. I want the code to just cut the data in cells n2:s2 and paste it in t1:y1, then repeat for any other rows that have data in columns n3:s3, n4:s4, n5:s5.
Once there is no data (row 6 i believe), it should move onto the next sheet (except "Report" sheet).
The problem i'm facing when i debug is it moves the data as expected, then starts again on the same sheet, so overwrites data with empty cells.
Sub MovethroughWB()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'This statement starts the loop
If ws.Name <> "Report" Then 'Perform the Excel action you wish (turn cell yellow below)
Range("N2:S2").Select
Selection.Cut Destination:=Range("T1:Y1")
Range("T1:Y1").Select
Range("N3:S3").Select
Selection.Cut Destination:=Range("Z1:AE1")
End If
Next ws
End Sub
I'm sure its something basic, but can't find what!
Try:
Sub MovethroughWB()
Dim ws As Worksheet
Dim i As Long, Lastrow As Long, Lastcolumn As Long
For Each ws In ThisWorkbook.Worksheets 'This statement starts the loop
If ws.Name <> "Report" Then 'Perform the Excel action you wish (turn cell yellow below)
With ws
Lastrow = .Cells(.Rows.Count, "N").End(xlUp).Row
For i = 2 To Lastrow
If .Range("N" & i).Value <> "" And .Range("O" & i).Value <> "" And .Range("P" & i).Value <> "" _
And .Range("Q" & i).Value <> "" And .Range("R" & i).Value <> "" And .Range("S" & i).Value <> "" Then
If .Range("T1").Value = "" Then
.Range("N" & i & ":S" & i).Cut .Range("T1:Y1")
Else
Lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
.Range("N" & i & ":S" & i).Cut .Range(.Cells(1, Lastcolumn), .Cells(1, Lastcolumn + 5))
End If
End If
Next i
.Rows("2:" & Lastrow).EntireRow.Delete
End With
End If
Next ws
End Sub
My current VBA provides an sum function for pre-defined columns within worksheets which are specified and defined in the code. This works fine, however I am adding new worksheets to this workbook on a daily basis, and its therefore not feasible to edit the code everyday to add a new worksheet and range for it to sum.
Is there a way I can edit my current code in order for it to conduct the sum function for every single worksheet in the workbook? I have attached the current code for reference below.
Sub AutoSum()
Sheets("MASTER ACCOUNT REVENUE").Select
Range("D4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(2, 0).Select
Dim cel1 As String, cel2 As String
cel1 = ActiveCell.Offset(-2, 0).End(xlUp).Address
cel2 = ActiveCell.Offset(-1).Address
ActiveCell.Value = "=sum(" & (cel1) & ":" & (cel2) & ")"
End Sub
Yes, just loop through the sheets. NOTE: It's best to avoid using .Select/.Activate
Sub autoSum_AllSheets()
Dim ws As Worksheet
Dim cel1 As String, cel2 As String
Dim firstCel As Range
For Each ws In ActiveWorkbook.Worksheets
With ws
Set firstCel = .Range("D4").End(xlDown).Offset(2, 0)
cel1 = firstCel.Offset(-2, 0).End(xlUp).Address
cel2 = firstCel.Offset(-1).Address
firstCel.Value = "=SUM(" & cel1 & ":" & cel2 & ")"
End With
Next ws
End Sub
Note: I'm aware of the redundencies in the Offset() parts, but just kept them in for OP to see how to more easily avoid .Select/.Activate.
Edit: To loop through a bunch of columns, one (albeit kludgy) way is to just add the column letters to an array:
Sub autoSum_AllSheets()
Dim ws As Worksheet
Dim cel1 As String, cel2 As String
Dim firstCel As Range
Dim cols() As Variant
cols = Array("D", "E", "F")
Dim i As Long
For Each ws In ActiveWorkbook.Worksheets
With ws
For i = LBound(cols) To UBound(cols)
Set firstCel = .Range(cols(i) & "4").End(xlDown).Offset(2, 0)
firstCel.Select
cel1 = firstCel.Offset(-2, 0).End(xlUp).Address
cel2 = firstCel.Offset(-1).Address
firstCel.Value = "=SUM(" & cel1 & ":" & cel2 & ")"
Next i
End With
Next ws
End Sub
Please note though, if the Column does not have any information in a cell after row 5, you will get an error (because the .XlDown goes to the very last row, and you can't then Offset(2,0) from there.)
Yes add:
Dim wscount as long
dim i as long
wscount = Activeworkbook.Worksheets.Count
for i = 1 to wscount
Sheets(i).Select
Range("D4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(2, 0).Select
Dim cel1 As String, cel2 As String
cel1 = ActiveCell.Offset(-2, 0).End(xlUp).Address
cel2 = ActiveCell.Offset(-1).Address
ActiveCell.Value = "=sum(" & (cel1) & ":" & (cel2) & ")"
next i
End Sub
I am looking for some help on this issue. I am using VBA in Excel 2007 to consolidate tables from five separate worksheets onto one worksheet. This works great in a stand alone workbook, but I need to move this into a workbook with other tabs that I will not consolidate data from. I tried creating an array as a varaible which included the five workbooks I need to consolidate, but I could not get it to work.
Here is the code I am using as a separate process that works:
Sub SummariseData()
Dim x As Long, llastrow As Long, lfirstrow As Long
Range("Data").CurrentRegion.Offset(1, 0).ClearContents
Application.ScreenUpdating = False
For x = 1 To Sheets.Count
If Sheets(x).CodeName <> "Sheet1" Then
If Sheets(x).Range("A2") <> "" Then
lfirstrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row + 1
llastrow = Sheets(x).Range("A1").End(xlDown).Row
Sheets(x).Range("A2:N" & llastrow).Copy Destination:=Sheet1.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Sheet1.Range("A" & lfirstrow & ":A" & lfirstrow + llastrow - 2) = Sheets(x).Name
End If
End If
Next x
End Sub
Can anoyone help with how to write the code to specifically call the the five tabs I need to consolidate, instead of loop through all tabs in the workbook? The tab names are T1, T2, T3, T4, T5 all consolidaing to a tab call Summary.
You can try this:
Dim sh As Worksheet, ws As Worksheet
Set ws = Thisworkbook.Sheets("Summary")
For Each sh in Thisworkbook.Sheets(Array("T1","T2","T3","T4","T5"))
If sh.Range("A2").Value <> "" Then
sh.Range("A1",sh.Range("A" & Rows.Count).End(xlUp).Offset(0,14).Address).Copy _
ws.Range("B" & Rows.Count).End(xlUp).Offset(1,0)
ws.Range(Range("A" & Rows.Count).End(xlUp).Offset(1,0).Address, _
Range("B" & Rows.Count).End(xlUp).Offset(0,-1).Address).Value = sh.Name
End If
Next
Not tested though so i leave it to you.
Try to replace with this :
Sub SummariseData()
Dim x As Long, llastrow As Long, lfirstrow As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Summary")
Range("Data").CurrentRegion.Offset(1, 0).ClearContents
Application.ScreenUpdating = False
For x = 1 To 5
sheetName = "T" & x
Set ws2 = Worksheets(sheetName)
If ws2.Range("A2") <> "" Then
lfirstrow = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
llastrow = ws2.Range("A1").End(xlDown).Row
ws2.Range("A2:N" & llastrow).Copy Destination:=ws1.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
ws1.Range("A" & lfirstrow & ":A" & lfirstrow + llastrow - 2) = sheetName
End If
Next x
End Sub
Details on what I changed :
I used worksheet objects instead of calling the worksheet by its name each time (ws1 for the worksheet TO which you're copying, and ws2 for the worksheet FROM which you're copying.
I used a loop that goes from 1 to 5, to concatenate that value to a T, in order to get the tab name.