Can't get the tabs value (name) in VBA macro - vba

I'm stuck here. below is my vba macro. I want to lookup againts other tabs but the result only give me
=VLOOKUP(BE2, $BQ$2:$BQ$6, 1, 0) but not the tabs name.. Why? How can I get the sheets name?
Set myValues = Application.InputBox("Please select a:", Type:=8)
Set myResults = Application.InputBox("Please select a next sheet:", Type:=8)
On Error Resume Next
Set myValues = myValues.Offset
FirstRow = myValues.Row
FinalRow = Cells(65536, myResults.Column).End(xlUp).Row
Range("BF2").Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
" " & myResults.Address & " , 1, 0)"

You're close. You just need to set the Address External argument to True.
Something like:
myResults.Address(External:=True)

The .Address property only returns the row and column reference unless you set the External parameter to True. See below:
Range("BF2").Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
" " & myResults.Address(External:=True) & " , 1, 0)"
If you don't want the workbook name in the reference, you can build the reference using the .Worksheet.Name property of the Range object. See below:
Range("BF2").Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
" " & myResults.Worksheet.Name & "!" & myResults.Address & " , 1, 0)"
See the Range.Address Property on MSDN for more information.

Related

How to use variables to set pivot table Function parameters with VBA?

I would like to use variables to specifying parameters of pivot table. Most of them are working correctly, but 2 of them not. The “function” parameters “xli” and “ValueFilterQuant(pos)” don’t come up with any values. I tried it write between quotation marks and quotation marks-& but nothing happened. Is there any method to set Function parameters with variables? This is the snippet of code:
For pos = 1 To UBound(ValueQuant)
If ValueQuant(pos) = "Work" Then
xli = "xlSum"
Label = "Sum of "
Else
xli = "xlCount"
Label = "Count of "
End If
ActiveSheet.PivotTables("PivotTable1").CubeFields.GetMeasure "[database].[" & ValueQuant(pos) & "]" _
, xli, "" & Label & "" & ValueQuant(pos) & ""
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
" PivotTable1").CubeFields("[Measures].[" & Label & "" & ValueQuant(pos) & "]"), "" & Label & "" & ValueQuant(pos) & ""
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[Measures].[" & Label & "" & ValueQuant(pos) & "]")
.Caption = "" & ValueFilterQuantaf(pos) & "" & ValueQuant(pos) & ""
.Function = ValueFilterQuant(pos)
End With
Next
The code is incomplete. I don't know where ValueFilterQuant(pos) is coming from.
I suspect that like xli = "xlSum" it is returning a string variable, but the thing is that those parameters should not be strings. They are numbers that are derived from an XlConsolidationFunction enumeration.
So something like:
If ValueQuant(pos) = "Work" Then
xli = XlConsolidationFunction.xlSum '-4157
Label = "Sum of "
Else
xli = XlConsolidationFunction.xlCount '-4112
Label = "Count of "
End If
Same thing needs to happen for ValueFilterQuant(pos).
Note that it is more work to derive a dynamic enumeration at runtime through a string. I guess that you are reading a string value in off a worksheet or similar process.
You could set them up in a dictionary beforehand, and pick out the numbers that way.
The list of values is here: https://msdn.microsoft.com/en-us/library/office/ff837374.aspx

Fill a column from a concatenate range

What I have this far is below.
Range("F4").Value = Range("D4").Value & " x " & Range("E4")
This is creating a concatenate in Column F for me. This is working fine for the individual cell but I would like this to continue concatenating down until it hits an empty cell.
I have tried looping without success.
below is an image of the sheet
Set Concat= Rows(4)
concatstring =""
For i = 1 To Concat.Cells.Count
If Concat.Cells(i).Text <> "" Then
Concatstring = concatstring & " x " & Concat.Cells(i).Text
Else
Exit for
End If
Next
Concat.cells(I).text = concatstring
This should do it. Let me know if you still have problem.
Try this...
i=4
Do while(Range("D" & i).Value<> "")
Range("F" & i).Value = Range("D" & i).Value & " x " & Range("E" & i)
Loop

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)

Deploy VBA Cell Reference in Array Formula

