I am struggling to write an effective macro that will find a cell with error in column, then replace that cell with value of the first non-empty cell without error below it (there might be consecutive error cells) then loop for 12 columns.
The code I have below replaces each of the error cells in all 12 columns, but not in a consistent manner: some cells will indeed be filled by the next cell below that contains a number, but some cells end up with value of the second next cell below that contains a number. I can't tell where the problem in my code is.
Option Explicit
Sub ClearError()
ThisWorkbook.Sheets("WorkSheet1").Activate
Dim c, x, z As Integer
Dim y As Long
For z = 3 To 14 Step 1 ' Start with column 'C' and do for total of 12 columns
x = 999
For c = 1 To x Step 1
If IsError(Cells(c, z)) Then
Cells(c, z) = Range(Cells(1, z), Cells(x, z)).Find(y, _
After:=Cells(c, z), LookIn:=xlValues, SearchDirection:=xlNext).Value
End If
Next c
Next z
End Sub
If you could offer any insight or advice, I would greatly appreciate it. Thank you for your time! Ante
Try reversing your loop.
For c = x to 1 Step -1
If IsError(Cells(c, z)) Then
Cells(c, z) = Cells(c + 1, z)
End If
Next c
Related
My code adds one column after each existing column.
I need to add 14 columns. I want this to start by adding the columns after column 2 for each column with data. I believe my current code covers that.
Dim z As Integer
Columns(2).Select
For z = 2 To 20
ActiveCell.EntireColumn.Insert
ActiveCell.Offset(0, 2).Select
Next z
Loop backwards, and no need to Select.
This doesn't check if there is any data in the column.
Sub x()
Dim z As Long
For z = 20 To 2 Step -1
Columns(z).Resize(, 14).Insert
Next z
End Sub
Welcome.... Your code inserts 19 columns.. As you have selected second column First column is inserted between A and B. Now if you want to insert first columns between B and C then Select third column first. and then Z from 4 to 17..Oh you mean 14 columns each time after col B? Then...
Dim z As Integer
Columns(3).Select
For z = 4 To 22
Range(ActiveCell, ActiveCell.Offset(0, 13)).EntireColumn.Insert
'ActiveCell.EntireColumn.Insert
ActiveCell.End(xlToRight).Offset(0, 1).Select
'ActiveCell.Offset(0, 2).Select
Next z
I want a formula for column E3 depend on column A3,B3,C3 and D3. If multiple columns show yes or single column show yes I want show as below in pink. Need to combine shctin names which show "yes".Formula required for column E .End result shoul show as pink color.
Check it out
Sub Button1_Click()
Dim x, y, z
For x = 3 To 15
For y = 1 To 4
If UCase(Cells(x, y)) = "YES" Then
z = z & "_" & Cells(2, y)
End If
Next y
Cells(x, 5) = Right(z, Len(z) - 1)
z = ""
Next x
End Sub
User Defined Function,the function code belongs in a regular module.
Place this formula in E3 and drag down,
=Get_It(A3:D3,2)
Function Get_It(a As Range, Return_Row As String)
Dim c As Range
Dim s As String
For Each c In a.Cells
If UCase(c) = "YES" Then
s = s & "_" & Cells(Return_Row, c.Column)
End If
Next c
Get_It = Right(s, Len(s) - 1)
End Function
There's another way using formulas. A little ugly but ok as a non-VBA alternative.
I'm currently in need of extracting cell values from a correlation table that fit a certain profile, e.g. "<0.6". This could an easy enough manual task if it weren't for the fact that I'm running correlation coefficients for >4,000 items. The idea for the output would be to create another table with a concatenate column showing the items involved and another column containing the value for the correlation of those items.
I'd imagine that VBA would be the way to go but maybe there's some other faster and simpler way I could be overlooking.
Any help would be much appreciated!
Thanks in advance :)
Sub extract()
Worksheets("Matrix").Select
Range("A1").Select
Dim Row As Long
Dim Col As Long
x = 1
y = 1
i = 0
Worksheets("Paste").Cells.ClearContents
Worksheets("Paste").Range("A1") = "X"
Worksheets("Paste").Range("B1") = "Y"
Worksheets("Paste").Range("C1") = "Value"
Worksheets("Matrix").Activate
Row = Worksheets("Matrix").Range("A1", Worksheets("Matrix").Range("A1").End(xlDown)).Rows.Count
Col = Worksheets("Matrix").Range("A1", Worksheets("Matrix").Range("A1").End(xlToRight)).Columns.Count
If Row <> Col Then
MsgBox "ERROR: Matrix is not symmetrical, can't be a correlation matrix"
Exit Sub
End If
For x = 1 To Row
For y = 1 To Col
If Cells(y, x) > 1 Then
Cells(y, x).Copy
Worksheets("pegar").Range("C2").Offset(RowOffset:=i).PasteSpecial xlPasteValues
Worksheets("paste").Range("B2").Offset(RowOffset:=i).Value = y
Worksheets("paste").Range("A2").Offset(RowOffset:=i).Value = x
i = i + 1
End If
Next y
Next x
Worksheets("paste").Select
MsgBox "values extracted"
End Sub
I have an Excel file that contains some data in column B, now i wish to categories the data in A column like serial number first 1 to 5 again starts from 1 to 5 until the data ends,
for example in below format
1 A
2 B
3 C
4 D
5 E
1 F
2 G
3 H
4 I
5 J
1 K
2 L
3 M
4 N
5 O
I do not have existing code for above task please help me.
you can use the following
put 1 in the Cell A1
put =IF(OFFSET(A2,-1,0)=5,0,OFFSET(A2,-1,0))+1 in cell A2
double click in the bottom corner of cell A2, this will repeat the function for all cells in column A
hope that it will help you
Use some code
Sub DoItGood()
Dim rws As Long, rng As Range, t As Range
Columns(1).ClearContents
rws = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("A1:A" & rws)
x = 1
For Each t In Range("A1:A5")
t = t + x
x = x + 1
Next t
Range("A1:A5").AutoFill Destination:=rng, Type:=xlFillCopy
End Sub
You can get a repeated list of numbers from 1 to n downwards in rows with the following approach:
=MOD((ROW(A1)-1),n)+1
Take the integer remainder of the division row number (starting with 0) and n. You will get 0,1,2,...,n-1,0,1,2,...,n-1,0,1... To this add 1.
In your case n is 5:
=MOD((ROW(A1)-1),5)+1
filled downwards.
I've modified the vba function below to suit my needs.
I have many workbooks with sheets that contain 4500+ rows, and I use the function to search for two given values (as boundaries). Then, it selects the rows as the range. Finally, do whatever on that range. The function:
Function GeoM(A, B)
Application.Volatile
Dim x As Integer
Dim y As Integer
Dim rng As Range
x = Application.WorksheetFunction.Match(A, Range("B:B"), 0) ' looking in col B
y = Application.WorksheetFunction.Match(B, Range("B:B"), 0) ' looking in col B
Set rng = Range(Cells(x, 18), Cells(y, 18)) 'Im working on col 18
GeoM = Application.WorksheetFunction.GeoMean(rng)
End Function
The problem is, this code works just fine except with GeoMeann. I noticed when the range of data is relatively small (number of data cells) it returns a value. However, if the range is larger than approx. 126 cells, it returns #value!.
I'm stuck and working on solving this issue. Is the GeoMean function limited to a given number of data?
Thanks
There appears to be a 170 character limit on my testing for earlier Excel versions (I tested in xl03), validated in this Mr Excel thread
(Xl10 worked fine on the longer dataset)
I also tried:
Using Evaluate
Using a 1D array
failed samples
Dim X
Set rng1 = Range("A1:A171")
MsgBox Evaluate("GeoMean(A1:A171)")
X = Application.Transpose(rng1)
MsgBox Application.WorksheetFunction.GeoMean(X)
to no avail.
So I think your two workarounds are either:
Inserting a formula via VBA into Excel and using this result
As per the MrExcel thread use the derivation of GeoMean, ie =EXP(AVERAGE(LN(Range)))
Suggested Approach
MsgBox Evaluate("EXP(AVERAGE(LN(A1:A171)))")
Thanks to brettdj, I fixed the function and it works now:
Function GeoM(A, B)
Application.Volatile
Dim x As Integer
Dim y As Integer
Dim rng As Range
Dim LnValue As Double
Dim count As Integer
x = Application.WorksheetFunction.Match(A, Range("B:B"), 0) 'look in col. B
y = Application.WorksheetFunction.Match(B, Range("B:B"), 0) 'look in col. B
Set rng = Range(Cells(x, 18), Cells(y, 18)) 'set range of rows on col# 18
Do
LnValue = LnValue + Math.Log(Cells(x, 18)) 'calculates sum of ln(value)
x = x + 1
count = count + 1 'calculates the total number of values
Loop Until x > y 'stop when x (upper row#) is greater than y (lower row#)
GeoM = Math.Exp((1 / count) * LnValue) 'GeoMean formula
End Function
This function searches a specified column for two values as upper and lower limits (Note: that means you shouldn't have repeated values in that column. In another words, the column should have unique values). Then, it finds the GeoMean of the values on other column, which has values fall in the same range of rows.