Newbie here need help to find the next emptycell in a range from the activecell.row....
Sub FindNextCell()
'
'Macro1 Macro
Cells.Find(What:="", _
After:=Range("F2:I22")(Cells(ActiveCell.Row, _
Columns.Count).End(xlToLeft).Offset(0, 1)), _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate
End Sub
When using the Find method, it's better to set the result as a Range.
Of course, there is a possibilty the Find will not return any results (in case it doesn't find another empty cell in the specifed range), that's why we add If Not EmptyRng Is Nothing Then.
Code
Option Explicit
Sub FindNextCell()
Dim FindRng As Range, EmptyRng As Range
' define the Range to search according to ActiveCell current row
Set FindRng = Range("F" & ActiveCell.Row & ":I" & ActiveCell.Row)
' COMMENT : need to cheat a little to get the first cell founds in the searched range
' by starting from the last column in the range, Column "I"
' Otherwise, will return the second cell found in the searched range
Set EmptyRng = FindRng.Find(What:="", after:=Cells(ActiveCell.Row, "I"), _
LookIn:=xlValues, lookat:=xlWhole)
' found an empty cell in the specified range
If Not EmptyRng Is Nothing Then
EmptyRng.Select
Else ' unable to find an empty cell in the specified range
MsgBox "Unable to find an empty cell in " & FindRng.Address & " Range"
End If
End Sub
Related
Trying to copy data from one Excel spreadsheet to another (from New_data to report).
In the New_data spreadsheet I find the second time System (hence why I start the search below the first one at N21) appears then I need to copy all data below it from columns b - k until I hit blank cells. How do I get the amount of rows to only capture filled cells?
Range("B584:K641") needs to be dynamic.
Sub CopyWorkbook()
Range("N21").Select
Cells.Find(What:="system", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("B584:K641").Select
Selection.Copy
Application.WindowState = xlNormal
Windows("report.xlsx").Activate
Range("A2").Select
ActiveSheet.Paste
Windows("new_data.csv"). _
Activate
End Sub
Try the next code please. It should be very fast (if I correctly understood where to be searched for 'system', starting with what...). The code assumes that "new_data.csv" is the csv workbook name. If not, you must use its real name when defining shCSV sheet:
Sub CopyWorkbook()
Dim shR As Worksheet, shCSV As Worksheet, lastRow As Long, systCell As Range, arr
Set shR = Workbooks("report.xlsx").ActiveSheet 'use here the sheet you need to paste
'it should be better to use the sheet name.
'No need to have the respective sheet activated at the beginning
Set shCSV = Workbooks("new_data.csv").Sheets(1) 'csv file has a single sheet, anyhow
lastRow = shCSV.Range("B" & rows.count).End(xlUp).row
Set systCell = shCSV.Range("B21:B" & lastRow).Find(What:="system", _
After:=shCSV.Range("B21"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If systCell Is Nothing Then MsgBox "No 'sytem' cell has been found...": Exit Sub
arr = shCSV.Range(systCell, shCSV.Range("K" & lastRow)).Value
shR.Range("A2").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
Try:
Sub test()
Dim LR As Long
Dim Ini As Long
LR = Range("B" & Rows.Count).End(xlUp).Row 'last non empty row in column B
Ini = Application.WorksheetFunction.Match("system", Range("N21:N" & LR), 0) + 20 'position of system after n21
Range("B" & Ini & ":K" & LR).Copy
'''rest of your code to paste
End Sub
Note that this code is searching word system only in column N. If it's somewhere else, you'll need to adapt the MATCH function
I set a range to equal the filtered range and start a loop to count how many none empty cells occur until the first empty cell in column B.
Sub CopyWorkbook()
ThisWorkbook.Sheets("new_data").Activate
Range("N21").Select
Dim rng As Range
Set rng = Cells.Find(What:="system", After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
Dim i As Double
i = rng.Row
Do Until ThisWorkbook.Sheets("new_data").Range("B" & i) = vbNullString
i = i + 1
Loop
i = i - 1
Range("B" & rng.Row & ":K" & i).Select
Selection.Copy
Application.WindowState = xlNormal
Windows("report.xlsx").Activate
Range("A2").Select
ActiveSheet.Paste
Windows("new_data.csv").Activate
End Sub
I found a Stack Overflow question that was helpful in finding an answer. Find cell address
Is there a way to search for a value in a list such as in column A, identify the row number of that value (for e.g. it could be Row 40 of Column A), go to a different column (e.g. Row 40, Column B) and then insert data into that column but through the macro (so it is done automatically).
I have tried to play around using the code below but cannot seem to get anywhere;
Dim Cell As Range
Dim RowNumber As Long
Columns("B:B").Select
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
RowNumber = Cell.Row
If cell = "celda" Then
'find row, go to Column B of that row, and insert "abc"
Else
'do it another thing
End If
I found the code above in the link below;
How to find a value in an excel column by vba code Cells.Find (not my own work but props to the creator)
You need little modification to your code. Try below...
Sub FindAndAdd()
Dim fCell As Range
Dim strSearch As String
strSearch = "Harun"
With ActiveSheet
Set fCell = .Columns("A:A").Find(What:=strSearch, _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
'Lookat:=xlWhole to match whole word.
End With
If Not fCell Is Nothing Then
fCell.Offset(0, 1) = "New Value"
Else
MsgBox "No match found.", vbInformation, "Search Result"
End If
End Sub
I want to implement a VBA Code to work with multiple different sheets, for example: it starts by looking for a certain number in the first row, once it's found, it jumps to that column and types a certain formula into the 2nd cell in that column, so far it works good, But the issue is that I wanna make it to Autofill that formula down the column if the first cell in that row contains data.
Like if A2 is not blank, continue the auto fill the cell in the active column (let's say the active column is D, then the it would fill the Cell d2 if a2 not blank) and stops once the cell in A Column is blank .. etc
So, Is it possible?
Sub Macro1()
Rows("1:1").Select
Selection.Find(What:="156", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Offset(1).Select
ActiveCell.FormulaR1C1 = _
"= "Formula will be here""
End Sub
Might be best to save a copy of your workbook before running the code below.
Maybe something like this is what you're after. If Find found something in column D, then it puts the dummy formula in the range D2:D?, where ? is whatever the last row in column A is (which I think is what you described).
Option Explicit
Sub Macro1()
Dim ws As Worksheet
Set ws = ActiveSheet ' Can you refer to the workbook and worksheet by name? Please do if possible
With ws
Dim cellFound As Range
Set cellFound = .Rows(1).Find(What:="156", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
If cellFound Is Nothing Then
MsgBox ("The value was not found in the first row of sheet '" & ws.Name & "'. Code will stop running now")
Exit Sub
End If
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(cellFound.Offset(1), .Cells(lastRow, cellFound.Column)).FormulaR1C1 = "=""Formula will be here"""
End With
End Sub
Check this simple code, I think it will satisfy your needs:
Sub Macro1()
Rows("1:1").Select
Selection.Find(What:="156", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
col_Num = ActiveCell.Column
total_Rows = WorksheetFunction.CountA(Range("A:A"))
Cells(2, col_Num).Select
Cells(2, col_Num) = "=Put your Formula here"
begin_Cell = Cells(2, col_Num).Address(False, False)
end_Cell = Cells(total_Rows, col_Num).Address(False, False)
Selection.AutoFill Destination:=Range(begin_Cell & ":" & end_Cell)
End Sub
There are easier ways to locate a column header label although I'm unclear on why you are using the LookAt:=xlPart argument. It seems to me you should not have to 'wildcard' the search but a 'wild card' search can be accommodated.
Sub FindnFill()
dim m as variant
with worksheets("sheet1")
m = application.match("*156*", .rows(1), 0)
if not iserror(m) then
if not isempty(.cells(2, "A")) then
.range(.cells(2, m), .cells(.rows.count, "A").end(xlup).offset(0, m-1)).formula = _
"=""formula goes here"""
else
.cells(2, m).formula = _
"=""formula goes here"""
end if
end if
end with
end sub
Find & Fill
About the Find Method
It is best practice to always set the following three parameters, because they
are saved each time they are used.
LookIn - If you use xlFormulas, it will find e.g. =A2 + 156, which you don't want.
LookAt - If you use xlPart it will find e.g. 1567, which you don't want.
SearchOrder - Not important, since a row is being searched.
Additionally SearchDirection is by default xlNext and can therefore safely be omitted.
Additionally MatchCase is by default False and can therefore safely be omitted.
Additionally SearchFormat - To use it you previously have to set Application.FindFormat.NumberFormat and can therefore safely be omitted.
The Code
Sub FindFill()
Const cDblFind As Double = 156 ' Found Value
Const cLngRow As Long = 1 ' Found Row Number
Const cVntColumn As Variant = "A" ' First Column Letter/Number
Const cStrFormula As String = "=RC[-1]+5" ' Formula
Dim objFound As Range ' Found Column Cell Range
Dim lngRow As Long ' First Column Non-empty Rows
With ActiveSheet.Rows(cLngRow)
' Check if cell below cell in First Column and Found Row is empty.
If .Parent.Cells(cLngRow, cVntColumn).Offset(1, 0).Value = "" Then Exit Sub
' Calculate First Column Non-empty Rows.
lngRow = .Parent.Cells(cLngRow, cVntColumn).End(xlDown).Row - cLngRow
' Find cell in Found Row containing Found Value.
Set objFound = .Find(What:=cDblFind, After:=.Cells(.Row, .Columns.Count), _
LookIn:=xlValues, LookAt:=xlWhole, Searchorder:=xlByRows)
If Not objFound Is Nothing Then
' Write Formula to Found Column Range
objFound.Offset(1, 0).Resize(lngRow).FormulaR1C1 = cStrFormula
End If
End With
End Sub
I'm hoping to achieve the following:
Take user input via the Input box.
Search the table headers for that text.
Filter the found column to remove all blank cells (Leaving just the cells with data in.)
I've progressed a bit with a script I found, to give the input box, search the table header and select the found cell.
I need to merge into this the step of filtering the column of the found cell. If I record the steps it filters the same column no matter what I search for, so I think I need a way of reading back the found cell details and choosing that column to filter out blanks.
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
I now have it working using the following code, the only error I now get is a 1004 (WorksheetFunction class) error if I cancel the InputBox :-
Sub Find_First()
Dim i1 As Integer
Dim FindString As String
Dim Rng As Range
Dim rngData As Range
Set rngData = Application.Range("A2").CurrentRegion
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
i1 = Application.WorksheetFunction.Match(FindString, Application.Range("A2:CZ2"), 0)
Rng.AutoFilter Field:=i1, Criteria1:="<>"
End Sub
Looks like you really need the autofilter worked out:
I've done this in a similar scenario:
Dim i1 as Interger
Dim rngData as Range
Set rngData = ws.Range("A1").CurrentRegion
Using Match to find my column number matching FindString
i1 = Application.WorksheetFunction.Match(FindString, ws.Range("A1:CZ1"), 0)
rngData.AutoFilter Field:=i1, Criteria1:="<>"
How to get the value from a cell on an active sheet and look it up on a non active sheet and then rename the value?
Dim rw As Long
rw = ActiveCell.Row
If Sheets("Home").Range("D" & rw).Value = "Tender" Then
With Worksheets("Time Allocation").Columns("B:B")
Set cell = .Find(What:=.Range("B" & rw).Value, After:=Range("B" & rw), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell.Value = "test"
Else
cell.Value = "test"
End If
End With
End If
I have tried using cell.value = "test" but this causes an error:
object variable or block with variable not set
please can someone show me where I am going wrong?
The bad news is that you cannot .Select one or more cells on an inactive worksheet. The good news is that there is absolutely no requirement that you do so and in fact it is generally less efficient than directly addressing the cell, cells or column(s).
Dim rw As Long, cell as range
rw = ActiveCell.Row
If Sheets("Sheet1").Range("D" & rw).Value = "Tender" Then
With Worksheets("Sheet2").Columns("B:B")
Set cell = .Find(What:=Sheets("Sheet1").Range("B" & rw).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell = "test" '<~~the default property is the .Value
Else
MsgBox "cannot test. not found. cell is nothing and cannot be referenced."
End If
End With
End If
The way you are bouncing around between two worksheets and referring to the ActiveCell property like it is on one worksheet sometimes and another worksheet other times is a little confusing. I'm not sute I got the What parameter right in the Range.Find method.
See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.