Adding Data Validation to an excel spreadsheet using vb.net - vb.net

I am trying to add Data Validation to a spreadsheet using VB.NET with data that is on Sheet2. I am able to open the workbook with both sheets and the data I need on Sheet2, but the problem I am having is populating the Data Validation Dropdown on Sheet1. I am trying to create the Data Validation in Column A and the first 700 rows of Sheet1, and the data on Sheet2 will be in Column A with the first 800 rows.
I have tried the following and it creates the Data Validation, and it only has one value inside - "Sheet2" -since I am not actually putting a formula in here.
With xlsSheet 'worksheet
.Range(String.Concat("Sheet1!$A$2:$A$700")).Validation.Add(XlDVType.xlValidateList, & _
Formula1:="Sheet2")
End With
When I try the following, it creates the excel sheet with the columns and the data on sheet2, but it doesn't create the data validation.
With xlsSheet 'worksheet
.Range(String.Concat("Sheet1!$A$2:$A$700")).Validation.Add(XlDVType.xlValidateList, & _
Formula1:="=Sheet2!$A$2:$A$800")
End With
I was looking for examples online and found http://www.get-digital-help.com/2011/10/05/add-values-to-a-data-validation-list-vba/ but this too did not work for me.
How can I get the data from A2:A800 in sheet2 into data validation on sheet1 from A2:A700?
Any help would be appreciated.

If anyone would like to know a solution to this, here is what I did:
I first created a named set that I will use to reference in the datavalidation dropdown.
workbooks.Names.Add(Name:="Working", RefersToR1C1:="=Work2!R2C1:R700" & "C1")
'Named set needed for data validation.
With xlsSheet
.Range("Work1!$A$2:$A$700").Validation.Add(XlDVType.xlValidateList, Formula1:="=Working")
End With
In the code above you can see Work1 is the name of the sheet I want to insert the Data Validation Dropdown in. The named range is getting the data from Work2 - my second sheet.

Related

While Creating Userform I'm facing some errors

Hi I'm Uma Mahesh from Vijayawada, I am trying to create a Employee UserForm.
In that I'm trying to Load Country Mobile Code by Selecting Country.
I created a Excel Workbook with three Sheets named as 1. Raw Data, 2. Home, 3. Filled Data
I loaded Country Names with Mobile Codes in Raw Data and hided it and I want Data to be filled in Home Sheet or Filled Data Sheet
Now I'm in Home Sheet and trying to fill data, but it is showing runtime error, but when I'm in Raw Data Sheet and trying to fill data, there is no error appearing.
How to resolve it?
Will any one please respond??
Following is my code
Dim CC2 As Long, i as Long
**CC2 = Sheets("Sheet1").Range("a1", Range("a1").End(xlDown)).Cells.Count**
For i = 1 To CC2
If Cmb1.Value = Sheets("Sheet1").Cells(i, 1).Value Then
Sheets("Sheet2").Range("a1").End(xlDown).Offset(1, 0).Value = Sheets("Sheet1").Cells(i, 2).Value
End If
Next i
The above is my code and the error showing in line 2
Thank you
Uma Mahesh
FunThomas is right. Try activating the sheet when performing your code.
Example.
Worksheets("TestSh").Activate 'Performe your code here and then just activate the next sheet. Just keep in mind that if you use the name of the sheet in Excel, someone could change it in your workbook and mess up your code.
Different methods to activate a sheet.
Worksheets("Sheet1").Activate
Worksheets("1").Activate
I custom name my sheets in the Properties Window, it makes it easier, and if someone changes the name or the order of the sheets, it won't mess up my code.
For example, you could name your sheets ShOriginal and ShNew, so its neater:
ShOriginal.Activate
ShNew.Activate
Here's a site that explains it better:
https://analysistabs.com/vba-code/worksheet/m/activate/
Let us know if we were able to help you!

Using Excel Macro to truncate in specified cells but still allow data entry in the same cell

I am completely new at working with Excel Macro but have encountered a spreadsheet issue at work that I believe may be solved by using a Macro formula. I need to truncate a value (cannot be rounded) to the second decimal place in specified cells. However, the values to be truncated must be entered by a third party using a template spreadsheet that the Macro formula will be attached to. Is there a way to lock a formula to certain cells while allowing data entry in those cells that would be updated by the formula? I have used the Macro below to successfully truncate cell values and now just need a way to force that Macro to run every time new data is input into the specified cells.
Sub TruncateSelection()
ActiveSheet.Range("A1:A15").Select
Dim Cell As Range
With Selection.Cells
.Value = Evaluate("IF(ROW(1:" & Selection.Cells.Count & "),TRUNC(" & .Address & ",2))")
End With
End Sub
You will need to bind your script to the Worksheet_Change event in VBA
This MSDN article should shed some light on what you'll need to do.
https://msdn.microsoft.com/en-us/library/office/ff839775.aspx

