VBA find missing text String and count missing values - vba

HI there I am trying to put a VBA code to look for a specific string (text) I have defined in the code located in another workbook find the number of missing values text from the list I defined and produce a report how many values text are missing and if any values wrong do not match the text how many are wrong. this is some of the code I have put but I am struggling to find a code to look for the defined text and produce a report of the number of missing instrument and nr of wrong instrument: Any suggestions will be highly appreciated.
Dim mypath As String
Dim folderpath As String
Dim filename As String
Dim MyBook As Workbook, newbook As Workbook
Set MyBook = ThisWorkbook
Dim file As String, sheetdata As String
Dim ws As Worksheet
Dim x As Workbook
Dim y As Workbook
Dim FindString As String
Dim Rng As Range
Dim findrow As Integer
Dim i As Integer
Dim finalrow As Integer
Dim bottomA As Integer
Dim c As Range
Dim TESTMarketFI2015submitted As Worksheet
Dim FI As Worksheet
Dim a As Range
Dim col As Integer
Dim row As Integer
Dim cell As Integer
Dim inputbox As Variant
Dim application As String
Dim value As String
Dim bond As String
Dim promissoryNote As String
Dim loan As String
Dim certificatesOfDeposit As String
Dim embededOptionBond As String
Dim repo As String
Dim bondOption As String
Dim bondForward As String
Dim securedBond As String
Dim inflationLinkedBond As String
Set x = Workbooks.Open(filename:="Z:\Profiles\My Documents\MAPPING\TEST MAPPING TABLES\TEST_Market_FI_2015 - submitted.csv")
worksheets("TEST_Market_FI_2015 - submitted").Range("A:A").Select
For Each c In Sheets("TEST_Market_FI_2015 - submitted").Range("A:A")
Next
If value = ("bond,promissoryNote,loan,certificatesOfDeposit,embededOptionBond,repo,bondOption,bondForward,securedBond,inflationLinkedBond") Then
c.EntireRow.Copy Worksheets("TEST_Market_FI_2015 - submitted").Range("A:A" & Rows.Count).End(xlUp).Offset(1)
Worksheets("FI").Select
Range("A2").End(xlUp).Offset(i, 0).PasteSpecial
Else
MsgBox "Nothing found under instrument"
End If
End Sub

