Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
Dim CellContent0
Dim CellContent1
Dim CellContent2
Dim CellContent3
CellContent0 = ActiveCell.Address
CellContent1 = ActiveCell.Offset(, -4)
CellContent2 = ActiveCell.Offset(, 1)
Sheets("1c").Select
Cells.Find(What:=CellContent1, After _
:=ActiveCell, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False). _
Activate
Cells.Find(What:=CellContent2, After _
:=ActiveCell, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False). _
Activate
ActiveCell.Offset(, -1).Copy
Sheets("shipping").Select
Range(CellContent0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Please advice how to fix the code.
I need to copy 2 cells to the left(-4) and right(1) from active cell. Then I go to another sheet and get value based on previous copied cells(-1).
Next I want to move back to previous sheet and paste copied value into initial active cell
For example If I run the macros from cell D7, finally I need to paste copied value to the same cell D7.
Since you do not answer my clarification question, please try the next adapted code which avoids selecting/activating, which only consume Excel resources, slowing the code speed and not bringing any benefit. It copies where your code tried to do it and the same value from clipboard is copied in the initially selected cell:
Sub Macro4()
' Macro4 Macro
' Keyboard Shortcut: Ctrl+Shift+D
Dim CellContent0 As Range, CellContent1 As Range, CellContent2 As Range
Dim ws1C As Worksheet, wsSh As Worksheet, Find1 As Range, Find2 As Range
Set ws1C = Sheets("1c")
Set wsSh = Sheets("shipping")
Set CellContent0 = ActiveCell
Set CellContent1 = CellContent0.Offset(, -4)
Set CellContent2 = CellContent0.Offset(, 1)
Set Find1 = ws1C.cells.Find(What:=CellContent1.value, After _
:=CellContent0, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Set Find2 = ws1C.cells.Find(What:=CellContent2.value, After _
:=CellContent0, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not Find2 Is Nothing Then 'if a match has been found:
wsSh.Range(CellContent0.Address).value = Find2.Offset(, -1).value
'now I try copying what I understood from your comment:
CellContent0.value = Find2.Offset(, -1).value
End If
End Sub
Copying only the cell value, no clipboard is necessary, too.
Related
I have no experience of VBA, and i'm trying to understand if i can get it to run something for me in Excel.
I’m trying to get the user to input a value, click find, this value will be present in the column D. Once the value has been found it must move the sheet to that cell, then scroll to the right to open a hyperlink associated with the previous cell.
I can get the code to do the above based on an exact input but can’t get it to work on the user input.
The simple code to do it against a specific value and open the file is below:
`Sub Macro7()
'
' Macro7 Macro
'
'
Cells.Find(What:="BMS1244", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
Range("N1468").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub`
How do I replace the specific value with user input?
thanks
Does this work for you?
Sub Macro1()
'
' Macro1 Macro
'
'
Dim found As Range
Dim LinkCell As Range
Dim what_to_find As String
what_to_find = InputBox("Which value would you like to find?")
Set found = Cells.Find(What:=what_to_find, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'change the 5 to however many columns to the right you want to jump
Set LinkCell = found.Offset(0, 5)
LinkCell.Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Here Is what i need to do :
First , I have Two sheets ("AM Production","PM Production") need to Find String "Pcs" In the each sheet and count the results then Excute macro multiple times depending on that count in both sheets (Every sheet with its own count) So i did the following : - I have Two Macros one counts pcs word in the sheet and the other excute the Second macro with that number.
Sub FindPcs()
Range("N1").Select
'Find
Cells.Find(What:="Pcs", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'Found Nothing
'Replace
ActiveCell.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Copy To Above Cell
ActiveCell.Range("A1:B1").Select
Selection.Copy
ActiveCell.Offset(-1, 0).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End Sub
The Action Macro :
Sub FindMultipleTimes()
Dim x As Integer
x = "=COUNTIF(C[10],""Pcs"")"
For i = 0 To x
Application.Run "PERSONAL.XLSB!FindPcs"
Next i
End Sub
I need to merge the two macros As The main idea is to find pcs in the "AM Production" sheet then execute Sub FindMultipleTimes() in the end when it find nothing it goes to "PM Production" and Repeat the Counting and Executing part .
Note :I tried the Range and If Nothing Method with find but it throws another error object required.
Thanks in Advance.
No need to call the macro multiple times, use a Do .. Loop Until loop.
Option Explicit
Sub FindMultipleTimes()
Dim sht
For Each sht In Array("AM Production", "PM Production")
FindPcs Sheets(sht)
Next
End Sub
Sub FindPcs(ws As Worksheet)
Dim fnd As Range, n As Long
Application.ScreenUpdating = False
With ws
Set fnd = .Cells.Find(What:="Pcs", After:=.Range("N1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not fnd Is Nothing Then
Do
fnd.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'Copy To Above Cell
fnd.Resize(1, 2).Copy fnd.Offset(-1)
fnd.EntireRow.Delete
n = n + 1
Set fnd = .Cells.FindNext
Loop Until fnd Is Nothing
End If
End With
Application.ScreenUpdating = True
MsgBox n & " found on " & ws.Name
End Sub
I have data in a sheet and I want to keep a specific section and delete the rest unwanted data based on a specific text search.specific text is dynamic.
So based on Activecell using offset I delete the portion above the specific text and want delete the below portion as well. (Say for example row 56-61 only the data I need, will not be same rows in all sheets)
the current code runs for one worksheet and stops in the second sheet
Sub Test999()
Dim ws As Worksheet, f As Range
For Each ws In Worksheets
Set f = ws.Cells.Find(What:="abc", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
ws.Range(f.Offset(-2, 0),
ws.Range("A2")).EntireRow.Delete
ws.Range(f, Selection.End(xlDown)).Select
Selection.End(xlDown).Select
ws.Range(ActiveCell.Offset(2, 0),
ws.Range("A500")).EntireRow.Delete
End If
Next ws
End Sub
I want to run this code to all worksheets
There was issue in the Range Selection.
Also be sure that ActiveCell corresponds to the correct cell you want to search from, because activecell can be different in each sheet.
Sub Test999()
Dim ws As Worksheet, f As Range
For Each ws In Worksheets
'Debug.Print ws.Name
Set f = ws.Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
ws.Activate
ws.Range(f.Offset(-2, 0), ws.Range("A2")).Select
ws.Range(f.Offset(-2, 0), ws.Range("A2")).EntireRow.Delete
f.Select
ws.Range(f, Selection.End(xlDown)).Select
Selection.End(xlDown).Select
ws.Range(ActiveCell.Offset(3, 0), ws.Range("A500")).EntireRow.Delete
End If
Next ws
End Sub
I have a macro :
Sheets("AMEND ESTIMATE").Select
Cells.Find(What:=Sheets("AMEND QUOTE").Range("G4").Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(41, 3).Select
Selection.Copy
Sheets("AMEND QUOTE").Select
Range("G4").Offset(14, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'#2
Sheets("AMEND ESTIMATE").Select
Cells.Find(What:=Sheets("AMEND QUOTE").Range("H4").Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(41, 3).Select
Selection.Copy
Sheets("AMEND QUOTE").Select
Range("H4").Offset(14, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'#3
Sheets("AMEND ESTIMATE").Select
Cells.Find(What:=Sheets("AMEND QUOTE").Range("I4").Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(41, 3).Select
Selection.Copy
Sheets("AMEND QUOTE").Select
Range("I4").Offset(14, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
As you have seen, this macro finds a range from Amend Quote inside AMEND ESTIMATE(worksheets), gets a value and pastes in a certain offset cell in Amend Quote.
This was working fine, but now it is throwing run time error 91.
Can you please help me.
The issue is that your find isn't finding anything. You need to put a bit of error handling to account for when the find returns no result, by setting the result of the find action to a variable and then doing the activate on the variable only if there's something there.
Something like this:
EDIT - updated code below including behaviour to allow the sub to exit if the search term isn't found or if the search term is a zero length string.
I've also tidied up your code a lot to remove 'select then manipulate' - you can manipulate the cells without selecting them first, it'll save a lot of processing time.
Finally I've condensed the whole 50 iterations into a single loop rather than repeating the same action 50 times changing the cell reference by 1 column each time.
Please remember to accept my answer as correct if it helps you.
Sub test()
Dim rng As Range
Dim aEst As Worksheet, aQuo As Worksheet
'Set your sheet names into variables for easier referencing
Set aEst = Sheets("AMEND ESTIMATE")
Set aQuo = Sheets("AMEND QUOTE")
For i = 7 To 57 '7 = Column H, 8 = Column G, etc.
'Set the address of the found value to the rng variable
Set rng = aEst.Cells.Find(What:=aQuo.Cells(4, i).Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then 'CHECK IF THE SEARCH TERM (FROM QUOTE SHEET) WAS FOUND IN THE TARGET SHEET (ESTIMATE)
If Not rng = "" Then 'CHECK IF THE SEARCH TERM WAS A ZERO LENGTH STRING
rng.Offset(41, 3).Copy 'Copy the cell 41 rows down and 3 columns across
aQuo.Cells(4, i).Offset(14, 0).PasteSpecial Paste:=xlPasteValues 'Paste into the cell 14 rows below the original search term in the QUOTE sheet
ElseIf rng = "" Then 'EXIT SUB IF SEARCH TERM WAS A ZERO LENGTH STRING
MsgBox "Work is Done"
Exit Sub
End If
ElseIf rng Is Nothing Then 'EXIT SUB IF SEARCH TERM WAS NOT FOUND IN THE TARGET SHEET
MsgBox "Work is Done"
Exit Sub
End If
Next i 'Move to the next column across and loop
End Sub
So I have two excel documents.
One to take data from (RESULT.xlsm).
Another to insert data into (Summary.xls).
What I want is the adjacent cell values next to the hightlighted names to get inserted into Summary.xls under the respective columns. So I tried recording a macro but what happens is only the first record gets inserted.
Since only two links are allowed for me, i put it all in one picture:
http://i50.tinypic.com/9veihl.png
Note: There are multiple records in RESULT.xlsm and the screenshot shows just one.
I would like help on how I can extract data from all the set of records and insert in Summary.xlsx
Here's the recorded macro code:
Sub Summ()
Workbooks.Open Filename:="Summary.xlsx"
Windows.Arrange ArrangeStyle:=xlVertical
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Air System Name", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B10").Select
Selection.Copy
Windows("Summary.xlsx").Activate
Range("A5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Floor Area", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B14").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("B5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Total coil load", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B27").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("C5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Sensible coil load", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("B28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("D5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Max block L/s", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B30").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("E5").Select
ActiveSheet.Paste
Range("A6").Select
End Sub
I've also attached the excel files at mediafire:
Excel files
Please do help.
Thanks alot:)
So I looked up at alot of resources and tried to follow what #Tim Williams told me to and stumbled across this page (the last part): https://sites.google.com/a/madrocketscientist.com/jerrybeaucaires-excelassistant/text-functions/column-sets-to-rows
They had a solution almost close to my problem, so I made a few modifications and I'm done:D
Note: This is within the same document, different sheets.
The code of it:
Dim LR As Long, NR As Long, Rw As Long
Dim wsData As Worksheet, wsOUT As Worksheet
Dim HdrCol As Range, Hdr As String, strRESET As String
Set wsData = Sheets("Sheet1") 'source data
Set wsOUT = Sheets("Sheet2") 'output sheet
strRESET = " Air System Name " 'this value will cause the record row to increment
LR = wsData.Range("A" & Rows.Count).End(xlUp).Row
'end of incoming data
Set HdrCol = wsOUT.Range("1:1").Find(strRESET, _
LookIn:=xlValues, LookAt:=xlWhole) 'find the reset category column
If HdrCol Is Nothing Then
MsgBox "The key string '" & strRESET & _
"' could not be found on the output sheet."
Exit Sub
End If
NR = wsOUT.Cells(Rows.Count, HdrCol.Column) _
.End(xlUp).Row 'current output end of data
Set HdrCol = Nothing
On Error Resume Next
For Rw = 1 To LR
Hdr = wsData.Range("A" & Rw).Value
If (Hdr = " Air System Name ") Then
NR = NR + 1
End If
If Hdr <> "" Then
Set HdrCol = wsOUT.Range("1:1").Find(Hdr, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not HdrCol Is Nothing Then
wsOUT.Cells(NR, HdrCol.Column).Value _
= wsData.Range("B" & Rw).Value
Set HdrCol = Nothing
End If
End If
Next Rw
The only little problem is the space. In my excel document, my report has trailing and leading spaces, and this doesn't match with my sheet2 columns headers, I kind of temporarily fixed it, since I looked around and couldn't find a way to automatically trim all the space from the whole column.
So that's it:)