How to use VBA to VLOOKUP across workbooks, sheet by sheet

my apologies in advance - I am relatively novice with VBA
I have a report generated daily, with several sheets (Let's say Sheet1, Sheet2, Sheet3). These sheets have tables where I track notes for each order line, and I would like to vlookup the notes from day to day.
The issue I am facing: I need to be able to repeat this for Sheet1, Sheet2, and Sheet3, pulling from the previous day's respective Sheet1, 2, 3, and repeat this day by day.
Is there a way for the vlookup to recognize my active sheet name, e.g. Sheet3, and look back at the prior day's workbook and pull from that Sheet3?
Is this something that can be done?
Thank you for any help
Before you get where you are trying to go you need a consistent starting point. I would create a new workbook and hold your logic there. You don't need your macro to be in the workbook that your data is.
Like this:
Using named ranges is best in case you want to change the look of your control workbook, and you can create formulas for the file names so that they default to your current day and prior workday.
The code would launch both workbooks. Hopefully this is a valid starting point for you and gives you an idea how to proceed.
Sub mergeThese()
path_New = Range("path_New")
path_Old = Range("path_Old")
file_New = Range("file_New")
file_Old = Range("file_Old")
Workbooks.Open path_Old & file_Old
Workbooks.Open path_New & file_New
End Sub

Looping values from excel sheet in UFT

Please can anyone help me !!!!
I have this function that calls data from an external excel workbook and places the value into the application under test, I need it to loop through all the values in the excel sheet row by row
Below is the link to the framework and the function I am trying to use and need it to loop through the values in the excel sheet. PLS HELP.
MANY THANKS
http://www.automationrepository.com/2013/08/designing-hybrid-framework-in-qtp-part-3/
'================================================================================
'Function name - fnFetchDataFromExcelSheet
'Description - This function retrieves the data from the excel sheet based upon the column name
'================================================================================
Function fnFetchDataFromExcelSheet(strColumnName)
Dim sColValue
'Initialize the return the value to "empty" string
fnFetchDataFromExcelSheet = "empty"
'Add a new blank sheet into the QTP data table
'Excel data will be copied into this blank sheet
DataTable.AddSheet("dtDataSheet")
'Import the data from the excel sheet into the QTP data table
DataTable.ImportSheet sExcelWorkbookPath, sSheetName, "dtDataSheet"
'Find the value from the data table
sColValue = DataTable.Value(strColumnName, "dtDataSheet")
'Return the value back to the calling function
fnFetchDataFromExcelSheet = sColValue
'Remove Reference
DataTable.DeleteSheet("dtDataSheet")
End Function
This function you have posted from a site is not correct logically!
First, You need to pass both sSheetName & strColumnName to the function.
Even if we ignore that it is a miss by mistake Or Assume that sSheetName is a global variable (so need to pass this to function), For each and every call to this function, It imports a sheet to the datatable, gets the value in the first row for the given cell and agains delete it(!!!). It would affect the performance very badly. You will also get the same value everytime!!
I would say, Ignore the function. Follow these steps.
1) First you need to import the excel into your datatable.
DataTable.ImportSheet /path/to/excel, /name/of/the/sheet, "dtDataSheet"
/path/to/excel & /name/of/the/sheet should be in " "
2) get the Row count
intRowcount=DataTable.GetSheet("dtDataSheet").RowCount
3) Use a simple For loop
For iLoop = 1 To intRowcount
DataTable.GetSheet("dtDataSheet").SetCurrentRow iLoop
Msgbox DataTable.value("ColumnName","dtDataSheet") 'Replace the msgbox with what you want
Next

VBA Duplicate Sheet with code

So I have an Excel workbook that contains a template sheet for products.
I am creating new products on a Main Page via a UserForm which creates a new sheet, copies the cells from the template sheet and pastes them onto the new sheet and then fills in the rest with the information given from the UserForm.
The problem I am having is that I want each one of these newly created sheets to contain code for Worksheet_Change. I do not know of a way in which to create the new sheet and also give it the necessary code which is currently in the template sheet.
Any help will be very appreciated!
Don't copy the cells to the new sheet.
Instead, copy the entire sheet.........any sheet code will copy with it.
EDIT#1:
For example, if worksheet "Template" contains some worksheet code, then:
Sub marine()
N = Sheets.Count
Sheets("Template").Copy After:=Sheets(N)
End Sub
will copy that sheet, code & all.