I have a named range compiled of 30 string values. Each of the string values is also used as the name of a sheet. I was wondering if it would be possible to loop through each string in the range to edit it's respective sheet. I've gone through some trial and error but am still stuck. Any help would be greatly appreciated.
Option Explicit
Sub NamedRanges()
Dim ws As Worksheet
Dim rng As Variant
rng = Sheets("Teams").Range("TeamList_Full")
For Each ws In rng
ws.Range("A36").Value2 = 2
Next ws
End Sub
You can do it like this:
Sub NamedRanges()
Dim c As Range
For Each c In ThisWorkbook.Worksheets("Teams").Range("TeamList_Full").Cells
ThisWorkbook.Worksheets(c.Value).Range("A36").Value2 = 2
Next c
End Sub
Since you appear to be targeting the same cell on each sheet, you can also achieve your aim by avoiding a loop
Sub NamedRanges()
Dim sh As Worksheet, rng As Range
Set rng = Worksheets("Teams").Range("TeamList_Full")
Set sh = Worksheets(rng.Cells(1).Value2)
sh.Range("A36").Value2 = 2
Worksheets(WorksheetFunction.Transpose(rng.Value2)).FillAcrossSheets sh.Range("A36")
End Sub
Related
I'm trying to put together a very simple VBA script to clear cell contents within a specified table column range ([Front Straddle]:[Front Option]), of all tables within a specified worksheet. This script will only live within the "VolJump" worksheet, which contains an arbitrary number of identically formatted, differently named tables. Because of this, I felt the best approach was to reference the tables by the index number.
This is where I'm running into issues with the proper referencing/nesting within the 'Range' function, shown below. Any help is greatly appreciated.
Sub ClearCells()
Dim i As Long
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("VolJump")
If sh.ListObjects.Count > 0 Then
For i = 1 To sh.ListObjects.Count
Range("Activesheet.ListObjects(1)[[Front Straddle]:[Front Option]]").Select
Selection.ClearContents
Next i
End If
End Sub
Clear Contents of Table Columns Range
Option Explicit
Sub ClearCells()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("VolJump")
Dim tbl As ListObject
For Each tbl In ws.ListObjects
ws.Range(tbl.Name & "[[Front Straddle]:[Front Option]]").ClearContents
Next tbl
End Sub
Just using the ListObjects:
Sub ClearColumns()
Dim lo As ListObject
Dim ColNum1 As Long, ColNum2 As Long
For Each lo In ThisWorkbook.Worksheets("Sheet1").ListObjects
'Get the index numbers of the start and end columns.
ColNum1 = lo.ListColumns("Front Straddle").Index
ColNum2 = lo.ListColumns("Front Option").Index
'Resize the range from the start column to the end column and clear it.
lo.DataBodyRange.Columns(ColNum1).Resize(, ColNum2 - ColNum1 + 1).ClearContents
Next lo
End Sub
I managed to copy cell formats within the same sheet by using
Dim c As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If c Then Exit Sub
c = True
f = Mid(Target.Formula, 2)
Range(f).Copy
Target.PasteSpecial xlPasteAllUsingSourceTheme
c = False
End Sub
But in another sheet, I use =INDEX(Sheet1!$I$1:$J$255,MATCH(A4,Sheet1!$F$1:$F$255,0),1) to get the value from Sheet1.
Now how can I copy the source format just like the cells in the same sheet would!?
I had tried everything in Google Search to no avail!
I even tried to make my own function and still failing to do so!!!
'(One variation)
Function CopyFrom(ByVal Cell)
Dim r As Range
Set r = Worksheets(Cell.Parent.Name).Range(Cell.address(External:=False))
CopyFrom = r.Value2
r.Copy
Application.Caller.PasteSpecial xlPasteAllUsingSourceTheme
End Function
I'm kind of at the end of my rope now!
Somebody PLEASE be so kind and teach me how to do it!
Much appreciated!!!
Assuming the cell you want to copy is A1 on sheet called "another sheet".
Assuming you want to paste in cell A1 on sheet called "Sheet1".
option explicit
dim wb as workbook
dim wsSource as worksheet, wsDest as worksheet
dim Target As Range
Sub test()
set wb = thisworkbook
set wsSource = wb.Sheets("another sheet")
set wsDest = wb.Sheets("Sheet1")
Set Target = wsDest.Range("A1")
wsSource.Range("A1").Copy
Target.PasteSpecial xlPasteAllUsingSourceTheme
End Sub
I am having a lot of trouble with this one. I want to replicate this value in VBA:
=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))
I am having an impossible time trying to get this done.
If the three ranges are in the same sheet then:
Sub MaxMax()
Dim r As Range
Set r = Union(Range(Named_Range1), Range(Named_Range2), Range(Named_Range3))
MsgBox Application.WorksheetFunction.Max(r)
End Sub
If you don't want to place the formula in one of the cells and calculate the result in VBA code, you can also use the following code:
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim y As Double
Set rng1 = ThisWorkbook.Names("Named_Range1").RefersToRange
Set rng2 = ThisWorkbook.Names("Named_Range2").RefersToRange
Set rng3 = ThisWorkbook.Names("Named_Range3").RefersToRange
y = WorksheetFunction.Max(rng1, rng2, rng3)
If named ranges are in another workbook, replace ThisWorkbook with Workbooks("workbook name")
For the result to be placed in F9, please try:
Sub Maximal()
Range("F9").Formula = "=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))"
End Sub
Here's what I have so far:
Sub TrimColumnD()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim c As Range
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
c.Value = WorksheetFunction.Trim(c.Value)
Next c
Next ws
End Sub
The trim function only works on the cells in the first worksheet.
Please change this line:
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
into this one:
For Each c In ws.UsedRange.Columns("D").Cells
In your code internal loop refers to activesheet while it should refer to ws variable representing sheet.
Create a VBA loop to re-size all of the tables in an Excel project.
A list of table names is stored in the "Tlist" named range
This code all works if the table names are written in.
The code works in this loop for the column count, but on the second reference of x , the re-size, I get an error of 'subscript out of range'
Hovering over the second x on debug, Excel seems to read it in right, but I can't get it to re-size the table Something to do with the list object function not supporting the text in the loop? Or am I doing this loop wrong and need to define x differently? Any help greatly appreciated.
Sub RSizeTables()
Dim rr As Integer
Dim cc As Integer
Dim x As Range
Dim £Table As Range
Set £Table = Range("Tlist")
For Each x In £Table
rr = 2
cc = Range(x).Columns.Count
With Sheets("Data").ListObjects(x)
.Resize .Range.Resize(rr, cc)
End With
Next x
End Sub
This code will resize all tables to two rows:
Sub ResizeAll()
Dim ws As Worksheet
Dim lo As ListObject
'If you only have tables in one sheet, use this
Set ws = Sheets("Data")
'Else this:
'For Each ws in Sheets
For Each lo In ws.ListObjects
lo.Resize lo.Range.Resize(2)
Next lo
'Next ws
End Sub
If you need to loop only the tables in TList, this will do the job:
Sub ResizeTList()
Dim varTableName As Variant
For Each varTableName In Range("TList")
With Sheets("Data").ListObjects(varTableName)
.Resize .Range.Resize(2)
End With
Next varTableName
End Sub