Passing Array of strings to Sheets() in VBA - excel

i intend to save the sheets as pdf which works fine, however when i wanted
to save sheets with names stored in some cell i run into a problem
i have written following function in VBA and i cant' figure out what is the problem or how to walk around it
Sub Export_to_pdf()
Dim SheetArr() As String
.
.
.
SheetArr = Split(Worksheets("EXPORT").Range("C12").Value, ",")
Sheets(SheetArr).Activate
.
.
.
End Sub
the problem with code above is that when i pass to Sheets regular array like Sheets(Array("sh1","sh2")).activate - its fine but when i use the split on cell which content looks like "sh1","sh2" it throws error
from what i tried to debug i found out that possibly the problem is that Split returns String array as opposed to variant array which i suspect to be the problem, how can i solve this problem ? casting string values with CVar doesn't seem to work
Thanks for any answers

This works fine for me:
Sub Export_to_pdf()
Dim SheetArr
SheetArr = Split(Sheet1.[A1], ",")
Sheets(SheetArr).Select '<< not Activate
End Sub
In A1: Sheet1,Sheet2

Related

VBA Code to look up function in cell, store in variable and copy somewhere else

I'm trying to come up with a script that looks up a function in named range "Hard_Constr_Cost". Saves the function in variable "HC_Baseline" and then copies it to another range.
The reason why I don't just set one range equal to another is that after the function is stored I want to run a separate loop overwriting the value in range "Hard_Constr_Cost". At the end I want to overwrite with the original function stored in the variable "HC_Baseline".
Below is the script I created so far but I'm getting an out-of-range error. Does anyone know how to fix this? Any help is appreciated.
Sub HC_Sensitivity()
Dim HC_Baseline As String
HC_Baseline = Sheets("SU").Range("Hard_Constr_Cost").Formula
Sheets("SU").Range("T5") = HC_Baseline
End Sub

EXCEL VBA Type mismatch with "Next" highlighted

I'm creating small project in Excel, and because I'm a VBA newbie I do encounter a lot of problems that I'm trying to resolve on my own. However i can't cope with this:
I created Sub that accepts two objects: FormName and ControlName.
What i want it to do, is to loop through every Control in specific UserForm and populate every ListBox it encounters, from another ListBox.
I created this funny string comparison, because I need to operate on objects in order to execute the line with AddItem. This comparison actually works well, no matter how ridiculous it is. However when I launch the program, I got
Type Mismatch error
and to my surprise "Next" is being highlighted. I have no idea how to fix this, nor what is wrong.
Public Sub deploy(ByRef FormName As Object, ByRef ControlName As Object)
Dim i As Integer
Dim O As msforms.ListBox
i = 0
For Each O In FormName.Controls
If Left(FormName.Name & O.Name, 16) = Left(FormName.Name & ControlName.Name, 16) Then
O.AddItem (FormName.PodglÄ…d.List(i))
i = i + 1
End If
Next
End Sub
I call this sub using:
Call deploy(UserForm1, UserForm1.ListBox3)
Above, I use Listbox3 because otherwise i got error saying that variable is not defined. However in my comparison I kinda override this.
If someone can explain in simple words, how to fix this type mismatch issue or how to write it in more elegant way

Extract URL from =Hyperlink()

I'm trying to extract the formula that is evaluated from a link generated with a formula:
=HYPERLINK("https://www.google.com/search?q="&A1&B1,"Link")
(A1 has vba and B1 has help)
I've seen many, many threads and even some SO threads with suggestions, but they only get me to the ...q= without considering I have more text to come.
The best luck I've had so far is from this thread, and I've tweaked it to search until B1 like this:
...
S = Left(S, InStr(S, "B17") + 2)
...
But it returns https://www.google.com/search?q="&A1&B1.
How can I get it to first evaluate what's in those cells, before returning the URL?
I was overthinking this I think. Thanks to #MacroMan for getting my head straight. I put together the following, rather clunky, macro.
Function hyperlinkText(rg As Range) As String
' Inspired by https://stackoverflow.com/questions/32230657/extract-url-from-excel-hyperlink-formula/32233083#32233083
Dim sFormula As String
Dim Test As String
sFormula = rg.Formula
Test = Mid(sFormula, WorksheetFunction.Search("""", sFormula), WorksheetFunction.Search(",", sFormula) - WorksheetFunction.Search("""", sFormula))
hyperlinkText = Evaluate("=" & Test)
End Function
This can take a URL that looks like:
=HYPERLINK("https://www.google.com/search?q="&A17&B17,"Link") and return the evaluated URL:

populate combobox in VBA with array elements

I have a VBA procedure (in Excel 2007) where I aspire to set the ListFillRange property of a combobox styled as a list using an array.
I know this works if I right click the combobox and write "Sheet1!$F2:$F17" next to the "ListFillRange" property. I can also do this in code. However, I am interested in dynamically setting the value of this property by assigning it an array.
I know for sure the array works as I tested it; there is probably a syntax error here:
ThisWorkbook.Worksheets("Sheet1").OLEObjects("cmbS").ListFillRange = ar
when I do this I get:
"Type mismatch" error.
The result of this action should be that the component is populated with the array elements, from element(0) ... to the last element (n-1) of the array. Any pointers, thank you very much!
I also tried:
ThisWorkbook.Worksheets("Sheet1").cmbS.list = ar
and it says "permission denied"
Here are the combobox properties in case it helps:
After testing and trying, I found this works:
ThisWorkbook.Worksheets("Sheet1").cmbS.ListFillRange = ""
Dim i As Integer
For i = LBound(ar) To UBound(ar)
ThisWorkbook.Worksheets("Sheet1").cmbS.AddItem (ar(i))
Next
However, I am interested in populating with all values at once for faster effect, not just adding element by element.
I know its late but maybe it is going to help someone else. At least the following code works (much faster than element for element) for me.
dim arr() as variant
arr = Worksheets("Total").Range("C2:"&lrow).Value
Worksheets("Menu").ComboBox2.List = arr
The only way you can populate a combobox with the content of an array is by doing it element by element. I find it hard to believe that it would be a notably slow process no matter how large your array is.

Excel VBA - Execute string as code

I am trying to execute the following string as code using Evaluate but get Error 2029. Anyone know why?
Help much appreciated.
Calculation = "Format(""21/08/2012"", ""MMM"")"
Value = Evaluate(Calculation)
Try instead
Calculation = "TEXT(""21/08/2012"", ""MMM"")"
EVALUATE converts formulas to results, and FORMAT is a VBA function. The formula equivalent is TEXT.
You can also skip the evaluate and use the FORMAT function on the date directly.
You can use most worksheet functions in VBA directly with Application.WorksheetFunction. - for example - try this out:
Sub DateExample()
Dim StringTest As String
StringTest = Application.WorksheetFunction.Text("12/08/2012", "MMM")
Cells(1, 1).Value = StringTest
End Sub
Good Luck

Resources