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)
Related
I'm facing a odd behavior by applying different colours within one cell via VBA.
In my case there are hundrets of cells within one column, showing different work-packages.
My vba code exaclty does what it should do, by coloring identified strings (respecively work packages) via looping through the cells and identifiying each work package via RegExp.
Here there is one extract that is doing the coloring job:
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = suchmuster
If .test(objWks_myTable.Cells(row_myTable, 20).Value) Then
Set RegMC = .Execute(objWks_myTable.Cells(row_myTable, 20).Value)
For Each RegM In RegMC
objWks_myTable.Cells(row_myTable, 20).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = vbOrange
Next
End If
End With
The issue appears as soon as I double click the cell after my makro run.
Then without any recognizable pattern, some characters are shown in a different color (mostly not only one character but a connected bunch of). In the picutre, the first cell shows the colours after my vba run, the second cell shows how it will immediately look like, if i double click it.
If I leave the edit mode via Escape, the original vba set colors will stay, If I leave the edit mode via Return, the undefined changes happen.
There are no formats nor format conditions set within the cells.
I really need somebodys help here. Would be great to find a solution!
Many Thanks !
This picture should show the issue:
Picture of the issue
I've found the issue.
First I tried also Instr instead of using a RegExp but the issue didn't disappear.
So I was investigating in my code that writes the strings into the cells.
And within that code I did the following:
dummy = dummy & " # " & z_trim(ctrl.Caption) & vbCrLf
ActiveCell.Value = dummy
The issue is because of vbCrLf
If I write the strings into the cells the following way, the changes within my coloring run are fixed, there is no change by entering the cell in edit mode:
dummy = dummy & " # " & z_trim(ctrl.Caption) & Chr(10)
ActiveCell.Value = dummy
Picture of fixed issue
It works, so I'm fine. But still interessted, why vbCrLf is causing such confusing thing?
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
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 am trying to continue code to the next line. I have tried the (space)_ method and get compile errors, expecting seperator or expecting line # etc.... Have also tried & on next line. My code looks something like this.
Set rangeLimit = Range (A1:A9,B1:B9 on through M9:M37")
I need to move to the next line and continue this range all the way through Y9:Y37
I have looked on several help sites and all I have seen point to the things I have already tried. I am sure I am missing something simple. Any help would be greatly appreciated.
Set rangeLimit = Range("A1:A9,B1:B9," & _
"M9:M37")
space underscore works for me
Set rangeLimit = Range("A1:A9,B1:B9", _
"M9:M37")