Excel function doesn't write anything - excel

Goodmorning, i wanted to do a macro in Excel that multiply the element on the right for every element on the left (then put the result in another column), till a blank cell.
This is an example of the elements:
[enter image description here][1]
And this is what i try to write with no result...it seems like the cycle goes well, but it doesn't write anything ... could you please help me out? Anyway, sorry for my bad English, i hope i made it clear.
Thank you.
Sub test()
Range(A1).Select
x = 1
y = 1
Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
If IsEmpty(Ax) = True Then
y = x + 1
End If
If IsEmpty(Ax) = False Then
Cells(E, x).Value = Cells(A, x).Value * Cells(D, y).Value
End If
x = x + 1
ActiveCell.Offset(1, 0).Select
Loop
End Sub

you have to put the cell references as strings.
VBA will interpret Range(A1) as whatever variable A1 is set to. It is NOT the "A1" cell in your spreadsheet.
Correct is
Range("A1").Select
Similarly if you have a variable x, and want to get the cell reference A1 then you need to do something like this:
x=1
if IsEmpty("A" & x) then ...
Also, the Cells(row,column) function uses row first, then column.
I'm assuming you have "On Error Resume Next" somewhere in your code, as what you have written should throw up a lot of errors.

Related

How to highlight duplicates rows based on more than 30 columns?

