I have three looping procedures that use a few sheets and a few ranges. I'd like to set this up so I can change the sheet names and ranges in one single place, rather than make the changes in three different places. Is that possible?
Now, I'm doing it like this.
' Loop One
Sub TestMe()
sheet_in = "Model In"
sheet_out = "Model Out"
range_in = "E10:AB300"
range_out = "E2"
Set sheet_in = Sheets(sheet_in).Range(range_in)
Set sheet_out = Sheets(sheet_out).Range(range_out)
row_count = Sheets(sheet_in).Range(range_in).Rows.Count
col_count = Sheets(sheet_in).Range(range_in).Columns.Count
For j = 1 to col_count
For i = 1 to row_count
range_out.Cells(K , 1).Value = range_in.Cells(i, j).Value
Next i
Next j
End Sub
I feel like there's go to be a better way to do this.
Consider defining constants in a dedicated module:
Public Const WS_SHEET_IN As String = "Model In"
Public Const RNG_SHEET_IN As String = "E10:AB300"
Public Const WS_SHEET_OUT As String = "Model Out"
Public Const RNG_SHEET_OUT As String = "E2"
In your regular code:
Sub TestMe()
' etc., etc.
row_count = Worksheets(WS_SHEET_IN).Range(RNG_SHEET_IN).Rows.Count
End Sub
As always though "it depends" is the answer to what is the "best" way to organize your code.
For example:
Name your worksheet code module and use that name directly instead of referring to the tab name:
InSheet.Range(RNG_SHEET_IN)
OutSheet.Range(RNG_SHEET_OUT)
Use properties or functions to return commonly-accessed objects:
Function InRange() As Range
Set InRange = thisworkbook.worksheets(WS_SHEET_IN).Range(RNG_SHEET_IN)
End function
Etc etc
Related
Is there anyway that I can put various classes into an array so that instead of hard-coding like:
set var1 = new cls1
set var2 = new cls2
we can do:
varArr = array ( var1, var2...)
clsArr = array (cls1, cls2...)
and now I just loop thru varArr and clsArr to set new class accordingly, like:
sub setNew(x as double)
set varArr(x) = new clsArr(x)
end sub
I did try but it didn't work, please help me out
Thank you very much !
Triet (mr)
I try, but my knowledge also limited.
First of all I do not think you can put class into an array. You can put objects. I also failed to use the array() function.
Please see how I proceeded with some demonstration:
This is the class module, containing a value, the property and a primitive print sub:
Private nr As Integer 'Use as identification
Sub cPrint()
MsgBox "Object ID: " & nr
End Sub
Property Let ID(v As Integer)
nr = v
End Property
In the module in the first cycle I create the object, put the ID value. In the second one all ID values are printed. I hope you can use it for your purposes.
Sub clsTest()
Dim varArr(1 To 5) As cls1
Dim i As Integer
For i = 1 To 5
Set varArr(i) = New cls1
varArr(i).ID = i
Next
For i = 1 To 5
varArr(i).cPrint
Next
End Sub
I have declared constants like the below in the module,
Public Const strFolderA1 = "C:\ABCD\One"
Public Const strFolderA2 = "C:\ABCD\two"
I am trying to call this in a loop,
For i = 1 To 3
strFile = Dir(strFolderA & i & "\" & filenm)
Loop
The above Dir code is wrong, but I want to call the constant based on the looping integer.
Could someone help?
Please let me know if the question is not clear.
VBA does not provide any method for concatenating a string to be used as a dynamic variable name. You could create a string constant with a delimiter then split it before use.
Option Explicit
Public Const strFolderA As String = "C:\ABCD\One|C:\ABCD\Two|C:\ABCD\Three"
Sub abcs()
Dim i As Long, fldrs As Variant
fldrs = Split(strFolderA, "|")
For i = LBound(fldrs) To UBound(fldrs)
Debug.Print fldrs(i)
Next i
End Sub
It is very simple but yet I can't figure it out. Maybe because it cannot be done? Regardless here we go:
I would like to use a combobox that will, when selected, input cells with text values, functions and reference to external cells.
First line of the options would be to have the name populated.
Second line is a formula that would change from course to course.
Third line would provide a cell with a reference to another cell's content from another file. So if multiple course file are used I can have one master file that if I change the content of a cell the change will reflect on all the course file cells that are referring to it once updated.
This is in crude code form what I would like it to perform.
Private Sub ComboBox1_Change()
If Me.ComboBox1.Value = "ITCourse" Then
Worksheets("PARADE STATE").Range("I1").Value = "ITCourse"
Worksheets("Data Base").Range("C1").Value = IF(V9>70,"Prep Week",IF(V9>65,"Week 1",IF(V9>60,"Week 2",IF(V9>55,"Week 3",IF(V9>50,"Week 4",IF(V9>45,"Week 5",IF(V9>40,"Week 6",IF(V9>35,"Week 7",IF(V9>30,"Week 8",IF(V9>25,"Week 9",IF(V9>20,"Week 10",IF(V9>15,"Week 11",IF(V9>10,"Week 12",IF(V9>5,"Week 13",IF(V9>0,"Week 14")))))))))))))))
Worksheets("Week 1").Range("B2").Value = 'N:\ITcourse\00 - Data Base\[ITcourse.xlsx]Sheet'!$A$3
End If
If Me.ComboBox1.Value = "HRCourse" Then
Worksheets("PARADE STATE").Range("I1").Value = "HRCourse"
Worksheets("Data Base").Range("C1").Value = IF(V9>40,"Prep Week",IF(V9>35,"Week 1",IF(V9>30,"Week 2",IF(V9>25,"Week 3",IF(V9>20,"Week 4",IF(V9>15,"Week 5",IF(V9>10,"Week 6",IF(V9>5,"Week 7",IF(V9>5,"Week 8")))))))))
Worksheets("Week 1").Range("B2").Value = 'N:\ITcourse\00 - Data Base\[HRcourse.xlsx]Sheet'!$A$3
End If
End Sub
Thank you!
You need a function that returns the number of weeks for any given course name. This function should use a Dictionary to store the information, and the dictionary may be loaded from a dedicated worksheet.
Function WeeksPerCourse(courseName As String) As Long
Static dict As Scripting.Dictionary
If dict Is Nothing Then
' Fill the dictionary here. Note that it is better
' to load the values from a dedicated, hidden worksheet
Set dict = CreateObject("Scripting.Dictionary")
dict("ITCourse") = 14
dict("HRCourse") = 8
' Etc...
End If
WeeksPerCourse = dict(courseName)
End Function
With this function available, your procedure can be simplified like follows:
Private Sub ComboBox1_Change()
Dim course As Sting: course = Trim(ComboBox1.value)
Worksheets("PARADE STATE").Range("I1").value = course
'Dim nWeek As Long
'nWeek = WeeksPerCourse(course) - Worksheets("PARADE STATE").Range("V9").value / 5
'Worksheets("Data Base").Range("C1").value = IIf(nWeek < 1, "Prep Week", "Week " & nWeek)
Worksheets("Data Base").Range("C1").Formula = "= ""Week "" & INT((WeeksPerCourse('PARADE STATE'!I1) - 'PARADE STATE'!V9)/5)"
Worksheets("Week 1").Range("B2").Formula= "='N:\ITcourse\00 - Data Base\[" & course & ".xlsx]Sheet'!$A$3"
End Sub
I have these
Public Const WS1 = "SheetNameA"
Public Const WS2 = "SheetNameB"
Public Const WS3 = "SheetNameC"
Public Const WS4 = "SheetNameD"
Public Const WS5 = "SheetNameE"
Public Const WS6 = "SheetNameF"
Where SheetNameA-B-C are the names of sheets in a workbook.
And I would like to activate worksheets one after the other using a For loop 1 to 6like so:
For i = 1 to 6
Workbooks([wbkname]).Sheets(WS & i).Activate
[some more code doing stuff]
Next i
Now I know that doesn't work, I'm just trying to explain what I want.
Is that possible? To call a public constant by a concatenated name (to make that public constant string the name of the sheet we're activating)?
I hope that makes sense.
Edit: On second thought, maybe I'll just do a Select Case, which I know is not pretty, but I think, will be fine for my purpose.
For i = 1 to 6
Select Case i
Case 1
somevariable = [name of one sheet]
Case 2
somevariable = [name of another sheet]
....
End Select
Workbooks([wbkname]).Sheets(somevariable).Activate
Just use an array. First run MAIN(), then run marine():
Public WS(1 To 6) As String
Sub MAIN()
WS(1) = "SheetNameA"
WS(2) = "SheetNameB"
WS(3) = "SheetNameC"
WS(4) = "SheetNameD"
WS(5) = "SheetNameE"
WS(6) = "SheetNameF"
End Sub
Sub marine()
For i = 1 To 6
Workbooks("Book1").Sheets(WS(i)).Activate
[some more code doing stuff]
Next i
End Sub
I'm not aware of an "indirect" function in VBA, what you can do is to define a public array and store the values there, here you can get the values by index. As its not possible to define array constants in VBA you need to initialize your array before use (e.g. In the workbook's load event).
I'm programming a Makro for a Excel xy-Diagramm
The diagramm is nearly correct, but i have dublicate DataSeriesNames;
I already tried to go through all Series and Compare the Names.
There were no Errors, but it didn't work.
Code was like that:
For a=1 to Count
If ActiveChart.SeriesCollection(Count).Name = Range("A2").Value Then
Name = true
End If
a = a + 1
Next
If Name = false Then
ActiveChart.SeriesCollection.NewSeries
End If
ActiveChart.SeriesCollection(Count).Name = "='Tasks'!$D$" & i
ActiveChart.SeriesCollection(Count).XValues = "='Tasks'!$B$" & i
ActiveChart.SeriesCollection(Count).Values = "='Tasks'!$C$" & i
Mfg Robin
There are a couple of things wrong here.
First of all, you're always looking at the same series! I think you want to replace Count with a in your If statement.
If ActiveChart.SeriesCollection(a).Name
Secondly, once that is corrected, even if your Name variable gets set to True at some point, it may get reset to False later as the For...Next loop continues iterating through the remainder of the series collection. To prevent this, add an Exit For like this:
For a = 1 To Count
If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
Name = True
Exit For
End If
a = a + 1
Next
Also, I suspect you haven't declaring Name as a Boolean variable, so by default it's a Variant so its value isn't False to start with! You should declare the variable at the top of your procedure with Dim Name as Boolean, and then if you want Name to be False by default, you should really say so explicitly: Put Name = False before the loop. Moreover, Name is a terrible name for a variable... and so is Count Argh! I think your code should look like this:
Option Explicit
Sub MySub()
Dim a As Long
Dim NameExists As Boolean
Dim SeriesCount As Long
SeriesCount = ActiveChart.SeriesCollection.Count
NameExists = False
For a = 1 To SeriesCount
If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
NameExists = True
Exit For
End If
a = a + 1
Next
If NameExists = False Then
' Rest of your code goes here...
End Sub