does anyone one how to instead of static range:
'~~> Copy the range
wsI.Range("A1:K50").Copy
To change it to dynamic where A1:A50 is calculated depending on non-blank cells.
So if Sheet1 has 23 lines and the rest of them are blank so range would be: A1:K23?
just in case someone looks for an answer:
issue - when copying data from other sheet where I had formulas, pasted cells are blank, but not actually blank. It holds some sort of 'nothing'...
solution ,SpecialCells(xlCellTypeVisible).Copy to copy data and paste in the same workbook with Paste:=xlPasteValuesAndNumberFormats (it holds date value as this was something I needed). Then used "find and replace" code to clear "blank, but not blank" cells.
'~~> Find "" and replace with pneumonoultramicroscopicsilicovolcanoconiosis
Worksheets("paste").Range("A1:K500").Cells.Replace What:="", Replacement:="pneumonoultramicroscopicsilicovolcanoconiosis", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'~~> Find pneumonoultramicroscopicsilicovolcanoconiosis and replace with ""
Worksheets("paste").Range("A1:K500").Cells.Replace What:="pneumonoultramicroscopicsilicovolcanoconiosis", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Related
I am trying to create a code that does two things:
First, the code will search for a column that contains a given text within the worksheet.
Afterwards, the code will search for the row with the last non-blank cell in the worksheet, while remaining on the same column as 1.
picture example
As shown in the example above, the code would first search for the text "FIRST" in A1. Afterwards, it will search for the last non-blank cell in the worksheet, which is C11. However, it should remain in the column that contains "FIRST", so that only the row changes. The final result would be A11.
So far, I found the code that lets me do 1.
Cells.Find(what:="FIRST",
after:=ActiveCell,
LookIn:=xlFormulas,
LookAt:=xlPart,
SearchOrder:=xlByRows,
SearchDirection:=xlNext,
MatchCase:=False,
SearchFormat:=False).Activate
and another code that lets me do 2.,
Cells.Find(what:="*",
after:=Range("A1"),
LookAt:=xlPart,
LookIn:=xlFormulas,
SearchOrder:=xlByRows,
SearchDirection:=xlPrevious,
MatchCase:=False).Activate
However, I am unable to use them together to achieve my intended goal.
Any suggestions to answer this would be appreciated. Thank you!
I was wondering about your starting point of ActiveCell, so I reversed the methods and used the last row as the starting point to search for FIRST.
Dim r As Long, c As Long
With Worksheets("sheet1")
r = .Cells.Find(what:="*", after:=.Cells(1, "A"), _
LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False).Row
c = .Cells.Find(what:="FIRST", after:=.Cells(r + 1, "A"), _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
.Cells(r, c).Activate
End With
I'm trying to use "*" as a wildcard for data in Column F. All the data is in the same format and length as "Pie**" where each "*" represents (0-9)
Example: Pie22, Pie71, Pie15, Hot22, Hot41, Hot98
When I try the code below, I run into the issue where CoffeeXX is finding and replacing "Hot" in "hotdog" and it becomes "chickendog".
Is there a was to fix this so that the .find sees the wildcard instead of just picking anything that contains the text?
Set PieXX = Columns("F").Find(What:="Pie**", Lookin:=xlvalues)
If not PieXX is nothing then
Columns("F").Replace What:"Pie**", replacement:="Hotdog", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If
Set CoffeeXX = Columns("F").Find(What:="Hot**", Lookin:=xlvalues)
If not CoffeeXX is nothing then
Columns("F").Replace What:"Hot**", replacement:="Chicken", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If
For each cell in Range("F1", Range("F1").End(xlDown))
If cell.value like "pie??" then cell.Replace "pie", "hi", LookAt:=xlPart
Next
This is untested
I am trying to make a userform that can bring up data using an ID number.
I am trying to reference a text box and select it, and then using it as a reference to fill out the Time and comments in the sheet. I think the is I cant put "txtID.Value" into the Find function.
Here is an example of my code:
Sheet1.Select
Columns("A:A").Select
Selection.Find(What:="txtID.Value", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.Offset(0, 8).Value = txtTime2
ActiveCell.Offset(0, 9).Value = txtComment2
When using the Find function, it's recommended to use a Range object, and set it to the result. This method allows you to trap a possible scenario where Find failed to find a match in the searched range Sheet1.Columns("A:A").
Also, try to avoid using Select, Selection and ActiveCell, and use fully qualified Range objects (like in the code below).
Code
Dim FndRng As Range
Set FndRng = Sheet1.Columns("A:A").Find(What:=txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not FndRng Is Nothing Then ' successful find
FndRng.Offset(, 8).Value = txtTime2
FndRng.Offset(, 9).Value = txtComment2
Else ' unable to fins the value in txtID
MsgBox "Unable to find " & txtID.Value & " in Sheet1"
End If
Note: if you have this code outisde the User_Form module, then you need to add the User_Form reference when trying to get the txtID.Value.
For eaxmple, let's say the name of your form is UserForm1, then change this line to:
Set FndRng = Sheet1.Columns("A:A").Find(What:=UserForm1.txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
To Make it work, you will have to put the code as below, which is quite self explainatory.
Selection.Find(What:=Userform1.textbox1.value, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
hope this helps
I am trying to search for the date, which I have in cell K1 as =today()....
Whenever I am recording the macro, however, it will continue to use the date which the macro was written. What I am curious about is a methodology of pasting what is on the clipboard into the VBA search function.
So I want to search column A for what is in cell K1. This is what code I have now (the find what is what I just typed to help you all have an idea of what I'm looking for)
Range("K1").Select
Selection.Copy
Columns("A:A").Select
Selection.Find(What:="COPY CONTENTS OF CELL K1", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
This should do it. Note that your original code will throw an error if today's date isn't found. Typically when working with Find() it's safer to test the returned value before trying to do something with it.
Dim f As Range
Set f = Columns("A:A").Find(What:=Date(), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing then
'do something with f
Else
'???? do what
End If
You could replace your entire code with this:
Activesheet.Columns("A:A").Find(What:=ActiveSheet.Range("K1"), After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
What was happening is that there's no way to paste a cell's contents via .Copy directly into a VBA function. So I placed what you wanted to paste into it into the function directly.
I think you will also find this helpful:
How to avoid using Select in Excel VBA macros
I have a worksheet with a formula that returns =NA() under certain conditions. Using VBA, I'd like to find #N/A, but I haven't been able to tweak this answer.
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
I've tried What:=CVErr(xlErrNA), What:=xlErrNA and also What:="#N/A" to no avail.
As an added difficulty, I have to take Dutch into account, so What:="#N/A" probably wouldn't even work in Dutch.
Note. I'm asking this question out of curiosity as I haven't found a method online.
At this moment, I'm calculating which cells contain =NA()
You're looking in the cell formulas. Try looking in the cell values, then I can get this to work using the string "#N/A" for the What argument:
lastrow = .Cells.Find(What:="#N/A", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Please try the below code ,
You should use the loop to read the error.
Sub Test()
If Range("A2").Text = "#N/A" Then
MsgBox "hi"
End If
End Sub
Hi I have another solution,
You have paste these formulas values into another columns as text and
then use the replace code,
Please try the below code.
Sub Tst()
Columns("C:C").Select
Selection.Copy
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
The column C Contains formulas values (Error) .
The problem here is that you're looking for "#N/A" as a value of the cell and it's not the value it's a error indicator, so if you're trying to find a cell wich gives you a error you have to use something like this:
If WorksheetFunction.IfError("Put here a loop to read the cells;"Error")="Error" then
"Write what you desire for cells with error"
end if