I am trying to set values for variables to be used later on in my code based on the column value of a found cell.
Thanks to some existing subject, I was able to find the cell, but I am unable to set its column value to a name.
Here is my code:
Dim rFind As Range
With Range("A1:DD1")
Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
MsgBox rFind.Column
End With
End Sub
The MsgBox returns the correct column number but my attempts at getting it set to a name has failed.
Thanks for your help !
EDIT:
My goal is to create an automatic table with data extracted from another table. I want to use the column number to extract data for each row of my table from the correct column. I currently use a system where I "hardcode" my names for the current column number (e.g.: Publi Const example As Integer = 5). However this is not a flexible solution if my data table were to change (new or removed columns). Finding the column to then set it would solve the issue.
Perhaps this, to name the whole column?
rFind.EntireColumn.Name = "Fred"
Fuller code
Sub x()
Dim rFind As Range
With Range("A1:DD1")
Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
rFind.EntireColumn.Name = "Fred"
Else
msgbox "Not found"
End If
End With
End Sub
Related
I'm trying to filter on a column labeled "Date" across 10 worksheets. The column "Date" may not be in the same column position for every worksheet.
Is there a macro or code I can use to filter on the "Date" column in one easy button push? Instead of going to each individual worksheet and filtering separately..
I would only be filtering on the same date for all worksheets. For example, if I wanted to filter on "9/3/2021", I would go to each worksheet and find the "Date" column and manually filter 10 times.
if you add two simple cells to one of your sheets containing the header text and the value you want to filter you can use the below code to achieve the intended result.
for example my header named "Date" so in my first sheet I wrote "Date" in A3 and the date I want to filter into A4. just keep in mind that since we are searching for the "Date" header in first row either we should put our so called Criteria Range header in another row or since our search ends with first occurrence we can add the new header to one of the columns after our original Date column.
Note:
The Criteria Range table headers must be exactly like the target selection headers for this to work. so here I have one column and one header, and I only selected the Date column.
Sub Test()
Dim Rng As Range
For i = 1 To 4
With Sheets(i).Range("A1:Z1")
Set Rng = .Find(What:="Date", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'################################# New Part ▼
' R will be the address of the found column in Range format (e.g. "F1")
R = Rng.Address(RowAbsolute:=False, ColumnAbsolute:=False)
Dim rgData As Range, rgCriteria As Range
' rgCriteria is the two cells Criteria Range
Set rgCriteria = Sheets(1).Range("A3:A4")
' these next two lines are adding the Date column range to rgData
LastRow = Sheets(i).Cells(Rows.Count, Rng.Column).End(xlUp).Row
Set rgData = Sheets(i).Range(Sheets(i).Range(R), Sheets(i).Cells(LastRow, Rng.Column))
' since I was disappointed in AutoFilter I used Advanced Filter and its even faster
rgData.AdvancedFilter xlFilterInPlace, rgCriteria
'################################# New Part ▲
Else
MsgBox "Nothing found"
End If
End With
Next i
End Sub
That's All!
References:
○ - Thanks to this Excel Macro Mastery channel video. make sure to watch it to get a better understanding of what I wrote.
you can find the "Date" column (which I presume there is a row contains the word "Date") by searching that row (e.g. first row of each sheet) for the word "Date". then you can give the column number to your filtration sub and get your filtering result.
1st Way
Sub Test()
For i = 1 To 10
For j = 1 To 50
If Worksheets(i).Cells(1, j).Value = "Date" Then
' here goes your filtration procedure on j column
Exit For
End If
Next j
Next i
End Sub
which i is the number of worksheets and j is the found column (in first 50 columns)
2nd Way (Faster Way)
Using Range.Find which suggested by #BigBen because:
Looping cell-by-cell is slow and inefficient
Sub Test()
Dim Rng As Range
For i = 1 To 5
With Sheets(i).Range("A1:Z1")
Set Rng = .Find(What:="Date", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
' here goes your filtration procedure on (Rng.Column)
Else
MsgBox "Nothing found"
End If
End With
Next i
End Sub
which i is the number of worksheets and Rng.Column is the found column (in A1:Z1 range)
References:
Microsoft Docs page for Range.Find method
there is another Microsoft Docs Page if you want
to use Application.Match method
I'm trying to copy data from a column in a sheet called "KPI", in cells H6:H100, to a specific row in a sheet named "table". The row depends on two variables in the KPI sheet which user selects from drop downs in C2:D2.
I have managed to get the code to find the right row each time by searching columns A then B in the "data" sheet.But when it comes to the copy paste/transpose column H from "KPI" sheet into the right row on the "table" sheet it throws up a 424 error.
I might be missing something really obvious so any help is appreciated.
Sub copy_transpose()
Dim rng_source As Range
Dim Found As Range, Firstfound As String
Dim rngSearch As Range
Dim Criteria As Variant
Set rng_source = ThisWorkbook.Sheets("KPI").Range("H6:H100")
Set rngSearch = Sheets("Table").Range("A:A")
Criteria = Sheets("KPI").Range("C2:D2").Value
Set Found = rngSearch.Find(What:=Criteria(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Found Is Nothing Then
Firstfound = Found.Address
Do
If Found.EntireRow.Range("B2").Value = Criteria(1, 2) Then Exit Do 'Match found
Set Found = rngSearch.FindNext(After:=Found)
If Found.Address = Firstfound Then Set Found = Nothing
Loop Until Found Is Nothing
End If
If Not Found Is Nothing Then
Application.Goto Found
rng_source.Copy
Sheets("Table").Range(cell.Offset(0, 1), cell.Offset(0, 7)).PasteSpecial Transpose:=True
Else
MsgBox ("Error")
End If
End Sub
I needed more coffee. I hadn't spotted that is was referencing "cell" instead of "found".
Today I learned that "cell" is not a vba function, and was actually something I had dimensioned in my older code, and was the equivalent of "found".
I'm trying to find a cell within a set row with a specific string ("Final Mark", then search the cells below that text. If that cell contains a specific piece of text ("Fail"), I then need a msgbox to pop-up.
This is for a workbook that collects data on marked assignments. I've tried the below code, but it's not working.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FinalMark As Range
Set FinalMark = Rows(5).Find(what:="Final Mark", LookIn:=xlValues, lookat:=xlWhole)
If Not FinalMark Is Nothing Then
If Not FinalMark.Column.Find(what:="Fail", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) Is Nothing Then
MsgBox "Input reason for fail in Further Notes.", vbInformation
End If
End If
End Sub
I was hoping it would search the column where I found the text "Final Mark", but it doesn't.
Change
If Not FinalMark.Column
to
If Not Columns(FinalMark.Column)
I have searched to find the answers to get to where I am but am now stuck! I am a relative beginner with VBA.
I have a Workbook that lists a few hundred orders that we are producing for our customer.
The order details are on the first sheet called "In Progress" and on the 3rd sheet called "StyleData" are more details about each product such as its composition, design reference, SKU etc...
At present my code searches column A on the Data sheet based on the 6 digit style code in the active cell on the In Progress Sheet, then goes to that cell. I have put a MsgBox in purely to put a pause in the code so I know where it has got to.
What I want it to do after finding the style code on the data sheet is return a value on the same row from column H, preferable in a format that the use can select and copy, then it will return to the original cell at the start of the macro.
Code as follows:
Sub get_composition()
Dim item_no As String
Dim data_sheet As Worksheet
Dim found_item As Range
Set Rng = ActiveCell
item_no = ActiveCell.Value
Set data_sheet = Sheets("StyleData")
If Trim(item_no) <> "" Then
With Sheets("StyleData").Range("A:A")
Set found_item = .Find(What:=item_no, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not found_item Is Nothing Then
Application.Goto found_item, True
Else
MsgBox "Nothing found"
End If
End With
End If
MsgBox "Return to Original Cell"
Application.Goto Rng
End Sub
if I understand what you want :
you arrive at cell "found_item" and want to return a value from the same row.
If it's so, you can use method Offset on "found_item"
found_item.Offset() allow you to navigate from the current range
https://msdn.microsoft.com/en-us/library/office/ff840060.aspx
If you are on column A, found_item.Offset(, 1) will return the range on the same line but column B
I know this is a simple fix, but all my searching has not uncovered the answer. I need to create a dynamic hyperlink.
Sheet 1
Column A1 = 3/0/1
Column A2 = 3/0/2
Sheet 2
Column C3 = 3/0/1
Column D3 = 3/0/2
Now, creating a link on Sheet 2 to the appropriate cell in Sheet 1 is easy. Think is, Sheet 1 has the potential to be sorted in a variety of ways. So I need the link dynamic. I need it to find "3/0/1" whether it's in column A1 or A77, or whatever.
I've seen examples using the ADDRESS function within the HYPERLINK function, but can't get it to work. Any ideas?
This might actually be one of those time when you want to use .Selection to govern which cells the macro is enacted upon.
Sub hlink_Sel()
Dim fnd As Range, sel As Range
On Error Resume Next
With Sheets("Sheet1")
For Each sel In Selection
Set fnd = .Columns("A:A").Find(What:=sel.Text, After:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False)
If Not fnd Is Nothing Then
Debug.Print fnd.Address(0, 0, xlA1, external:=True)
.Hyperlinks.Add Anchor:=sel, Address:="", _
SubAddress:=fnd.Address(0, 0, xlA1, external:=True), TextToDisplay:=sel.Text
End If
Next sel
End With
Set fnd = Nothing
End Sub
Just select some cells that are supposed to have their values duplicated in Sheet1's column A and run the macro. Given the odd nature of your sample data, I've used .Text as the search term in the find operation. Note that the .Address function has the external parameter set to true in order that the worksheet name is included.