Array Handling in VBA - vba

Is it possible to select multiple values dynamically using Excel Macros.What i meant by dynamically is the number of values to be selected should be entered by the User.
My code is
monthList = monthList & Chr$(34) & "[Time].[Month].&[" _
& Range("Table_Months").Rows(i).Columns(2).Value & "]" & Chr$(34) & ","
Next i
c_mnth = Left(monthList, Len(monthList) - 2)
c_mnth = Right(c_mnth, Len(c_mnth) - 1)
ActiveSheet.PivotTables("PivotTable1").PivotFields("[Time].[Month].[Month]"). _
VisibleItemsList = Array(c_mnth)
By this code I couldn't make it,because at last variable c_mnth consist of all my required field as single string which has to be individual string to execute the code.

Is this what you are trying? (UNTESTED)
Dim MyAr
'
'~~> Rest of your code
'
c_mnth = Left(monthList, Len(monthList) - 2)
c_mnth = Right(c_mnth, Len(c_mnth) - 1)
MyAr = Split(c_mnth, ",")
ActiveSheet.PivotTables("PivotTable1").PivotFields("[Time].[Month].[Month]"). _
VisibleItemsList = Array(MyAr)

Related

How to display desired text in hyperlink using VBA code

I have displayed my hyperlink in the column E as shown
\\maroon\cgm images\mech.pdf
But I want to display only mech.pdf in the cell how to modify my code.
my code used for displaying above hyperlink is shown below:
str = "\\maroon\CGM Images\" & pn & ".pdf"
ActiveSheet.Hyperlinks.Add Range("e" & i), str
In this I want to show only pn & str.
You need to specify TextToDisplay
ActiveSheet.Hyperlinks.Add Anchor:=Range("e" & i), Address:=Str, TextToDisplay:=pn & ".pdf"
Sometimes the Excel VBA reference is indeed helpful to find out on your own how functions and methods work: Hyperlinks.Add Method (Excel)
If you have
\maroon\cgm images\mech.pdf
in a cell and you wanna get only the name of the pdf with ".pdf" you can do the next code:
Dim spliter() as String
Dim str_pdf as String
str = \maroon\cgm images\mech.pdf ' Cells(a,b).value
spliter = Split(str, " ") 'To do the first split (You can delete ', " "' )
spliter = Split(spliter(1), "\")
'Now spliter(1) == mech.pdf so...
str_pdf = spliter(1) ' spliter(0) == images , spliter(1) == mech.pdf

Italicizing a specific part of a concatenation for a different worksheet

I am just beginning to experiment with VBA and wanted to create code that italicized the title of presentations in a concatenation, given that Excel normally does not allow that to happen. I also want to paste the output in another worksheet.
The current code is what I created to paste the concatenation output in a specific column of the SAME worksheet (named "Presentations Table"), but I am having trouble figuring out how to:
paste the concatenated output in a specific column of a different worksheet (named "Presentations Cited") starting from the first row and column in the same workbook
how to get it to automatically italicize ONLY the text from the title column. All other text in the concatenation should not be italicized.
I would be grateful for any help!
Table structure:
Current output:
Worksheets("Presentations Table").Range("a3", Worksheets("Presentations Table").Range("a3").End(xlDown)).Select
Row = 1
col = 1
For Each Cell In Selection
Authors = Cells(Row, col)
Year_Month = Cells(Row, col + 1)
Title = Cells(Row, col + 2)
Presentation_Type = Cells(Row, col + 3)
Event_Name = Cells(Row, col + 4)
Location = Cells(Row, col + 5)
Worksheets("Presentations Table").Cells(Row, col + 2) = Authors & " (" & Year_Month & "). " & Title & ". " & Presentation_Type & " at the " & Event_Name & ", " & Location & "."
Row = Row + 1
Next
You can paste them as HTML formatting with something like this (not tested):
Dim c As Range, s As String
Set c = ThisWorkbook.Worksheets("Presentations Table").Cells(3)
s = "<html>"
While c <> ""
s = s & c & " (" & c(, 2) & "). <i>" & c(, 3) & "</i>. " & c(, 4) & " at the " & c(, 5) & ", " & c(, 6) & ".</br>"
set c = c(2) ' the cell below c
Wend
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.SetText s
.PutInClipboard
End With
c(2).PasteSpecial

Faster Userform Listbox filtering Excel VBA

I'm developing a userform, and have a listbox in it. I'm loading the listbox, but only with select data, and based on different dropdowns in the userform as well. The sample sheet I have goes through 6000 lines, but I'm being told that it will eventually include 20,000+. It's already taking around ten seconds to filter. Is there a more efficient way of doing this?
Code is below:
For rowNum = isStartRow To isEndRow
Dim h2T As String: h2T = iSH.Range(h2 & rowNum).Text
Dim h3T As String: h3T = iSH.Range(h3 & rowNum).Text
Dim h4T As String: h4T = iSH.Range(h4 & rowNum).Text
Dim h5T As String: h5T = iSH.Range(h5 & rowNum).Text
Dim descT As String: descT = UCase(iSH.Range(desc & rowNum).Text)
If h2T Like "*" & Me.cmbo_H2.value & "*" And h3T Like "*" & Me.cmbo_H3.value & "*" And h4T Like "*" & Me.cmbo_H4.value & "*" And h5T Like "*" & Me.cmbo_H5.value & "*" And descT Like "*" & UCase(Me.txt_Search.value) & "*" And (iSH.Range("A" & rowNum) = 10 Or iSH.Range("A" & rowNum) = 20) Then
If Not Exists(Me.cmbo_H2, h2T) Then Me.cmbo_H2.AddItem h2T
If Not Exists(Me.cmbo_H3, h3T) Then Me.cmbo_H3.AddItem h3T
If Not Exists(Me.cmbo_H4, h4T) Then Me.cmbo_H4.AddItem h4T
If Not Exists(Me.cmbo_H5, h5T) Then Me.cmbo_H5.AddItem h5T
Me.list_Items.AddItem iSH.Range(desc & rowNum).Text
Me.list_Items.List(Me.list_Items.ListCount - 1, 1) = iSH.Range(codeCol & rowNum).Text
Me.list_Items.List(Me.list_Items.ListCount - 1, 2) = iSH.Range(iNumber & rowNum).Text
Me.list_Items.List(Me.list_Items.ListCount - 1, 3) = iSH.Range(moqCol & rowNum).Text
End If
Next
a more efficient way would be to read the range into an array and then do all processing within the array. Reading the sheet is costly in terms of processing time, read it once to an array and then process
This may help you http://www.cpearson.com/excel/ArraysAndRanges.aspx

How to set a loop into a macro that has no integer values set?

I have written the code below so that it will retreive the file locations and put them into Path Column(B) which corresponds to the .csv column(C) where a "YES" is found.
Dim csv_ap As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
Set csv_ap = Range("C65000").End(xlUp)
If csv_ap.Value = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value _
& "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value _
& "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
csv_ap.Offset(0, -1) = path_report2
End If
As you can see only the bottom row for column B has been filled. I'm not 100% sure why this is but could be down to not having a loop involved? I have tirelessly looked into adding a loop but cannot do so without affecting the current code. Any ideas?
I have edited the code above and got a loop working. But now it is duplicating the bottom row.
Dim cell As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
For Each cell In Sheets("Mail Report").Range("C2:C10").Cells
If cell = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value & "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value & "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
cell.Offset(0, -1) = path_report2
End If
Next
This is the result of the macro:
Your range is defined as a single cell by Set csv_ap = Range("C65000").End(xlUp).
If you want to process all the rows from 1 to the last occupied range, then you would need:
Set csv_ap = Range("C1:C" & Range("C65000").End(xlUp).Row)

Excel VBA hlookup function

I am trying to use the excel VBA HLookup function. I have tried it two ways and both produce error. Could anyone explain me what is that I am doing wrong?
First try:
lookupValue = Worksheets(1).Name & "!A1"
tableArray = Worksheets(3).Name & "!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Value = "=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
Second try:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Table = Worksheets(3).Range(Cells(1, 1))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)
For your first one, you need to use US regional settings, so a comma separator, and you should really enclose the sheet names in single quotes in case they contain spaces or look like special names (e.g. dates):
lookupValue = "'" & Worksheets(1).Name & "'!A1"
tableArray = "'" & Worksheets(3).Name & "'!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Formula = "=HLookup(" & lookupValue _
& "," & tableArray & ",1,FALSE)"
and for the second you have to use a range object for the table argument:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Set Table = Worksheets(3).Range(Worksheets(3).Cells(1, 1), Worksheets(3).Cells(1, Col_letter))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)
I have assumed Table is declared as Variant, Object or Range.
Note you will still get a run-time error if there is no match for your lookup value.
what you're doing there usually works, which is creating a string with the function you want to call and inputting it.
I don't think that's the safest way, since it would not work if you run that macro from an Excel with a different language.
the proper way to call an Excel function from VBA call is for example:
cells(1,2) = Application.WorksheetFunction.VLookup(123,Range("A1:C100"),3,FALSE)
anyway if you'd rather use this string approach the problem in the first try is that
"=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
results in the string:
=HLookup(Sheet1!A1;Sheet3!$A$1:$B$1;1;FALSE)
note that you're using semicolons where you're supposed to use commmas.
the problem in the second try is that the .Range property takes a string as input, so you cant Range(Cells(1,1)) you should do something like .Range("A1:A3")
hope it helps !