Storage of Range Values after .Find - excel

This is part of a bigger code that both filters based on input text and then creates subtotals out of the values associated with warranty subtypes, this is applied to several different sheets and it all works.
There is the possibility of many different Warranties subtypes.
I check for each one individually, first for an exact match case for "WarrantyPrefA Total" (this should be on the AJ Column).
If it exists, I want to store that range value inside a variable(GaRangeID), so I can apply an offset of that range to grab the two numerical values present in other columns and paste it on another Workbook.
If it doesn't exist, I want to terminate that find, and find another exact match case.
My guess is I'm messing up the .Find inner syntax to search the correct range.
Dim GaRangeID As Range
Dim WBModeloA1 As Worksheet
Dim WBModeloA2 As Worksheet
Set WBModeloA1 = Workbooks("ModeloAnalisis.xlsm").Sheets("Cartera 1")
Set WBModeloA2 = Workbooks("ModeloAnalisis.xlsm").Sheets("Cartera 3")
'GPB
Dim strSearch As String
Dim lastrow As Long
strSearch = "WarrantyPrefA Total"
lastrow = WBevoDeuM.Range("AJ" & Rows.Count).End(xlUp).Row
Set GaRangeID = WBevoDeuM.Range("AJ1", "AJ" & lastrow).Find(What:=strSearch, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not GaRangeID Is Nothing Then
WBModeloA1.Range("E67") = GaRangeID.Offset(0, -3).Range("A1")
WBModeloA1.Range("E67").Value = WBModeloA1.Range("E67").Value / 1000
WBModeloA2.Range("H91") = GaRangeID.Offset(0, -21).Range("A1")
WBModeloA2.Range("H91").Value = WBModeloA2.Range("H91").Value / 1000
Else
End If
'GPA
Set GaRangeID = Cells.Find(What:="WarrantyPrefB Total", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not GaRangeID Is Nothing Then
WBModeloA1.Range("E65") = GaRangeID.Offset(0, -3).Range("A1")
WBModeloA1.Range("E65").Value = WBModeloA1.Range("E65").Value / 1000
WBModeloA2.Range("H90") = GaRangeID.Offset(0, -21).Range("A1")
WBModeloA2.Range("H90").Value = WBModeloA2.Range("H90").Value / 1000
Else
End If
The reason I show it repeats the same structure but with another find afterwards is because I used to have the "find" part defined in another way.
The following way properly pastes the subtotals onto the other workbook, but I discarded it since it always sets the GaRangeID as the active cell, when the search gets nothing, the active cell remains as the old subtotal found, and so it just pastes the values of WarrantyA onto B.
Cells.Find(What:="WarrantyPrefB Total", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Set GaRangeID = ActiveCell

I don't have much to offer except to simplify your code a bit and use xlValues instead of xlFormulas for the LookIn parameter
Sub Tester()
Dim f As Range
Dim WBModeloA1 As Worksheet
Dim WBModeloA2 As Worksheet
Dim wsData As Worksheet, rngSearch As Range
'use "ThisWorkbook" if it's where your code lives
Set WBModeloA1 = ThisWorkbook.Sheets("Cartera 1")
Set WBModeloA2 = ThisWorkbook.Sheets("Cartera 3")
Set wsData = ThisWorkbook.Sheets("Data") 'the sheet you're searching
'define a range to search
Set rngSearch = wsData.Range(wsData.Range("AJ1"), _
wsData.Cells(Rows.Count, "AJ").End(xlUp))
'if you have multiple of these following blocks you really need a loop
Set f = rngSearch.Find(what:="WarrantyPrefA Total", LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=False)
If Not f Is Nothing Then
WBModeloA1.Range("E67") = f.Offset(0, -3).Value / 1000
WBModeloA2.Range("H91") = f.Offset(0, -21).Value / 1000
End If
Set f = rngSearch.Find(what:="WarrantyPrefB Total", LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=False)
If Not f Is Nothing Then
WBModeloA1.Range("E65") = f.Offset(0, -3).Value / 1000
WBModeloA2.Range("H90") = f.Offset(0, -21).Value / 1000
End If
End Sub

Related

Pasting Rows B2:B16 from Sheet 2 onto Newly Inserted column on Sheet 1

I am attempting to have a macro insert a column on sheet "Runs", and then paste information from sheet "Templates" onto the newly inserted column on a specific Row. I have named the range for row four as "Eight", however, info from templates is pasted onto column A, Row 4, and not the newly inserted column.
Set myWorksheet = Worksheets("Runs")
myFirstColumnT = myWorksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
myLastColumnT = myWorksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
For iCounter = myLastColumnT To (myFirstColumnT + 1) Step -100000000
myWorksheet.Columns(iCounter).Insert
Sheets("Templates").Select
Range("B2:B16").Copy
Sheets("Runs").Select
With Columns(iCounter).Select
Range("eight").PasteSpecial
End With
Next iCounter
The issue is your With doesn't actually do anything and if it did your Range doesn't reference it.
I've removed your loop, it has such a massive step it isn't actually looping anything. Also you don't need a loop for this.
I removed the .column from your .finds because that will cause an error if it fails, I also added in some error checking for if (when) it doesn't find anything.
I removed all instances of .Select because they aren't necessary.
Dim myworksheet As Worksheet
Dim myfirstcolumnt As Range
Dim mylastcolumnt As Range
Dim newcol As Long
Set myworksheet = Worksheets("Runs")
'I'm assuming you need this for a reason other than the loop, otherwise you can remove it
Set myfirstcolumnt = myworksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If myfirstcolumnt Is Nothing Then
MsgBox "TS not found"
Exit Sub
End If
Set mylastcolumnt = myworksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If mylastcolumnt Is Nothing Then 'Avoiding errors
MsgBox "TE not found"
Exit Sub
End If
newcol = mylastcolumnt.Column + 1 'No need to loop to find the column you're making
myworksheet.Columns(newcol).Insert 'use the new column index to add the column
Sheets("Templates").Range("b2:b16").Copy
myworksheet.Cells(4, newcol).PasteSpecial 'We know it's going in row 4 and we have the new column index now
If you want to use your named range you can do myworksheet.Cells(myworksheet.range("Eight").Row, newcol)... Though I suggest changing the name, a range called "Eight" pointing to Row 4 isn't very clear.

VBA - Get row number of a value in a sheet, and then insert a different value into a different column of that same row

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

Trying to find value in range and get its row. Variable not set error?

The code below is supposed to take the value for net in each month, copies it, search for net name in range1(another worksheet) and pastes value in the cell corresponding to that row and column "AA".
This part of code is having issue:
Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Rows.row)
The error is -
object variable or with block variable not set.
what am I doing wrong?
Sub test()
Dim Range2 As Range
Dim lRow As Long
Dim Count As Long
Dim Net As String
Dim Line As Range
Dim Netrng As Range
Dim First As Range
Dim Range1 As Range
Dim wb As Worksheet
Set First = ActiveCell
Set wb = ActiveSheet
Set Range1 = wb.Range(First, First.End(xlDown))
ActiveWindow.ActivatePrevious
ActiveSheet.PivotTables("PivotTable1").PivotFields("Client Code").CurrentPage _
= "BUN"
ActiveSheet.Range("B5").Activate
lRow = Cells(Rows.Count, 1).End(xlUp).row - 6
Set Range2 = Range(ActiveCell.Offset(2, -1), ActiveCell.Offset(lRow, -1))
Set Months = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 2))
Count = 1
While Count <= Range2.Count
Set Line = Range2.Rows(Count)
Net = Line.Value
Line.Offset(0, 1).Copy
ActiveWindow.ActivatePrevious
Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).row)
Netrng.Offset(0, 4).PasteSpecial Paste:=xlPasteValues
Netrng.Value = 0
ActiveWindow.ActivatePrevious
Line.Offset(0, 2).Copy
ActiveWindow.ActivatePrevious
Netrng.Offset(0, 8).PasteSpecial Paste:=xlPasteValues
ActiveWindow.ActivatePrevious
Count = Count + 1
Wend
End Sub
As is, the code is assuming that the Find is successful, which may not always be the case.
To test:
Dim foundRng as Range
Set foundRng = Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not FoundRng is Nothing Then
Set Netrng = Range("AA" & foundRng.Row)
...
End If
Other recommendations:
Avoid using Select and Activate. (and ActiveCell, ActiveWindow, anything Active).
Fully qualify which Workbook and Worksheet each Range is on (helpful reading in the answer on avoiding Select).
While...Wend is old-fashioned. Use a For Each loop.