So I'm trying to do a couple things with this subroutine but can't get VBA to execute the .FormulaArray function.
Create a named range using offset & lastrow function
Use cell references to insert into the array formula
--
Sub namedrange()
Dim firstrow As Long
Dim LastRow As Long
Dim ColToLetter, absolute, Title, mc, mc1
ActiveCell.Offset(0, -1).Select
absolute = ActiveCell.Address
LastRow = ActiveSheet.Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
firstrow = ActiveCell.Row
ColLetter = Mid(ActiveCell.Address, 2, 1)
ActiveSheet.Range(ColLetter & firstrow & ":" & ColLetter & LastRow).Name = Range(ColLetter & "1").Value
Title = Range(ColLetter & "1").Value
ActiveCell.Offset(0, 1).Select
mc = ActiveCell.Offset(-1, 0).Address
mc = Mid(mc, 2, 3)
mc1 = Replace(mc, "$", "")
ActiveCell.FormulaArray= "=IF(ROWS(mc & "":"" & mc1)>SUM(IF(FREQUENCY(IF(Title<>"""",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),1)),"""",INDEX(Title,SMALL(IF(FREQUENCY(IF(Title<>"""",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),ROW(Title)-ROW(absolute)+1),ROWS(mc & "":"" & mc1))))"
End Sub
The formula bar shows what the vba function is outputting, which is not what I want. I don't know why it won't output the references I've created like mc should be "$A$2" not "mc".
Also when I try to execute the FormulaArray code I get a runtime error 1004 "Unable to set the FormulaArray property of the Range class"
Your .FormulaArray content has some typos. Here's how it should look like (assuming all the above code is fine):
ActiveCell.FormulaArray= "=IF(ROWS(" & mc & ":" & mc1 & ")>SUM(IF(FREQUENCY(IF(Title<>" & chr(34) & chr(34) & ",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),1))," & chr(34) & chr(34) & ",INDEX(Title,SMALL(IF(FREQUENCY(IF(Title<>" & chr(34) & chr(34) & ",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),ROW(Title)-ROW(absolute)+1),ROWS(" & mc & ":" & mc1 & "))))"
In general, remember that if you want the value of a variable to be printed into a string, you cannot write "a=mc+3" but rather a = " & mc & "+3".

How to select some selected Pivot table area?

I am trying to select and copy some selected area of a pivot table. I am able to determine the amount of area I want and am able to display the range in a message box which is not my objective. I want to copy that selected range. My code looks like this.
I want to copy the Values in the range (toprow1,leftcoloumn:lastrow,rightcoloumn).
FYI the message box code is something I don't need Its just their to tell you the Range no.
Sub PivotTableRangeAreas()
With ActiveSheet.PivotTables(1)
Dim TopRow1 As Long, TopRow2 As Long, LastRow As Long
Dim LeftColumn As Long, RightColumn As Long
TopRow2 = .TableRange2.Row
With .TableRange1
TopRow1 = .Row
LastRow = .Rows.Count + .Row - 1
LeftColumn = .Column
RightColumn = .Columns.Count + .Column - 1
End With
MsgBox "The pivot table named " & .Name & vbCrLf & _
"occupies these range elements:" & vbCrLf & vbCrLf & _
"With the Report (Page) field: " & vbCrLf & _
.TableRange2.Address(0, 0) & vbCrLf & _
"Without the Report (Page) field: " & vbCrLf & _
.TableRange1.Address(0, 0) & vbCrLf & vbCrLf & _
"First row, with the Report (Page) field: " & TopRow2 & vbCrLf & _
"First row, without the Report (Page) field: " & TopRow1 & vbCrLf & _
"Last row: " & LastRow & vbCrLf & _
"Left column: " & LeftColumn & vbCrLf & _
"Right column: " & RightColumn, , "Pivot table location."
End With
End Sub
I'm guessing it's just the values that you want to copy? If so, try starting with something like this - it'll put the values into Sheet2 starting at range A1. I'm not sure which range from the pivot table you want to copy to where - you'll have to change some of this to suit:
Sub CopyRange()
Dim vArray() As Variant
'Copies the values between (Toprow1, LeftColumn) and (LastRow, RightColumn) into an array
vArray = ActiveSheet.Range(Cells(TopRow1, LeftColumn), Cells(LastRow, RightColumn)).Value
'Pastes the values from the array into Sheet2, starting at A1
Sheet2.Range("A1").Resize(UBound(vArray, 1), UBound(vArray, 2)).Value = vArray
End Sub