Selecting Data between sheets to Write a Vlookup function - excel

I Have a data in sheet(Sales Data) form Cells B7:B207 and I'm trying to write a vba code for using data to generate Vlookup function in sheet(Salesmen Info). But I failed to generate the VBA Code. Please advise how would I generate the VLookup in (Salesmen Info).

you could just vlookup outside of VBA with a standard function
=vlookup(A1,"Sales Data"!$B$7:$B$207,1,false)
If your VBA is failing, make sure you reference the sheet "sales data" in quotation marks whenever you call it. What's your current VBA code?
Additionally, why are you trying to vlookup a single column (B). You need your range to be at least 2 columns so it can find the data in B and return the data from C

I like applciation.vlookup:
arr = application.vlookup(A1,"Sales Data"!$B$7:$B$207,1,false)
then, this should work (typing without excel handy so it might not!):
for i = 0 to ubound(arr,0)
for j = 0 to ubound(arr,1)
debug.print arr(i,j)
next
next
(Press Ctrl+J to see the immediate window and see your output)
or maybe you want it in a sheet?
dim rng as range
set rng = range("A1")
for i = 0 to ubound(arr,0)
for j = 0 to ubound(arr,1)
rng.offset(i,j).value = arr(i,j)
next
next

Related

excel countif entire row in a table

