Let me preface this question by saying I am brand new to VBA…
I am trying to loop a macro through selected worksheets in an Excel workbook. I am currently trying this on a small scale with the following example. I have created 8 worksheets, named A, B, C, D, E, F, G, and H. I have entered these names in cells A1:A8 on worksheet H, and I have given the range of cells A3:A6 (containing C-F) the name Beta. For this simple exercise, I want to run a macro that will insert the letter X in cell C1 only on the worksheets included in the named range (C-F).
I have looked at several different questions and examples related to VBA loops and have tried taking bits and pieces from them to perform this function. Here is my latest attempt:
Sub
Dim wksht As Worksheet
Dim i As String
Dim Beta() 'This is an array definition
i = C
For Each wksht In ActiveWorkbook.Worksheets
i = i + 1
ReDim Preserve Beta(C To i)
Beta(i) = wksht.Name
Next wksht
For i = LBound(Beta) To UBound(Beta)
wksht.Range("C1").Value = X
Next i
End Sub
Sidenote: The function I actually want to perform in selected worksheets is regression analysis. Where in the loop code would I insert this command?
I would greatly appreciate any guidance you experts out there could provide. Thanks!
I am trying to loop a macro through selected worksheets in an Excel workbook. I am currently trying this on a small scale with the following example. I have created 8 worksheets, named A, B, C, D, E, F, G, and H. I have entered these names in cells A1:A8 on worksheet H, and I have given the range of cells A3:A6 (containing C-F) the name Beta. For this simple exercise, I want to run a macro that will insert the letter X in cell C1 only on the worksheets included in the named range (C-F).
I have looked at several different questions and examples related to VBA loops and have tried taking bits and pieces from them to perform this function. Here is my latest attempt:
Sub
Dim wksht As Worksheet
Dim i As String
Dim Beta() 'This is an array definition
i = C
For Each wksht In ActiveWorkbook.Worksheets
i = i + 1
ReDim Preserve Beta(C To i)
Beta(i) = wksht.Name
Next wksht
For i = LBound(Beta) To UBound(Beta)
wksht.Range("C1").Value = X
Next i
End Sub
Sidenote: The function I actually want to perform in selected worksheets is regression analysis. Where in the loop code would I insert this command?
I would greatly appreciate any guidance you experts out there could provide. Thanks!