So I have been trying to figure out why this isn't working and I am getting really frustrated.
When I click on my "Search" button, it takes the text from a text box, and uses that as the search criteria. I want it to skip whatever row is currently active, so that as long as I keep pressing my "Search" buttton it will move on, and not keep finding the same row. And I had it working for a long time, then I upgraded to Windows 10 and stuff stopped working. This is just the one thing I can't seem to figure out. I made some changes to my original, so this is not the same code that used to work. No matter what I do it keeps showing me the same row even though the one RIGHT BELOW it has identical data. Like in the picture below, if I search for TRACE the third row of data is selected, but when I hit "Search" again, it doesnt move to the next row like it should. I am using Range.Find(What:= , After:=) and setting the After range to the very left cell of the current activated range. Which should start the search on the next row. But that is not happening.
Private Sub Search_Next_Click()
Dim Fnd As Range
Dim S_Range as Range
Dim CurrRow As Integer: CurrRow = ActiveCell.Row
Dim CurrColumn As Integer: CurrColumn = ActiveCell.Column
'Last row of data
LastRow = Range("B24").End(xlDown).Row
AC = ActiveCell.Address
''If the Find button is pressed and the current active cell is outside the range of my data
''this makes sure that the active cell moves to the upper left of that range
If AC = "" Or CurrRow < 24 Or CurrColumn > 10 Then
AC = "B24"
Range(AC).Activate
End If
ACr = ActiveCell.Row
On Error Resume Next
Set S_range = Range("B24" & ":J" & LastRow)
Set Fnd = S_range.Find(what:=SearchBox.Text, after:=Range(AC))
FR = Fnd.Row
If FR = "" Then
MsgBox ("No Match Found")
DoCmd.CancelEvent
SearchBox.SetFocus
Exit Sub
End If
On Error GoTo 0
Scell = "B" & FR & ":J" & FR
ActiveSheet.Range(Scell).Select
ActiveSheet.Range(Scell).Activate
End Sub
OK as soon as I posted this, I figured out what I did wrong. Although I dont know why it worked in the past, and then stopped. Maybe I changed something without realizing it and screwed myself up. Anyway, I was searching for a value in the D column. Once it was found the cell with the search criteria, the cells in column B through F were selected and activated in that row. But when I hit Search again, it started after the B column of that row. I thought it would skip to the next row, but it was moving to the next column to start the search. So it ran into the same value again, and the cycle repeated.
I just changed the After criteria to
After:=Range("B" & ACr + 1)
Then it started the search on the next row like I wanted.
Im leaving this up in case anyone else comes across this mistake. Hopefully I can help someone else realize that they are not the only ones that make stupid mistakes.....
Related
For example, on some sites,
I already study & try on Macro for insert HPageBreak Present on Every occurrence on particular text, with loop every cell of single column, but my requirement is every 2nd occurrence of particular txt
https://answers.microsoft.com/en-us/msoffice/forum/all/excel-macro-that-will-insert-horizontal-macros/9976e30a-8aae-4bec-84e8-43b35b113ec2
https://answers.microsoft.com/en-us/msoffice/forum/all/insert-dynamic-page-breaks-with-vba-for-excel/85790a6a-ef93-4354-8ad5-3cc5e4399285
https://www.extendoffice.com/documents/excel/1774-excel-insert-page-break-every-row.html
I already use Below code to put Hpagebreak on every found
but now requirement raise as every 2nd occurrence of "DISPATCH JAN TO NOV-22"
Sub Insert_Pagebreak_On_EveryFoundok()
Dim MYCOLUMN As Range
Dim MyCell As Range
'For Each MyCell In Range("G2:G" & Rows.Count).End.xlUp))
'For Each MyCell In Range Cells(Rows.Count, 2).End(xlUp).row 2
ActiveSheet.Range("G" & Rows.Count).End(xlUp).row))
ActiveWindow.view = xlPageBreakPreview
Set MYCOLUMN = ActiveSheet.Range("F2:F" & ActiveSheet.Range("F" & Rows.Count).End(xlUp).row)
For Each MyCell In MYCOLUMN
MyCell.Select
'If MyCell.Value Like "*Page 1 of 1*" Then
If MyCell.Value Like "*DISPATCH JAN TO NOV-22*" Then
ActiveCell.EntireRow.Select
ActiveWindow.SelectedSheets.HPageBreaks.Add
ActiveCell.offset(1, 0)
Else
ActiveCell.offset(1, 0).Select
End If
Next
ActiveWindow.view = xlNormalView
End Sub
This Loop check every cell that take more time, but I believe if use Range.Find method, then it can be more robust.
I am not knowing very well all aspects of VBA, but I daily use VBA in my many types of daily routine work & without it, I can't complete my work on time.
Currently I manually select 2 sets of data, adjust rows height to fit on A4, select -set-click print area & then print, and after print that I select below further 2 sets & do same thing, till sheet's data end,
painfully pass whole my day, just for print 2 data set on 1 A4 page.
There are need to beware for Hidden rows which hides for reason (not requirement in print). so, condition is only visible rows should be count for 2nd occurrence.
I attached Screen shot of whole scenario for reference.
[Plese Refer This Image as my situation]
enter image description here
Hundreds of data sets on this worksheet.
There should be 2 sets of data as pair require on every A4 size page.
so obviously page break requires on every 2nd occurrence of particular text.
If, that happen successfully, I am ready to manually adjust rows height to fit 2 sets in A4 page, so I get whole sheet ready for print in one go.
Hope, I try my best to describe my situation if require further, please mention.
Can anyone help regarding this?
I really appreciate & will be thankful forever.
Regards,
Chirag Raval
I have an Excel sheet with prices (of materials used in construction) from previous years.
I am trying to make a code that will show me all the data based on a certain year that I choose.
What my list/excel looks like
In cell E3 (in yellow), I input the year to "analyse".
I need a function that will search columns K to Q (and more since every year, prices change), for that year, and copy all the data of sales, reg. loc. and spec. loc. into the columns F, G and H.
If it's easier that, instead of putting the year in cells K3, L3 and M3 (for example), if I put it in cell N3, R3, etc. (in red) instead, the function will take the 3 previous columns and copy/paste them in columns F to H.
Option 2, if it makes the coding easier
Also the list goes until row 381, and there's a potential that more data will be input eventually so take into consideration as if the list had an infinite amount of rows. However, for the columns, it's always fixed to 3 columns.
FYI: it is not a school project. I'm trying to simplify my work instead of manually searching and copy/pasting the data every time.
Please, try the next code. It should do what (I understand) you need. It should be fast, not using clipboard for copying. As I suggested in my comment, it firstly searches/finds in the third row the year (long or string, as it is written in "E3"), starting searching after "E3", then copying the range built according to the found cell. If not a match is found, the code exits on the line If rngFirstCol Is Nothing Then Exit Sub. You may place a message there, to warn in such a case. It works on your first arrangement/picture, meaning that the year must be filled in the third row of the first column where from the necessary data should be collected/copied:
Sub ExtractPricesPerYear()
Dim sh As Worksheet, lastR As Long, rngFirstCol As Range, lngYear, necCol As Long
Set sh = ActiveSheet 'use here your necessary sheet
lngYear = sh.Range("E3").value 'the year to be searched
Set rngFirstCol = sh.rows(3).Find(What:=lngYear, After:=sh.Range("E3"), LookIn:=xlValues, Lookat:=xlWhole)
If rngFirstCol Is Nothing Then Exit Sub
necCol = rngFirstCol.Column
lastR = sh.cells(sh.rows.count, necCol).End(xlUp).row
With sh.Range(rngFirstCol.Offset(1), sh.cells(lastR, necCol + 2))
sh.Range("E4").Resize(.rows.count, .Columns.count).value = .value
End With
End Sub
Please, send some feedback after testing it.
And another issue: It is good to show us what you tried by your own. If not a piece of code, at least, something to prove that you investigated and had some ideas about the task to be solved, asking for hints, suggestions etc. proving that you know something about how it can be done...
Edited:
Following your requirement from last comment, please use the next solution. Please, copy the next code in the respective sheet code module (right click on the sheet name, then choose View Code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastR As Long, lastRF As Long, rngFirstCol As Range, lngYear, necCol As Long
If Target.Address(0, 0) = "E3" Then 'the code exits for any other change on the sheet
lngYear = Target.value 'the year to be searched
Set rngFirstCol = Me.rows(3).Find(What:=lngYear, After:=Me.Range("E3"), LookIn:=xlValues, Lookat:=xlWhole)
If rngFirstCol Is Nothing Then MsgBox "No such year found on the third row...:": Exit Sub
necCol = rngFirstCol.Column 'column number of the found cell
lastR = Me.cells(Me.rows.count, necCol).End(xlUp).row 'last row on the found column
lastRF = Me.Range("F" & Me.rows.count).End(xlUp).row 'last row on F:F column (to delete its content, if any)
If lastRF > 4 Then Me.Range("F4:H" & lastRF).ClearContents 'clear the range to make place for the new data
With Me.Range(rngFirstCol.Offset(1), Me.cells(lastR, necCol + 2))
Me.Range("F4").Resize(.rows.count, .Columns.count).value = .value
End With
End If
End Sub
I'm having a few teething problems with a code I'm writing. I'm relatively new to coding and don't understand it 100% but i can normally work it out. Im creating an events tracker so the rows indicate places on an event that people can book onto, when someone cancels i want tot to show they are cancelled but open up the slot again so someone else can book on.
I keep receiving either error 1004 or error 242 on the code below. What i'm trying to achieve is when i press the button the selected cell will highlight thee entire row,highlight it red and insert a new blank row below, copy the original row to a new destination (the last row of a dynamic range lower down on the same tab).
I've fiddled around loads with it already (activecell.entire row alyouts, setrng= layouts etc) so its probably really wrong and messed up - which is why i need some help sorting it out.
Sub CANCEL()
Dim rgCopy As Excel.Range
Set rgCopy = Range(Range("A" & ActiveCell.Row), Range("IV" &
ActiveCell.Row).End(xlToLeft)).Select
rgCopy.Copy (Range("_cancel3").End(xlDown).Offset(1, 0))
ActiveCell.EntireRow.Interior.ColorIndex = 3
ActiveCell.EntireRow.Insert Shift:=xlShiftDown
ActiveCell.EntireRow.Interior.ColorIndex = 2
ActiveCell.EntireRow.ClearContents
Selection.Insert Shift:=xlShiftDown
End Sub
It feels really close but that im just missing that last bit. Thankyou
Hi I think the following code should help you achieve your goal.
Sub CANCEL()
Dim rgCopy As Range
Set rgCopy = Range("A" & ActiveCell.Row & ":IV" & ActiveCell.Row)
Dim dynamicRange As Range
Set dynamicRange = Range("A20")
rgCopy.Copy
dynamicRange.PasteSpecial xlPasteAll
rgCopy.EntireRow.Interior.ColorIndex = 3
rgCopy.Offset(1, 0).EntireRow.Insert xlShiftDown
rgCopy.Offset(1, 0).ClearFormats
rgCopy.ClearContents
End Sub
I'm struggling with what I believe is a loop problem. I'm more of a "backyard mechanic" with Excel VBA so please excuse my simple question.
I can't share the workbook due to proprietary information unfortunately but I have the code I use with some field name changes.
Background: I have a column that I take 1 cell at a time and feed it into a pivot table field and run reports. The way I have it now, I delete the current Row which returns the reference back to cell A2. Think of it like a programming Pez dispenser. Awful and very brute force I know. The row delete operation takes a lot of system resources and I'd like to optimize it. I've tried reading through here and a few other websites for the past couple hours but I can't make heads or tails of what I'm coming across.
Any help would be very greatly appreciated!
Sub AutoReport()
Dim strPage As String
Worksheets("HomePage").Select
Beginning:
'Sets the name in Home Page to the name in Feederlist cell F2
With Sheet1
strPage = Worksheets("FeederList").Range("A2")
Worksheets("HomePage").PivotTables("PivotTable1").PivotFields("UNIQUE ID"). _
CurrentPage = strPage
End With
**Do a Bunch of Stuff**
' Feeds the next input into the machine
MoveToNext:
Worksheets("FeederList").Activate
Worksheets("FeederList").Range("A2").EntireRow.Delete
If Worksheets("FeederList").Range("A2") = "" Then
MsgBox "All Reports have been created.", vbInformation + vbOKOnly
Exit Sub
Else
GoTo Beginning
End If
End Sub
the acceptance is the first step to learning. In general, the stackoverflow community encourages to invigorate the technical and logical parts of the brain by providing the hints to solution and not the exact solution. However, as you are very new, I am starting with giving off hints and later on the code to rectify your issue. You are going very well with your code, however some minor tweaks will optimize your code significantly.
Worksheets("HomePage").Select
Dim lstRow As Long
Dim rngCell As Range
Dim rngSelection As Range
'Let's find the last row with data in column A.
'So that we only traverse the required range without the need of
'deleting previous cells while using the For loop.
lstRow = Worksheets("FeederList").Range("A" & Application.Rows.Count).End(xlUp).Row
Set rngSelection = Worksheets("FeederList").Range("A2:A" & lstRow)
For Each rngCell In rngSelection.Cells
'Ignore all the cells with blank value or else the pivot table will throw the error
If Trim(rngCell.Value) <> vbNullString Then
'Sets the name in Home Page to the name in Feederlist cell F2
strPage = rngCell.Value
Worksheets("HomePage").PivotTables("PivotTable1").PivotFields("UNIQUE ID"). _
CurrentPage = strPage
' **Do a Bunch of Stuff**
End If
Next
rngSelection.Clear ' Optional - if the range really needs to be cleared
MsgBox "All Reports have been created.", vbInformation + vbOKOnly
You need to understand the changes and ask any further questions that you might have. Below are the changes made to the original code.
Introduced calculation of last cell in Column A having some data
Introduced For Loop instead of using labels eliminating the need of deleting rows
I am working in Excel and trying to create a command button that will update the value in a cell based on the current value of the cell, down the entire column.
More specifically, I have a column with location IDs which are integers. At a click, I want all cells within that column that contain the values 1 or 2 to be changed to 10. Then, I want to run a prerecorded sort macro.
I have found information on cascading If statements, which seems to be the key. I thought something like the following might work, but it keeps telling me I have a logic error.
Private Sub CommandButton1_Click()
If Range("LocationID") = 1 Then
Range("LocationID") = 10
Else
If Range("LocationID") = 2 Then
Range("LocationID") = 10
End If
End Sub
Location is the column I am trying to search and change.I have not added my sort macro into this yet, because I assume that works like any other button that calls a macro.
This is probably really basic, but VBA is totally foreign to me. I've been banging my head against this problem all morning. Thanks for the help!
It sounds like you mean something like this:
Private Sub CommandButton1_Click()
Dim lRow As Long
lRow = 1
Do Until Range("A" & lRow) = ""
Select Case Range("A" & lRow)
Case 1, 2
Range("A" & lRow) = 10
End Select
lRow = lRow + 1
Loop
End Sub
Note: I just guessed that it's column A. You will want to change that in all 3 places to the column that has the value you want.