I am trying to accomplish something apparently simple but not for me. I’m new to all this and I would like to see if I can get any far with this first project. Basically I have, say 3 text boxes with a string in it (name) and a button next to them. Then there is another text box which will hold all 3 names so that I could copy as one string into clipboard. Many tutorials out there refer to concatenating the strings however I’m still unable to find a decent answer. To be fully honest I am trying to achieve this task in VBA from within Excel 2010. So, my 3 names are actually in 3 different cells… Next to each of them I have a button with a macro attached (but vb will also be good for me) that will add the name into another cell. The buttons works ok but my problem is that I can’t get them to concatenate in Excel. Every button just overwrites what’s was there before. Any help? Suggestion? Tips? Mind you I can only be able to follow if the answer is simple and well explained as I am new to all of this. Thank you.
If you had 3 strings in A1, B1 and C1, you could get the concatenated string in D1 by putting in the formula `=CONCATENATE(A1,B1,C1)' in the cell D1. If you want to do it in VBA,
Public Sub MyConcatenation()
With ThisWorkbook.Worksheets(1)
.Cells(1, 4).Value = .Cells(1, 1).Value & .Cells(1, 2).Value & .Cells(1, 3).Value
End With
End Sub
If you won't take offense: You need to pick up an introductory book or website to learn VBA (it is not VB or VB.Net). It will be a long, uphill struggle to learn it through asking questions on forums.
Related
I am new to selenium VBA and I am using selenium web drive to find the member's name in the web page and update the information for that record.
I can search the name and click the record by using the below code:
bot.FindElementByXPath("//*[#class='x2h' and text()='Peter
Melo']").Click
I would like to replace Peter Melo by using the Excel spreadsheet and Peter Melo is in the ActiveSheet Cell A1
So I tried to replace Peter Melo ActiveSheet.Range("A1").Value by using the above code but it doesn't work.
May I know how can I use the name available in Excel and search it in the Web?
Thanks in advance for any help or advice I might receive!
Can you try using
Sheet1.Cells(1, 1).Value instead of ActiveSheet.Range("A1").Value .
Range method usually used for a Range.its not the right approach to pass the single cell value.However ActiveSheet.Range("A1").Value should work. but if its not working then I assume in the active sheet it might not have any value. Can you try
Worksheets("Sheet1").Range("A1").Value
Where Sheet1 is the Sheet where you store "PeterMello".
But this should definitely work Sheet1.Cells(1, 1).Value
Your code should be
bot.FindElementByXPath("//*[#class='x2h' and text()='"+Sheet1.Cells(1, 1).Value+"']").Click
or
bot.FindElementByXPath("//*[#class='x2h' and text()='"+Worksheets("Sheet1").Range("A1").Value+"']").Click
I have a workbook that copies data from one workbook to another. This aspect works fine. I have a few cells which are causing me an issue as i need the value to come through as double its original value.
So basically one workbook has the radius and when it comes into the next workbook it needs to be the diameter, so i need to do a x2 somehow. I have tried doing a few things with no luck, i have also looked on the web and there doesnt seem to be a clear cut answer.
My code is this: -
OpenBook.Sheets("Input 2").Range("C39").Copy
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").PasteSpecial xlPasteValues
WHich works great as a simple copy from one workbook to paste to another. But i want to do something like this: -
OpenBook.Sheets("Input 2").Range("C39").Copy
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").PasteSpecial xlPasteValues *2
Any ideas?
I have seen this on the web Operation:=xlMultiply but dont understand how it works. I also found a lot of sites and people sayings its impossible. I am hoping they are wrong.
Thank you in advance.
Steven
Skip the clipboard and transfer the value, multiplying by 2:
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").Value = OpenBook.Sheets("Input 2").Range("C39").Value * 2
I hope someone will be able to help me sort this one out because I've been crawling the Internet for some time now and could not find any obvious reason why the behavior of my macro under Excel works for some users (including me of course) and does not work on some other users' laptops.
My objective:
Write from a VBA SubRoutine a vlookup formula in one cell. Based on that vlookup, either it finds something and it displays a text A or the vlookup throws an error and it then displays a text B.
The situation
Here is the formula if we were writing it directly in the sheet from the end-user's perspective: =IF(NOT(ISERROR(VLOOKUP(C2,DataSet2!$A$2:$A$12,1,0))),"Direct Report","Team member of direct report")
When we copy/paste that formula on a PC where the macro fails directly in the spreadsheet. It works.
When we run the macro actually trying to incorporate that formula in that exact same cell, it fails and throws an error 400.
Here is now the VBA code attempting to write the above formula in the cell:
formulaString = "=IF(NOT(ISERROR(VLOOKUP(C" & CStr(counter + 2) & systemListSeparator & "DataSet2!$A$2:$A$" & CStr(2 + totalDirectReportsCount) & systemListSeparator & "1" & systemListSeparator & "0)))" & systemListSeparator & """Direct Report""" & systemListSeparator & """Team member of direct report"")"
Selection.Offset(counter, 0).Formula = formulaString
So the above 2 statements, as you'll have understood probably, are included in a loop and for every single row I have, I want to add that formula in the first cell from the currently selected cell.
What drives me crazy is that: it works fine on my PC, it works fine on my colleague's PC who is in Finland, but on another colleague's PC also in Finland, it does not. I have Windows 10 and Office 2016, she has Windows 10 and office 2016 as well...
So I am really wondering if there could be some regional settings or Excel settings or System Settings which could make HER Excel not interpret my "formulaString" as viable for her laptop. I have made sure to fetch the list separator to avoid those usual regional settings easy traps but would there be other similar traps I am not aware of?
Thanks a lot for any hints I could follow to progress on my investigations.
Kind regards,
Nicolas C.
This is really a tricky point, but fortunately VBA provides an excellent solution.
Simply write the formula in Excel, select the cell and run the following code:
Public Sub TestMe()
With ActiveCell
Debug.Print .Formula
Debug.Print .FormulaLocal
Debug.Print .FormulaR1C1
End With
End Sub
In German Excel, this is what you would get:
=SUM(C1+D1)
=SUMME(C1+D1)
=SUM(RC[2]+RC[3])
Then rebuild your formulas, using only .Formula or .FormulaR1C1 and the result from the immediate window. Thus your colleagues from all over the world would be really happy. As noted in the 3 examples, the first and the third one always give the same answer, following the en_US format.
Another option is that Excel and VBA do not like your formula, because of the decimal separator in Finland, being different from the decimal separator in the USA. See How to add hardcoded float number values to the formula using VBA to change it a bit.
Solution
Nicolas C. edit: All the above comments and hereby answer have helped me solving my issue. Thank you very much guys for your prompt reaction and feedback. You really saved my day. So the root cause was indeed that I had tried to capture the end-user's system preferences on his/her locale and regional settings and making use of his/her list separator (which was ; actually) within the VBA script which was a bad move since, as mentioned by #AxelRichter and #Vityata, within a VBA script, .Formula needs always to be written in the en_US format, that is english function names, commas, etc.
I have now removed my user related list separator and replaced it with traditional commas and it solved my issue.
I am an old man trying to compare dates from two different files in Excel.
My code is:
Dim i As Integer
For i = 1 To 7
IF Data_for_Processing.xls (“SOLARLOG”). Cells (i,”A”).Value = Day_Conversion_chart.xls (Sheet1).Cells (i+2, “B”) Then
Cells(7+I, “B”)=”Equal”
Else: Cells(7+i, “B”) = “NotEQ”
End If
Next i
Will anyone help?
First of all, I would recommend following #simoco 's advice - Reading the documentation will provide the tools for solving future problems and give you the basics. As well as that, I would recommend using vba help. And another very useful tool for trying commands could be recording macros and analyzing them later on the editor.
So, first you need the code to be inside a macro. It will look like this (I chose the name TestMacro):
Sub TestMacro()
'Code here.
End sub
You should take into account that when your macro is running, it does so from the sheet you are working in, so any partial references to cells, etc. will refer to the book you are in and the sheet you are in when you run the macro. It is possible to select another sheet or book, and if you do so manually or on the code, references will be applied on that selected book or sheet.
What I call partial references here are those that read simply "ThisCell" instead of "ThisBook.ThisSheet.ThisCell". I would say that using partial references, though, is appropriate in a vast majority of cases.
The way your code is organized, be careful to run it from the workbook where you want the data to be in the end, let's say, in your 'main' workbook, so that the final values will be written there..
Another comment: whenever you want to use another file, this file must be open (as far as I know), while you are using it. In your code, you don't open any file, so the macro will not work if you don't open ALL referenced workbooks manually prior to running the macro.
When you want to reference something inside something, you mostly use ".". Please read the documentation - You will get a better idea of how this works. For this example:
Book.Sheet.Cell.Value is the structure you are using.
A book can be referenced as Workbooks("Name.xls") if it is open.
A sheet or worksheet can be referenced as Sheets("Name") or Worksheets("Name"), and also with numbers, like Sheets(1) or Worksheets(1). The differences can be seen on vba editor help. I chose Sheets(...) for your example.
Be careful with the symbols. I guess this was probably my problem, but I have to mention it just in case: When I copied your code, instead of double quotes (""), I got something different, that Excel would not recognize. If for any reason you are using different symbols, Excel might not understand.
A cell can be referenced in various ways too. The Range object is used extensively, and if you want to use "A1", "C44", etc., it's probably better to go for it. I like using Cells(,) as you did, when possible. As far as I know, this works nice with numbers (Cells(1,2), for example), which may be very convenient too. I kept this on your code, changing "A" and "B" and writing 1 and 2, respectively.
With all these changes incorporated:
Comments:
'NOTICE THAT ALL WORKBOOKS MUST BE OPEN.
'["A"] changed to [1]
'[Sheet1] changed to [1]
'["B"] changed to [2]
'Data_for_Processing.xls(“SOLARLOG”).Cells(i, 1).Value
'becomes Workbooks("Data_for_Processing.xls").Sheets(“SOLARLOG”).Cells(i,1).Value
'Day_Conversion_chart.xls(1).Cells(i + 2, 2).Value
'becomes Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i+2,2).Value
'["B"] changed to [2]
And one possible solution:
Sub TestMacro()
Dim i As Integer
For i = 1 To 7
If Workbooks("Data_for_Processing.xls").Sheets("SOLARLOG").Cells(i, 1).Value _
= Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i + 2, 2).Value Then
Cells(7 + i, 2) = "Equal"
Else: Cells(7 + i, 2) = "NotEQ"
End If
Next i
End Sub
This worked on my Excel example - I hope it is of some help.
Regards.
Sub TEST()
If cells(i, "R").Value <> "UK" Then
cells(i, "R").Interior.ColorIndex = 3
End If
End Sub
If I run this program it throws application defined error \
I am new to Excel (beginner)
How to correct this error!!!
Thanks In advance
I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.
This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!
First you have to define i variable
for example: Dim i as variant