Copy-paste to fixed cells macro searchvalue
Closed
gastonga
Posts
2
Registration date
Thursday December 1, 2011
Status
Member
Last seen
December 2, 2011
-
Dec 1, 2011 at 10:14 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Dec 5, 2011 at 08:54 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Dec 5, 2011 at 08:54 AM
Related:
- Copy-paste to fixed cells macro searchvalue
- Excel macro to create new sheet based on value in cells - Guide
- Spell number in excel without macro - Guide
- How to paste photo in resume - Guide
- Copy and paste fonts - Guide
- Copy cells from one sheet to another - Guide
3 responses
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Dec 2, 2011 at 06:38 AM
Dec 2, 2011 at 06:38 AM
So if I understood you correctly, what you want is that look at column H and find the first "copy" word (lets say it is H1). Then find the next cell in column "H" which is non-BLANk (lets say it is H11).
Once you know the two points, you want to copy the content of I1 (row corresponding to first "Copy" @ H1) cell down to one cell above the next non blank (I10 row corresponding to @H11 -1)
Did i get it correct ?
Once you know the two points, you want to copy the content of I1 (row corresponding to first "Copy" @ H1) cell down to one cell above the next non blank (I10 row corresponding to @H11 -1)
Did i get it correct ?
gastonga
Posts
2
Registration date
Thursday December 1, 2011
Status
Member
Last seen
December 2, 2011
Dec 2, 2011 at 07:17 AM
Dec 2, 2011 at 07:17 AM
Exactly!
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Dec 5, 2011 at 08:54 AM
Dec 5, 2011 at 08:54 AM
Try this
I have not tested it so there might be a wrinkle here and there
I have not tested it so there might be a wrinkle here and there
Sub SearchAndCopy() Dim lFirstHit As Long Dim lSecondHit As Long Dim strsearch As String Dim iSearchCol As Integer Dim lMaxUsedRows As Long lMaxUsedRows = getItemLocation("*", Cells) iSearchCol = 8 ' column H Do While True strsearch = CStr(InputBox("enter the string to search for")) If (strsearch = vbNullString) _ Then Exit Do End If lFirstHit = 0 Do While True ' FIND WHERE IS MY SEARCH WORD IN ROW lFirstHit = getItemLocation(strsearch, Range(Cells(lFirstHit + 1, iSearchCol), Cells(lMaxUsedRows, iSearchCol)), True, False, True) If (lFirstHit = 0) Then Exit Do 'FIND WHERE IS THE FIRST NON BLANK SPACE lSecondHit = getItemLocation("*", Range(Cells(lFirstHit + 1, iSearchCol), Cells(lMaxUsedRows, iSearchCol)), True, False, True) If (lSecondHit = 0) _ Then If (Cells(lFirstHit + 1, iSearchCol) = vbNullString) _ Then lSecondHit = lMaxUsedRows + 1 End If End If 'no non-blank cell found If (lSecondHit = 0) Then Exit Do lSecondHit = lSecondHit - 1 'next cells is not blank If (lSecondHit > lFirstHit) _ Then Cells(lFirstHit, "I").Copy Range(Cells(lFirstHit + 1, "I"), Cells(lSecondHit, "I")).PasteSpecial xlPasteValues End If Loop Loop End Sub Public Function getItemLocation(sLookFor As String, _ rngSearch As Range, _ Optional bFullString As Boolean = True, _ Optional bLastOccurance As Boolean = True, _ Optional bFindRow As Boolean = True) As Long 'find the first/last row/column within a range for a specific string Dim Cell As Range Dim iLookAt As Integer Dim iSearchDir As Integer Dim iSearchOdr As Integer If (bFullString) _ Then iLookAt = xlWhole Else iLookAt = xlPart End If If (bLastOccurance) _ Then iSearchDir = xlPrevious Else iSearchDir = xlNext End If If Not (bFindRow) _ Then iSearchOdr = xlByColumns Else iSearchOdr = xlByRows End If With rngSearch If (bLastOccurance) _ Then Set Cell = .Find(sLookFor, .Cells(1, 1), xlValues, iLookAt, iSearchOdr, iSearchDir) Else Set Cell = .Find(sLookFor, .Cells(.Rows.Count, .Columns.Count), xlValues, iLookAt, iSearchOdr, iSearchDir) End If End With If Cell Is Nothing Then getItemLocation = 0 ElseIf Not (bFindRow) _ Then getItemLocation = Cell.Column Else getItemLocation = Cell.Row End If Set Cell = Nothing End Function