I am trying to highlight duplicate rows by Excel VBA code but I'm receiving a "'Range' of Object _Global' failed" error and I'm hoping someone can help as to why.
Sub Duplicate_Row()
Dim x As Integer
Dim Y As Integer
Dim Z As Integer
x = 2
Y = x + 1
Z = 2
Do While Range("A:AA" & Y) <> ""
Z = Z + 1
Do While Range("A:AA" & Y) <> ""
If Range("A:AA" & x) = Range("A:AA" & Y) And Range("A:AA" & Y).Interior.ColorIndex = xlColorIndexNone Then
Range("A:AA" & x).Activate
Selection.End(xlToLeft).Select
Range("A" & x).Select
Range(Selection, Selection.End(xlToRight)).Select
'Selection.Interior.Color = vbYellow
Selection.Interior.ColorIndex = Z
Range("A" & Y).Select
Range(Selection, Selection.End(xlToRight)).Select
'Selection.Interior.Color = vbYellow
Selection.Interior.ColorIndex = Z
End If
Y = Y + 1
Loop
x = x + 1
Y = x + 1
Loop
MsgBox "Done"
End Sub
How to highlight duplicates in rows with msgbox ? I also want a msgbox for duplicate rows and highlight only those rows in which all the details are duplicate.
As my late father used to say "When you can do something the difficult way, then why do it the easy way?" and this is exactly what I see when reading your question. :-)
I have made a small Excel file, highlighting duplicates, using conditional formatting, as you can see in this screenshot:
(It's all about column "A", the others are just there for explanatory reasons: "B" contains the formula results, "C" contains the =FormulaText(Bn) formula.)
The formula this is based on, is: =COUNTIF($A$1:A1,A1)-1.
Some explanation: this formula (you can see the results in the second column) counts the amount of times that An (which is what you get by dragging down A1) is present in the range A1:An (the first cell A1 is fixed because of the dollar-signs in the formula), minus one (in order not to count the cell value itself. As a result, when the value gets found before (a duplicate), the value gets 1 or higher, and when the value does not get found before, you get a zero. Conditional formatting can be based on formulae: TRUE or non-values get highlighted.

Fill many cells in a row with one value

I'm trying to fill 1299 cells in a row with the value 0530 using the following code:
Sub FillValues()
Dim X As Integer
For X = 2 To 1300
Worksheets("Table1").Range("B" & X).Value = "'0530"
Next X
End Sub
For some reason it doesn't work and I don't know why. The error is "Index beyond the valid range."
Please try (no loop):
Worksheets("Table1").Range("B2:B1300").Value = "'0530"

Looping through columns then doing UCase

I have some code that loops through columns to find a specific ending in a column (_END). If it finds that, then it will loop through the rows in that column, changing date formatting. This works as intended and I am having no issues with it. However, I need to UCase the rows as well. Right now, it would output a date like "01-Jan-2016". However, I need it to be "01-JAN-2016". I have code below that is giving me trouble.
lngColHeaders = Cells(5, Columns.Count).End(xlToLeft).Column
For X = 1 To lngColHeaders
If (Right(Cells(5, X), 4)) = "_END" Then
LastRowDates = Cells(Rows.Count, X).End(xlUp).Row
For ZZ = 6 To LastRowDates Step 1
Cells(ZZ, X).NumberFormat = "dd-MMM-YYYY"
UCase (Cells(ZZ, X))
Next ZZ
End If
Next X
Like I said, it seems to format them correctly, but UCase (Cells(ZZ,X)) seems to do nothing. Any help is much appreciated.
I think you'll need to format the cell as text, and then do your UCase, along with a Format.
Cells(ZZ, X).NumberFormat = "#"
Cells(ZZ, X).Value = UCase(Format(Cells(ZZ, X).Value, "dd-MMM-YYYY"))

Using VBA function to check colomn has NA error and create an IF-Then Else output

I am trying to write a VBA function but getting an error , can someone take a look and suggest where I am going wrong.
What the function is doing is looking at Column 1 and checking if its error then assigning value of No to X. Same with Y on a different column.
If either of them have a value or name then the output is Yes, else its a No
Added picture, I am getting a value error right now. But if column F and G are same column H is a yes, if either one has a name H is yes. Only when both are N/A H is a No.
Function Checkmapping(x As Variant, y As Variant)
If x = "#N/A" Then
x = "No"
End If
If y = "#N/A" Then
y = "No"
End If
If x <> y Then
Checkmapping = "Yes"
Else: Checkmaping = "No"
End If
End Function
Calling via
BigBen: I know the formula is easy, but want a VBA function , just so I can add more complex logic later, if there are 10 parameters the formula will get crazy.
several issues here:
A cell containing an #N/A error is not the same as a cell containing the text "#N/A"
If a cell contains an error value (including #N/A comparing it to a string in VBA will throw a run-time error
There is a typo: Checkmaping = "No" vs Checkmapping = "No"
Refactored code
Function Checkmapping(x As Variant, y As Variant)
If Application.IsNA(x) Then
x = "No"
End If
If Application.IsNA(y) Then
y = "No"
End If
If x <> y Then
Checkmapping = "Yes"
Else
Checkmapping = "No"
End If
End Function
Based on your statement Only when both are N/A H is a No. the above won't give that result. Then try this
Function Checkmapping(x As Variant, y As Variant)
Checkmapping = "Yes"
If Application.IsNA(x) Then
If Application.IsNA(y) Then
Checkmapping = "No"
End If
End If
End Function
Bonus: based on your comment I know the formula is easy, but want a VBA function , just so I can add more complex logic later, if there are 10 parameters the formula will get crazy.
Function Checkmapping(r As Range)
Dim cl As Range
Checkmapping = "No"
For Each cl In r
If Not Application.IsNA(cl) Then
Checkmapping = "Yes"
Exit Function
End If
Next
End Function
Call it like =Checkmapping(F10:G10) or =Checkmapping(F10:Z10) to check more cells
And just for completeness, you can do this with a Formula for a variable number of cells
=IF(SUMPRODUCT(IFNA(F10:G10,-1)+1),"Yes","No")
Enter as an Array Formula (complete with Ctrl-Shift-Enter rather than just Enter) and then copy down

Repeat a command with diffrent data in VBA

I´m developing an Excel Makro right now.
Wanted to know, how I can repeat some lines of code using different data, without copy and paste.
Looking forward for your answers : )
This is my current code:
Sub deleteredundant()
Windows("Test1.xlsm").Activate
If Range("A6") = Range("A7") And Range("B6") = Range("B7") Then
Range("A7:B7").Select
Selection.ClearContents
End If
End Sub
It sounds like #BruceWayne has pointed in you in the right direction for what you need - removing duplicates.
As #Apurv Pawar shows you can use a loop, but he's selecting cells (if any code says select or activate a cell just don't.... you can reference a cell without selecting).
Another way is to have a procedure to remove the cells, and another procedure to tell it which workbook, worksheet and cell to look at.
Sub DeleteRedundant(CheckRange As Range)
If CheckRange = CheckRange.Offset(1) And CheckRange.Offset(, 1) = CheckRange.Offset(1, 1) Then
CheckRange.Offset(1).Resize(, 2).ClearContents
End If
End Sub
The code above will accept a range that is passed to it.
It will check if the passed cell is equal to the cell below itself:
CheckRange = CheckRange.Offset(1)
It will then check if the cell to the right of the passed cell is equal to the value below that:
CheckRange.Offset(, 1) = CheckRange.Offset(1, 1)
If the values match it will look at the cell below the passed cell, resize that to two cells wide and clear the contents of those two cells:
CheckRange.Offset(1).Resize(, 2).ClearContents
With this procedure in place we can pass it various range references to operate on:
Sub Test()
DeleteRedundant Workbooks("Excel Worksheet1.xlsx").Worksheets("Sheet1").Range("A6")
DeleteRedundant Workbooks("Excel Worksheet2.xlsx").Worksheets("Sheet2").Range("D5")
'Pass every other cell to the procedure in a loop.
'So will pass A2, A4, A6 - Cells(2,1), Cells(4,1) and Cells(6,1)
Dim x As Long
For x = 2 To 20 Step 2
DeleteRedundant Workbooks("Excel Worksheet1.xlsx").Worksheets("Sheet1").Cells(x, 1)
Next x
End Sub
But, as #BruceWayne says - you probably just need the Delete Duplicates button on the data ribbon.
try the below.
Sub deleteredundant()
Windows("Test1.xlsm").Activate
x = 1
Do While Range("a" & x).Formula <> ""
If Range("A" & x) = Range("A" & (x + 1)) And Range("B6" & x) = Range("B7" & (x + 1)) Then
Rows(x & ":" & x).Select
With Selection
.Delete EntireRow
End With
End If
x = x + 1
Loop
End Sub

Resources