using a "found" value to define a new range

I am trying to create a dynamic macro in VBA by which I can select a new range starting on a column with the title "Position Number". So my "program" ideally goes to the sheet, finds the range where I want to find this cell that says "Position Number", finds it, gives me the column number (as the column might change) and then it starts on that column to mark a new range and compare it with another sheet. I am so far stuck in the part where I am trying to use the column number I have found to define the new range. I have tried lots of things I found online, but cant fix it.
The error is on:
Set Range1 = Range("'C'& ColNum" & "R1")
I tried a few other variants of this but it does not work or gives me a number as output.
Thanks in advance!
Dim FilledRange As Range
Dim Range1 As Range
Dim Rng As Range
Dim ColNum As String
Worksheets("FILLED Today").Activate
Set FilledRange = Range("a1")
FilledRange.CurrentRegion.Select
Selection.Find(What:="Position Number", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ColNum = ActiveCell.Column
MsgBox (ColNum)
Set Range1 = Range("'C'& ColNum" & "R1")
MsgBox (Range1)
Use Cells:
Set Range1 = Cells(1,Colnum)
One should avoid .Select and .Activate:
Dim FilledRange As Range
Dim Range1 As Range
Dim Rng As Range
Dim findrng As Range
Dim ColNum As Long
With Worksheets("FILLED Today")
Set FilledRange = .Range("A1").CurrentRegion
Set findrng = FilledRange.Find(What:="Position Number", After:=.Range("A1"), LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not findrng Is Nothing Then ' test to ensure it was found.
ColNum = findrng.Column
MsgBox ColNum
Set Range1 = .Cells(1, ColNum)
MsgBox Range1
Else
MsgBox "String not found in Range"
End If
End With

Seach and find value based on value in another cell

I am trying to build a macro that will search a specific column.
Here are the steps:
1. user enters a number into the cell and then executes the macro.
2. based on the value of what the user has entered, the macro will find the text in a column.
I got everything to work pretty well except I don't know how to define the value of the cell that the user enters. Any help here would be appreciated.
Sheets("New Version ").Select
Range("B4").Select
Sheets("PN_List").Select
Columns("I:I").Select
'below is where I struggle
Selection.Find(What:=(""), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Let's say the user enters a number into cell B4, then you just have to adjust your code into:
Selection.Find(What:=Range("B4").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
You can do this in 2 ways.
Number1:
Module based: (code in module)
Sub Sample()
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
Number2:
Worksheet Event based. (code in Sheet)
Private Sub Worksheet_Change(ByVal Target as Range)
Dim search_range as Range, search_value as Range, _
lastcell as Range, foundcell as Range
Dim ws as Worksheet
Set ws = Thisworkbook.Sheets("PN_List")
Set search_range = ws.Range("I1", ws.Range("I" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = Thisworkbook.Sheets("New Version").Range("B4")
If Not Intersect(Target, search_value) Is Nothing Then
query = Msgbox("Search data?", vbYesNo)
If query = 7 Then Exit Sub
Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Not foundcell Is Nothing Then foundcell.Activate Else Msgbox "Not Found"
End Sub
The first one you enter data in B4 then run the macro.
The second one fires every time you change value in B4.
A msgbox will appear asking if you want to search the data entered.
Hope this helps.

Resources