Is there a way to use the find function with chang

2019-08-10 02:28发布

问题:

What I am trying to do is to have a user input field on one sheet, where the user types text into a cell. They can then run a macro which selects a different sheet and then uses that inputted text in a find function.

Once the text is found, the entire column which the text is found needs to be selected, along with all the cells to the right up to the next populated cell (in the row the 'found' word/phrase is located). These cells will then be deleted.

I am having two main problems. I can't seem to find how to use the Find function with a changing value (I say value but I mean whatever word, phrase, etc. is put into the user inputted field). I am able to only search for a specific value and can't reference the user inputted field instead.

The second problem is selecting the column- once the Find function finds the user inputted value/word/phrase, it selects the cell it is in. I would like it to select the entire column (and the columns to the right of it up to the next populated cell in a specific row).

This is a similar problem as I can't seem to reference the column in which the user inputted value/word phrase is found as it will constantly change. I am only able to select a certain column, the problem being the column it selects will probably not need to be deleted the next time the macro is run as the sheet it is finding the values in is constantly updated.

Here is the code I have so far.

Sub Macro3()
    Sheets("Instructions").Select
    Range("C53").Select
    Selection.Copy
    Sheets("Forecast").Select
    Cells.Find(What:="4150T83G01", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Columns("BW:BY").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
End Sub

So basically, the range C53 will never change. I always want that to be where the user inputs the word/phrase/value to be found.

Where the code says Cells.Find(What:="4150T83G01"- I need this to not refer to that specific value but instead refer to the user inputted word/phrase/value on the "Instructions" sheet in cell C53.

And finally, where the code says Columns("BW:BY").Select- I need this to refer to the column in which the user inputted word/phrase/value is found as well as the columns to the right of the found cell up until the next populated cell, not the specific columns BW:BY.

I hope that makes sense, and any help is greatly appreciated! Thank you!

回答1:

It seems that you're trying with a recorded macro? To do these kind of things you will need more than just recording macros.

Sub Macro3()
Dim strFind As String
Dim rngFind As Range
Dim i As Integer

strFind = Sheets("Sheet2").Range("C3").Value
Set rngFind = Sheets("Sheet1").Cells.Find(What:=strFind, LookAt:=xlPart)
'check if value is found
If Not rngFind Is Nothing Then
    i = 0
    Do While rngFind.Offset(0, i + 1) = ""
        i = i + 1
    Loop
    rngFind.Resize(1, i + 1).EntireColumn.delete Shift:=xlShiftToLeft
End If

End Sub