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
Related
A little background about what I'm trying to do:
My userform is going to be used to grade employee training with the results being added to an excel spreadsheet. The checkboxes in my form are representative of the different kinds of mistakes they can make and each mistake ideally has a quantity box that will specify how many times each mistake was made if its box is checked. I need both in one cell, as each row is 1 test for each employee. Here is an example of my userform:
Mistakes Screen Shot
All checkboxes and textboxes share the same number and I've already programmed them to automatically insert a 1 quantity if the box is checked/empty if unchecked. (Of course the quantity must be editable in case a mistake type is duplicated.)
So far I was able to use a loop with a string to get the checkboxes to put their captions into the single cell using this code:
Dim CheckBox As Control
Dim Mistakes As String, delimiter As String
For Each CheckBox In Me.Frame_Mistakes.Controls
If TypeOf CheckBox Is MSForms.CheckBox Then
If (CheckBox.Value) Then
Mistakes = Mistakes & delimiter & CheckBox.Caption
delimiter = " | "
End If
End If
Next
With Sheet1
.Cells(emptyRow, 4).Value = Mistakes
End With
However, so far I have been unable to figure out how to get the quantity to be added at the end of each mistake preceding the delimiter. I would prefer it if I could get the string to be in this format: Mistakes Format in Excel
If my intentions are unclear, I apologize. I am incredibly new and honestly surprised I was able to make it this far. Please and Thanks!
So, if I understand you correctly, You want the output of the Mistakes cell to read
[Caption of checked box](x[Number in Text Box])|[Caption of checked box](x[Number in Text Box])...
If that is so, you simply need to add a snippet of code to the end of your 'Mistakes' variable so that it reads:
Mistakes = Mistakes & delimiter & Checkbox.Caption & "(x" & TextBox.text & ")"
Where things get difficult is that you will need to differentiate between text boxes so that only the one that applies to the relevant checkbox is being used. You could do this in a number of ways, such as passing the Textbox as an argument, or with a switch case to name a few.
Another problem is making sure that the textbox only uses numbers. The way that I accomplish this is with a combination of the IsNumeric() and Val() functions. You first check if the value is numeric, then store it in an int using the Val() function. Since you only need it for a string, though, using the IsNumeric() function alone should be fine.
If you need more specific clarification, I would need to know exactly what you are looking for.
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.
I have a question about freezing certain cells. But first let me explain the situation.
I have made a search box in my excel sheet and when you search for a letter or word; the results show up in cells below the search box. Now I want to freeze those cells, so that wherever I go in my sheet. I can always use the search box and see the results.
The cells for the searchbox and results are B2:B25. Those are the ones I want to freeze. Also the only sheet I want to use this on is the sheet "Reading". On the rest of my sheets I do not use a search function.
So my question(s) is : Do I need to put the code inside a module or on that sheet, and how do I do this?
Now I have tried the following
Range(Cells(2,2), Cells(25, 2)).Select
ActiveWindow(or maybe Reading?).FreezePanes = True
Inside a module. But it did not work and I do not know what else to do.
Any help is much appreciated! Since I am very new to VBA.
Almost there. Problem is that the 'range' and 'cells' needs to be directed to the 'Reading' sheet, like so:
Sheets("Reading").Range(Sheets("Reading").Cells(2,2), Sheets("Reading").Cells(25, 2)).Select
ActiveWindow.FreezePanes = True
but if it always is B2->B25, why not use:
Sheets("Reading").Range("B2:B25").Select
ActiveWindow.FreezePanes = True
This should work. Select is not very desirable, because it is very slow, but in this case, you need to (as far as I know).
EDIT
BTW, you can do this from within a code module or from within a sheet, but if you choose to do it from within a sheet, you cannot select another sheet. So just use the range.
EDIT 2
whoopsy, typo corrected. 'Sheet' should have been 'Sheets'
I have recorded a macro in Excel that takes data from certain cells in one page of a spreadsheet and copies them to page in a different order. it does each cell individually, and is quite long.
However it only does it for one row of data in the first page.
How do I make this single macro into a loop that copies the same data but from each row into the new page?
I haven't included the macro code since it is very long but can do so if necessary.
Thanks
You could omit your code to only show the relevant part, or code a mockup macro to address only and only your problem.
Lets imagine your problem as a sub, which in this case is highly omitted:
Sub OmittedSub()
' Do stuff
End Sub
You can create new sub to call it many times, this new sub would be the one you would be calling instead:
Sub LoopOmittedSubs()
Dim i As Integer
' Loop to call your macro routine multiple times
For i = 1 To 100
OmittedSub
Next
End Sub
In case you would need to pass a value, for example your macro does not know which row to affect and you would need to tell it, you can pass the loop variable like this:
Sub OmittedSub(iRow As Integer)
' Do stuff for row number iRow
End Sub
Sub LoopOmittedSubs()
Dim i As Integer
' Loop to call your macro routine multiple times
For i = 1 To 100
OmittedSub i
Next
End Sub
This answer is the very basics of VBA. I dont know how to answer your question better without knowing what you already tried, how you tried, etc...
Raybarg provided you basic solution to that problem. I am not sure what do you mean by "certain cell", does it mean they have certain value or there are in regular order like A5, A10, A15?
In both cases you probably will use Raybarg's code and conditional statements.
You should include your code, it gives some hints on what you actually want to achieve and where is error.
I've attempted to track down and solve this error for hours now and I just can't figure it out. Here's the setup.
I have one excel workbook which has two sheets in it: "Input" and "Calculations". I've written in a few custom functions to calculate certain things. These functions are used in the "Calculations" sheet, but reference cells in the "Input" sheet. Now if I'm just using the sheet itself everything works perfectly fine and the functions work.
However, I have a second excel workbook which interacts with the first. I have a macro in the second workbook which attempts to define values in the "Input" sheet of the first workbook. However, when it does this, suddenly the functions don't work. When I attempt to trace the error of the cell in the "Calculations" sheet, and attempt to go to the cell in "Input" sheet, it claims the reference is invalid.
I have no idea what the problem is. At first I thought it maybe had something to do with the name of the first workbook (which was "Log K Calculator 7.0.0.xlsm") but I've tried changing that and I get the same problem. Here is the macro in the second sheet which attempts to change the values in the first:
Sub macro()
Dim logK As String
Dim this As String
logK = "Log K Calculator 7.0.0.xlsm"
this = ThisWorkbook.Name
Workbooks(logK).Activate
Workbooks(logK).Sheets("Input").Cells(11, 4).Value = Workbooks(this).Sheets(1).Cells(1, "B").Value
Workbooks(logK).Sheets("Input").Cells(12, 4).Value = Workbooks(this).Sheets(1).Cells(2, "B").Value
Workbooks(logK).Sheets("Input").Cells(14, 4).Value = Workbooks(this).Sheets(1).Cells(3, "B").Value
End Sub
I know this thread is 4 years old but, just in case anybody is still searching for the answer like I was, for me this error was caused not by my code but by the fact that I was running the procedure by clicking on a textbox which I had copied from another workbook. I had attached the procedure/macro to the textbox but had forgotten that there was already a hyperlink assigned to the same textbox. Removing the hyperlink fixed the issue.
You might want to check how Excel is treating whatever value is sitting in your external workbook. E.g. is it treating the numbers as text? You could check this by entering, say '10000, '300 and '600 manually in the input sheet (forcing it to treat them as text but display numbers), and see if you get the same ref error.
If you do, then casting the value into a typed variable (Long, Decimal, you name it) would probably fix the issue. Or at least make VBA choke on the incorrectly formatted ones (e.g. some have spaces in them, which might explain the apparent inconsistency)