You need to put your next under the End If. When it checks the value, it does not know what it's actually checking. It should be:
For Each c In Sheets("TEST_Market_FI_2015 - submitted").Range("A:A")
If c.value = ("bond,promissoryNote,loan,certificatesOfDeposit,embededOptionBond,repo,bondOption,bondForward,securedBond,inflationLinkedBond") Then
c.EntireRow.Copy Worksheets("TEST_Market_FI_2015 - submitted").Range("A:A" & Rows.Count).End(xlUp).Offset(1)
Worksheets("FI").Select
Range("A2").End(xlUp).Offset(i, 0).PasteSpecial
Else
MsgBox "Nothing found under instrument"
End If
Next
So now what will happen is, it will loop through each cell, in your selected range and check the value of c (the cell it's presently looking at). If the value of c matches your string then it will do stuff.
However, I doubt the cell value you're looking for is truly a comma delimited string. Are you trying to search for all of those strings individually, meaning that if c.value is ANY of the words in your () then it should do stuff. If so, one option to pursue would be creating a string array, and having the c.value look in the array instead.

Related

VBA - Using SearchRange to Find Variable Value

I'm working on an Excel Spreadsheet that pulls a pick list from the SQL Database and populates the sheet. It then prompts the user to Scan a Part number. I am trying to locate the part number in Column A, and return the row for the part number.
The part number begins as a Variant type, but thinking that type was the problem, I converted it to string by setting its value to another variable.
Finally, I found this snippet of code (I've tried many and none have worked so far), and it works when you specify a number (123456789012) as in the code below. It does not work if you replace that number with the variable sPart nor scanPart.
I need it to work to find the row of the variable sPart (String) or scanPart (Variant). What am I doing wrong?
Function ReturnRowNumber()
Dim ws As Excel.Worksheet
Set ws = ActiveSheet
Dim scanPart As Variant
Dim sPart As String
Dim FoundRow As String
scanPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number
sPart = scanPart
MsgBox sPart
Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = Range("A1", Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find(123456789012#, LookIn:=xlValues, lookat:=xlWhole)
MsgBox FindRow.Row
End Function
Thanks in advance for any help!
Dana
The code as written in your post will NEVER work, as the syntax is not correct, especially in the .Find statement. Perhaps that issue was throwing you in the
wrong direction, thinking it was a data type issue.
Please see this code. Tested on both string and numerical values.
Option Explicit
Sub Test()
Dim sPart As String
sPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number
MsgBox ReturnRowNumber(sPart)
End Sub
Function ReturnRowNumber(sPart As String) As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'be more explicit than 'ActiveSheet
Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = ws.Range("A1", ws.Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find(sPart, LookIn:=xlValues, lookat:=xlWhole)
If Not FindRow Is Nothing Then ReturnRowNumber = FindRow.Row
End Function
From your code and the description of your problem it sounds like what is happening is you are defining a variable as Variant, but that Varian is really a String (thus, never use Variant unless you have a very specific reason to).
Something like:
Dim Foo as Variant
Dim Bar as Long
Foo = "123"
Bar = "123"
Is basically like saying:
Dim Foo as String
Dim Bar as Long
Foo = "123"
Bar = CLng("123")
The reason why this is important is because:
12345 <> "12345"
They are different values. One is a Long representation, the other is a String representation.
The solution to your problem? It depends on how your initial values are stored. If you are using String representations of your barcodes then you want to declare your barcode as a String, and use that String to search. It seems though that you are actually storing whole numbers, and if that is the case you will want to declare your barcode as a Long.
If, by some curse, you are storing Barcodes as Strings and as whole numbers, well you will either need to choose one or the other, or you will need to use a more robust find method (I recommend dictionaries).
Function ReturnRowNumber()
Dim ws As Excel.Worksheet
Set ws = ActiveSheet
Dim scanPart As Variant
Dim sPart As String
Dim FoundRow As String
Dim xlCell as Range
scanPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number
sPart = scanPart
Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = Range(Range("A1"), Range("A1").End(xlDown))
Set xlCell = ActiveCell
Application.ScreenUpdating = False
SearchRange.Select
Set FindRow = SearchRange.Find(What:=sPart, After:=ActiveCell, SearchOrder:=xlByRows)
xlCell.Select
Application.ScreenUpdating = True
MsgBox FindRow.Row
End Function
It looks like your find function was not formatted properly. The code above works perfectly for me

Excel VBA Find column name of first blank cell in row

I am working in excel. I need to be able to find the first blank/empty cell in row 20 starting from column A. The return should be the actual column namei.e. AB, AAD, etc. What I am going to do is paste a value into this cell. Here is a picture of it with that row highlighted in green.
Dim wkb As Excel.Workbook
Dim wks2 As Excel.Worksheet
Dim strMSG As String
Dim LastRow As Long
Set wkb2 = ThisWorkbook
Set wks2 = wkb2.Sheets("Daily")
columnNumber = wks2.Cells(20, wks2.Columns.Count).End(xlToLeft).Column
Your query of: -
to find the first blank/empty cell in row
Is not answered by your code wks2.Cells(20, wks2.Columns.Count).End(xlToLeft).Column. Its a subtle but significant difference.
.End(xlToLeft) or .End(xlUp) is often used to find the last used cell in a row/column, a common requirement. To find the first used cell you either want to check each one or create a range based on all blank cells in a range, and look at the first item in that range.
The below code did it for me, and included the column reference as a letter.
Public Sub Sample()
Dim wkb2 As Excel.Workbook
Dim wks2 As Excel.Worksheet
Dim strMSG As String
Dim StrColumn As String
Set wkb2 = ThisWorkbook
Set wks2 = wkb2.Worksheets("Daily")
StrColumn = Replace(wks2.Range("20:20").SpecialCells(xlCellTypeBlanks).Cells(1, 1).Address, "$", "")
StrColumn = Left(StrColumn, Len(StrColumn) - 2)
Set wks2 = Nothing
Set wkb2 = Nothing
End Sub
I have a function that converts a column number to its letter, but I will try and convert it to simple code.
dim colLetter as string
Dim vArr
vArr = Split(Cells(1, columnNumber).address(True, False), "$")
colLetter = vArr(0)
I am not sure how well it works standalone. The function that I use is:
Public Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).address(True, False), "$")
Col_Letter = vArr(0)
End Function
And I know that works. Use whichever you like!

Excel VBA inserting value from different Worksheet using file path

I wanted to recall a value from different worksheet that contains "-" (hyphen) in folder/file name without changing the name of the folder/the file. (want to use the file path)
Sub file_path()
Dim strFolder As String
Dim CopySheet As String
Dim po, iu As String
Dim DestinationSheet As Variant
Dim qwer As Workbook
Dim poi As Workbook
Dim por As Variant
CopySheet = InputBox("path of workbook you recall")
Set qwer = Workbooks.Open(CopySheet)
po = InputBox("sheet name")
iu = InputBox("Range")
por = qewr.Sheets(po).Range(iu).Value
ActiveCell.Value = vals
po.Close
End Sub
I tried using this code but I keep getting error when i put the value for "por" variant. How can I fix this?

Copying & Pasting a Date Value in a VBA Macro

I have written a macro to copy and paste several cell values from one worksheet to another. This has been successful on all fields except one containing a date value.
For example when the cell contains '08-Jan-14' the pasted value is an integer '41647'.
How do I ensure the pasted value received by the new worksheet will be in date format?
Sub place_data(Source As Worksheet, Destination As Worksheet, Optional WorkbookName As String,
Optional rowToWrite As Integer)
Dim iSourceHeader As Integer
Dim iCol As Integer
Dim lSourceEnd As Long
Dim lDestEnd As Long
Dim rSource As Range
Dim rDestination As Range
Dim rngFrom As Excel.Range
Dim rngTo As Excel.Range
Set rngFrom = Workbooks(WorkbookName).Sheets(Source.Name).Range("D51")
Set rngTo = ThisWorkbook.Sheets("Self Test Summary").Range("A" & rowToWrite)
rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues
You are just pasting the values and not the formats. You need to add one more line after pasting
rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues
rngTo.Numberformat = "DD-MMM-YY"
Another Way
rngTo.Value = rngFrom.Value
rngTo.Numberformat = "DD-MMM-YY"
replace:
rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues
with:
rngFrom.Copy rngTo

Take all cells in an Excel Worksheet in as String (VBA)

Basically I'm creating a excel app that, when run, will prompt the user to point at the a specific excel file, and it will take the location in as a string, that works fine. What I am not sure how to do is choose a range in the active worksheet and take the value in each cell and combine them into 1 string.
This is my code so far:
Option Explicit
Sub locate_file()
Dim file As String
Dim sheet1_95 As String
Dim theRange As Range
'prompt user for location of other excel sheet'
file = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
'test input of location'
Workbooks("testing input file.xlsx").Sheets("location").Activate
Range("A1") = file
'activate the workbook and sheet'
Workbooks("95%.xlsx").Sheets("DT").Activate
'Testing retrieving cells as string'
Set theRange = Range("A2:A4")
'how do i retrieve values in this range and combine them into 1 string?'
End Sub
What I am not sure how to do is choose a range in the active worksheet
By using the Application.InputBox function (as opposed to VBA.InputBox):
dim r as range
set r = application.inputbox("Select a range", Type:=8)
and take the value in each cell and combine them into 1 string.
By looping through the cells:
dim c as range
dim s as string
for each c in r.cells
s = s & c.value
next
'Testing retrieving cells as string'
Set theRange = Range("A2:A4")
'how do i retrieve values in this range and combine them into 1 string?'
Dim c as int, x as int
Dim strValue as string
c = therange.rows.count
strValue = vbnullstring
for x = 1 to c
strValue = strValue & theRange.cells(x,1).value
next x