I have a structured table in excel 2016.
I want to have a cell to count the number of cells across the entire row within the table if it matches my criteria.
I have tried putting this formula in column A on each row =COUNTIF(Table[#],"my criteria") but that does not count properly.
However, this works: =COUNTIF(Table[#[ColB]:[ColH]],"my criteria"). But since my table will expand, I don't want to specify the column name [ColB], I want to refer to the entre row in the table.
A header
countif
colC
colD
colE
First
formula
A
C
Second
formula
B
C
formula = =COUNTIF(Table[#],"A") does not work
formula = =COUNTIF(Table[#[colC]:[colE]],"A") works
My table will expand both horizontally and vertically.
Please Note: This solution is only available if you chose to use VBA. VBA does not work in web versions of Excel. Please ensure that you resave your workbook as a macro-enabled workbook before proceeding.
You can choose to use VBA and create your own custom worksheet formula. Since this question didn't start out as a VBA issue, I will be a bit more detailed on the process of setting this up as opposed to just throwing you some code and you having to figure out what to do with it.
After you've re-saved your workbook as a macro-enabled workbook, open the VBA Editor (VBE) by pressing Alt and F11 simultaneously.
In the VBE, click Insert > Module. You should now see Module1 highlighted on the left side bar (The project Explorer).
Copy and paste the following code in the blank area of the module:
Option Explicit
Public Function GetMyRowCount(Criteria As Variant) As Long
Dim ws As Worksheet
Dim tblRng As Range, RowRng As Range
With Application.Caller
Set ws = .Worksheet
Set tblRng = ws.Range(.ListObject.Name)
Set RowRng = ws.Range(ws.Cells(.Row, .Column + 1), ws.Cells(.Row, tblRng.Columns.Count))
End With
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
End Function
Now use this UDF (User Designed Function) in your worksheet. In the column you would like the calculation to be in, simply type =GetMyRowCount("My Criteria") and it should calculate.
To point out how this code works in more detail:
Application.Caller is referring to the cell that this function is located in. Because we now know the location of the cell, VBA can use it's location to obtain the row data from it (which is why you don't need an argument for the row #).
RowRng is getting the starting point of the column within the ws.Range(...) function with the first ws.Cells(...) function. .Row is the row # from the GetMyRowCount function (using Application.Caller.Row method), and the 3 is simply the static column C.
The way we grab the last column we need is by counting the total # of columns within the table: ws.Cells(.Row, tblRng.Columns.Count)
Using the information we obtained from bullets 2 and 3, we can establish the entire range of the lookup we need, and then place this range into your CountIf() function, along with the criteria you passed with the function's argument:
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
As you can see in the following example, I wanted to count the number of times in the row the number 1 occurred:
Another example showing it works with text as well by using "Apple" as the criteria:
Try this: =COUNTIF(B:B,"my citeria"), so if your Column is A, range would be A:A, for B it is B:B.
Let me know if this helps.

Get Sum of Column H based on sheet name

I am trying to get the sum of column H from each individual sheet. The name of the sheet is given in column A
The way I have been doing it is using =SUM('BSF10003'!H:H), dragging this formula down and changing the last couple digits manually but this is a long process if there are 100+ columns.
I am new to VBA and am hoping there is a way to do this task quicker
Thanks again!
The INDIRECT function is another way. Example below,
=SUM(INDIRECT("'"& A2 &"'!H:H"))
A2 = The sheet name
INDIRECT lets you make references dynamically.
One way could be a UDF
Paste this in a module
Function SumSheets(ByVal SheetName As String) as Double
SumSheets=Application.WorksheetFunction.Sum(Worksheets(SheetName).Range.Columns(8))
End Function
And use it like a standard formula...
=SumSheets(A1)
Will return the sum of column H in the worksheet named in A1
Or as a variation, you could use this along with the UDF. You'd paste it in the same module like the UDF but you don't need to type the formula. Instead, you select a cell in a column and run this macro.
The formulas are added automatically.
Sub FillRangeWithSumSheets()
Dim n As Name: Dim s As String: s = "SumSheetsRange"
With ActiveWorkbook
For Each n In .Names
If n.Name = s Then
n.Delete
Exit For
End If`
Next
Set n = .Names.Add(s, "=OFFSET($A1,0," & ActiveCell.Column - 1 & ",COUNTA(Sheet1!$A:$A),1)")
n.Visible = 1
Range(s).Formula = "=SumSheets(A1)"
End With
End Sub

How can you shift selected cell using Excel VBA?

I am trying to automate a rolling calendar spreadsheet that tracks various metrics and charts them into a spark line. The script I would like to write would shift the selected range in the spark-lines every time it is ran.
I have done some googlefu and have tried using the offset function to no avail. This is because the data is in a predefined range defaulting to num 0 based on the formulas used to populate the spreadsheet int the first place.
excel vba : selected cells loop
https://www.excel-easy.com/vba/examples/loop-through-entire-column.html
https://support.microsoft.com/en-us/help/291308/how-to-select-cells-ranges-by-using-visual-basic-procedures-in-excel
I am stuck at incrementing the ActiveCell.SparklineGroups.Item(1).Item(1).SourceData from its current selected range to PPTracking!G8:R8 ... H8:S8 ... and so on each time the macro is ran.
This is my first time working in VBA and any help is greatly appreciated!
Sub Macro4()
Dim selectedRange As Range
Set selectedRange = PPTracking!F8:Q8
Range("E5:E6").Select
Application.CutCopyMode = False
ActiveCell.SparklineGroups.Item(1).Item(1).SourceData = "PPTracking!F8:Q8"
Range("E5:E6").Select
End Sub
You can either use Sparkline.ModifySourceData or directly change the Sparkline.SourceData property, as it looks like you are currently aiming to do.
This code will shift the SourceData 1 column to the right - from F8:Q8 to G8:R8, then to H8:S8, etc. - by using the original SourceData value as a reference within Range, which is then Offset by 1 column.
It concatenates the Parent.Name to the Address to get the full Worksheet Name and cell reference.
Sub ShiftSparklineData()
If ActiveCell.SparklineGroups.Count > 0 Then
With ActiveCell.SparklineGroups.Item(1)
.SourceData = "'" & Range(.SourceData).Parent.Name & "'!" & Range(.SourceData).Offset(, 1).Address
End With
End If
End Sub
Avoid using ActiveCell where possible though; reference the cell(s) with a sparkline using Sheets("Yoursheetname").Range("Cellreference")

Excel - Copy columns to rows

I have 3 columns in a sheet in excel as below
I need the output in the below format on a SEPARATE SHEET
I'm fine with either VB script or using just excel features. Could I please get some help?
Try this macro. Place the macro in a regular code module (Insert > Module). Adjust the ranges to suit your situation.
Sub rearrange()
Dim cel As Range, tgt As Range
Set cel = ActiveSheet.Range("A1")
Set tgt = ActiveSheet.Range("D1")
Do While Len(cel) > 0
tgt = cel
tgt.Offset(1, 0) = cel.Offset(0, 1) & cel.Offset(0, 2)
Set cel = cel.Offset(1, 0)
Set tgt = tgt.Offset(2, 0)
Loop
ActiveSheet.Range("A:C").Delete
End Sub
If you're not going to do this on a regular basis, here's a simple solution.
I don't have access to MS-Excel so I cannot give you the exact answer. But I hope this helps.
Steps:
Add a new column with the concatenate function to the right of the table for example, to merge cells b1 and c1, use =Concatenate(b1,c1) and keep this result in cell D1. Do a copy-paste of the function for the other rows as well.
Copy your selection to a new worksheet where you want the result.
Use paste special to only paste the values of copied cells without forumulas. This ensures that it won't reference the original cells or change relatively.
Use the transpose function to change the resulting contents like your final output while pasting the data. Similar one here.
If you need to do this regularly, this method is not suitable. You'll be better off with a VBA script. But it's been a very long time since I worked on Excel so I cannot help you there.

Excel - VLOOKUP via VBA

I want to know how exactly to incorporate a VLOOKUP function into my Excel worksheet via VBA; I am completely comfortable with the VLOOKUP function when entering it directly into a cell, but I am completely new to VBA.
A little info:
I have ~5500 rows of data;
I have a number of named ranges (such as catNo, catNoRange, to name but two);
I want to use VBA to check catNo against catNoRange and return the value of the sixth (6th) column in catNoRange;
I also want to know how - and where - to display the result once I have it; ideally I would like it to appear in cell J4 (and the corresponding cells down to J5500).
How can I achieve this?
Additional Info:
For those of you wondering why I don't just use a regular VLOOKUP to achieve this: I want to use VBA because I have written a script which checks to see if certain cells are empty, and if at least one of them is populated, then the VLOOKUP will execute, taking the value of the populated cell for the main search critera.
Code Update:
Following some advice, I have opted to attempt to use the Find function instead of VLOOKUP. It doesn't work; here is what I have:
Sub findCode()
Result = WorksheetFunction.Find("ABI0010P", "5012616173004", "33787")
Range("J4").Value
End Sub
For the find function you want to have something like this
Dim ws As Worksheet
Set ws = Worksheets(2)
Rowz = ws.Cells(rows.Count, 1).End(xlUp).Row
k = 2
searchvalue= ws.Cells(k, 2)
Set SEARCHRANGE= ws.Range("A2:A" & Rowz)
Set Findx = SEARCHRANGE.find(searchvalue, LookIn:=xlValues)
If Not Findx Is Nothing Then ws.Cells(k, 4) = "what ever you want"

Resources