I have a bunch of named ranges within a sheet that must get cleared every day. Currently I have it set up within VBA like this:
Range("CustomList1").ClearContents
Range("CustomList2").ClearContents
Range("CustomList3").ClearContents
Range("CustomList4").ClearContents
Range("CustomList5").ClearContents
(+15 more)
Not really a big deal but I feel like there must be a better way of going about it. That being said after doing some searching I didn't really see anything about looping through multiple named ranges. Any ideas/thoughts on this?
Dim i As Long
For i = 1 to 20
Range("CustomList" & i).ClearContents
Next
... something like that.
If you had some sort of naming convention for named ranges that you want to clear, you could do something like this which means you don't ever need to update your code ...
Dim objName As Name
For Each objName In ThisWorkbook.Names
If InStr(1, objName.Name, "NamedRange", vbTextCompare) = 1 Then
With objName.RefersToRange
.Worksheet.Range(.Address).ClearContents
End With
End If
Next
Skin's solution will work if the name is consistent, but if not, you could always create an array with all the range names.
Something Like
RngArray = Array("CustomList1","CustomList2","CustomList3, etc.")
For i = 0 to 19
Range(RngArray(i)).ClearContents
Next
A Union may be more typing than a loop but it completes the operation in a single statement.
Union(Range("CustomList1"), Range("CustomList2"), Range("CustomList3"), _
Range("CustomList4"), Range("CustomList5"), Range("CustomList6"), _
Range("CustomList7"), Range("CustomList8"), Range("CustomList9"), _
Range("CustomList10"), Range("CustomList11"), Range("CustomList12"), _
Range("CustomList13"), Range("CustomList14"), Range("CustomList15"), _
Range("CustomList16"), Range("CustomList17"), Range("CustomList18"), _
Range("CustomList19"), Range("CustomList20")).ClearContents
This method would likely be better suited to named ranges with abstract or dissimilar naming conventions.
I would do this slightly differently.
I would store the names under one name in the Formula==>Names Manager as shown below.
And then I will only use the below every where. No need for several lines of code everytime you want to clear the range.
Range("MyCustomList").ClearContents
Related
I am setting a range variable equal to a list of ranges. The list is so long that I would like to use a line break for easier management. However, I am having a tough time getting the syntax. I've tried many combinations.
The code in one line works and looks like this (with a lot more named ranges):
Dim xNamedRangeList As Range
Set xNamedRangeList = Range("NamedRange1, NamedRange2")
My best attempt so far is this.
Set xNamedRangeList = " Range(""" & _
"NamedRange1," & _
"NamedRange2" &_
""")"
Debug.print looks like this: Ranged("NamedRange1, NamedRange2")
While this works in debug.print, it sure doesn't work when I set xNamedRangeList equal to it. I think I've turned the formula into a string, but not sure how to make it work in the formula.
The correct syntax would be as shown by #TimWilliams.
But you say "with a lot more named ranges", so please note that the length an address used in Range(address) can have is limited to 255 characters. So if you have a lot of ranges you might hit that limit and it doesn't work.
If that might be the case you must use the Application.Union method:
Dim xNamedRangeList As Range
Set xNamedRangeList = Application.Union(Range("NamedRange1"), Range("NamedRange2"))
Line breaks are always Space followed by underscore _ and Enter.
Set xNamedRangeList = Application.Union(Range("NamedRange1"), _
Range("NamedRange2"))
Set xNamedRangeList = Range("NamedRange1," & _
"NamedRange2," & _
"NamedRange3")
If NamedRange1 and NamedRange2 are variables then try:
Range(NamedRange1 + NamedRange2)
I have a collection that I'm attempting to iterate through, which I am able to do no problem. What I would like to achieve is seeing the next object in the collection, but I am unable to find anything on this.
I've tried to look ahead using a (+ 1) in the if statement, but this doesn't seem to work.
For each a in CollBlank
if CollBlank(a + 1) <> "some value" then
'do code
end if
Next
Ideally, I'd like to be able to look ahead.
Access-vba & excel-vba are tagged since collections are used in both access and excel, I'm personally using it in Access right now, but most tutorials are through Excel.
Rather than using for each, use a for loop with an index variable, for example:
Dim i As Integer
For i = 0 to CollBlank.Count - 2
If CollBlank(i + 1) <> "some value" Then
' Do stuff
End If
Next i
I would like to download prices from the internet. The concept works, when I define symb as a constant value (e.g. K15). But now, I want to download data from different links, where the part symb changes according to the value of the cells G13 to G22 in my spreadsheet. (In other words, I want to go through each row from G13 to G22 - each containing a different value for symb - and download the data from the respective link).
I tried that with a simple loop, defining the variable symb in each one of the loops:
For i = 1 To 10
Symb = Worksheets("Futures").Range("G12").Offset(i, 0).Value
Set qt = querysheet.QueryTables.Add( _
Connection:="URL;" & "http://download.finance.yahoo.com/d/quotes.csv?s=" & Symb & ".cbt&f=sl1d1t1c1ohgv&e=.csv", _
Destination:=querysheet.Cells(5 + i, 1))
Next i
Obviously, it doesn't work like this. I assume that it is not possible to define a variable within the loop, is it? Can somebody give me a hint how I can make that work?
There's something worng with your Connection String. When I get rid of the .cbt in the URL string, it works. Or, you may have forgotten to include some letters, so debug it in your browser and get the Connection string correct and it should work.
You may also want to modify the destination, like so, and refresh the table:
Set qt = querySheet.QueryTables.Add( _
Connection:="URL;" & "http://download.finance.yahoo.com/d/quotes.csv?s=" & Symb & "&f=sl1d1t1c1ohgv&e=.csv", _
Destination:=querySheet.Cells(5, 1))
qt.Refresh
you could use an array like this
symb(1 to 10) as String
for i=1 to 10
symb(i)=cells(i,1)
next i
I've use my Excel 2013 to record a macro in inserting a chart, a column-clustered chart in my case. In the view code option, it shows me a line of code as below:
ActiveSheet.Shapes.Addchart2(286,xl3DColumnClustered).Select
Please help me as I cannot understand what does the number 286 represent. I know the syntax of Addchart2 is:
expression.AddChart2(Style,XlChartType,Left,Top,Width,Height,NewLayout)
If I change the "286" to "285", the chart appears with a blue background. An error comes out if the number is 100.
Can anyone kindly tell me what does the number represent?
One can also provide only the ChartType and the application will use the default style.
Set oShp = ActiveSheet.Shapes.AddChart2(XlChartType:=xl3DColumnClustered)
oShp.Chart.SetSourceData Source:=RngDta
This picture shows the default ChartStyle for all ChartTypes (excluding StockHLC and StockVOHLC)
This won't directly answer your question, but it will help you figure out what is going on.
This is pure conjecture on my part, but I would guess it's an undocumented bitfield. As you may know a bit field is just a way to use a number. So image we have a Byte variable which can be 8 bits (or flags). So in a byte we can store up to 8 values.
Example: We have field called "DaysOpen" bits 1-7 mean the store is open on that day of the week. (We'll ignore the 8th bit.) So if the store is open M-F that would be binary 0111 1100.
Then you just convert that number to decimal and we see that it's 124.
That variable is a Variant so it could be anything from a Byte to Long meaning it could be storing up to 64 different flags.
As a side note (if you are interested) you can use bit fields like so:
Option Explicit
Public Enum DayFlags
'Notice these are power of 2.
dfSunday = 1
dfMonday = 2
dfTuesday = 4
dfWednesday = 8
dfThursday = 16
dfFriday = 32
dfSaturday = 64
End Enum
Sub Example()
Dim openHours As DayFlags
'Set the flags:
openHours = dfMonday Or dfTuesday Or dfThursday
'See the binary?
MsgBox Right$("00000000" & Excel.WorksheetFunction.Dec2Bin(openHours), 8)
'Notice the order is right to left. This is call endianness.
'You can check for a specific flag like this:
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
'You can add a flag like this:
openHours = openHours Or dfFriday
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
'You can remove a flag like this:
openHours = openHours Xor dfFriday
MsgBox IsOpenOnDay(openHours, dfMonday) & vbNewLine & IsOpenOnDay(openHours, dfFriday)
End Sub
Private Function IsOpenOnDay(ByVal openHours As DayFlags, ByVal day As DayFlags) As Boolean
IsOpenOnDay = ((openHours And day) = day)
End Function
Well , I had the same situation once, and those are basically chart styles. I tried to figure out the exact numbering but then i realized that recording was a much easier way of knowing the style numbers just as you have done here.
To answer you question, record macros to know which style you want to implement in your macros.
Just checking to see if 5 years later anyone has a better answer. I sure could use an enumeration of the chart styles; I don't like putting simple numbers in my code without some explanation as to what it means. Of course, I could use a comment, but if the numbers are documented, then that means they could change.
I found a partial list: https://learn.microsoft.com/en-us/office/vba/api/excel.xlcharttype
I'm sure these numbers, plus the bitfield variations as suggested by Pillgram above to control various other chart aspects, answer the question. The possible combinations are in the thousands, so a full list would be pretty useless. Recording is still your best bet.
I have aset of account names in one workbook (the active one), and I need to use the .Find function to look for their ocurrences in another workbook/sheet. I don't think I'm getting the right Object handle for the other workbook/sheet, but nothing I try is working.
For Count = 1 to 10
accName = Cells(Count, 1).Value
AccRow(Count) = OBJECTHANDLE.Find(accName).Row
Next Count
Any help?
Never mind, I found the answer with perserverence (it's so rare at the end of the day). Needs to have the Object defined up to .Range, so:
Workbooks("WORKBOOK").Sheets("SHEET").Range("RANGE")
I hate VBA.