I am trying to create a VBA macro for post processing data, and it currently has a "Raw Data" sheet for the first sheet, and my post processing tools on the second sheet. What I have so far is a button that will search the data and create plots for the desired variables, but it pulls data for all of the test points. What I want to do is to be able to filter by test point from the data review sheet. What would be ideal would be to have an autofilter type dropdown menu on my post processing sheet where the test point can be selected, and the data on the previous sheet would be filtered.
Here's the search function I've been using:
Dim TestPt As Long
Dim rows As Long
rows = Sheets(1).UsedRange.rows.Count
'
Sheets(1).Select
Cells.Find(What:="TargetTestPointNumber", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
TestPt = ActiveCell.Column
Any help would be appreciated!
I'm not 100% sure what you needed exactly. But this code does a comparison between the value you want and the data sheet. If its = it will copy it to Row D so if you have stuff there you will need to change it. Also it assumes the data is in row 1.
Dim i As Long, lastRowD As Long, lastRowA As Long
With Sheets("datasheetname")
lastRowA = .Range("A" & .Rows.count).End(xlUp).Row
For i = 1 To lastRowA
lastRowD = .Range("D" & .Rows.count).End(xlUp).Row
If .Cells(i, 1).Value = "testvalue" Then
.Cells(lastRowD, 4).Value = "testvalue"
End If
Next i
.Range("D1", "D" & lastRowD).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveChart.SetSourceData Source:=Range("'datasheetname'!$D$1:$D" & lastRowD)
End With
note youll need to change test value and datasheetnames. As well as any of the column info. I tried it and it works. I just have no idea if this is what you need. If you need to copy more than 1 row, you would change in the if how many to do.
.cells(lastRowD,5).value= .cells(i,2).value etc
Sorry for the messy code, but I found this to work for me. Basically I copied the unique test point value to the other sheet, linked them to a ComboBox, and linked a macro to run with the ComboBox to autofilter the data on the other sheet. I'm sure there has to be a better way, but it works for me.
Sub ValueSelectionData()
'
Dim TestPt As Long
Dim rows As Long
Dim Value As Long 'used to select test point
rows = Sheets(1).UsedRange.rows.Count 'Row count on data sheet
Value = Sheets(2).Cells(2, 6).Value 'value linked to ComboBox selection
'
Sheets(2).Columns("A:A").ClearContents
Sheets(1).Select
Cells.Find(What:="TargetTestPointNumber", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate 'searches for test point column in data
TestPt = ActiveCell.Column
Range(Sheets(1).Cells(2, TestPt), Sheets(1).Cells(rows, TestPt)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 1)), Unique:=True
If Value > 0 Then
Sheets(1).Select
Cells.Find(What:="TargetTestPointNumber", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
TestPt = ActiveCell.Column
Value = Sheets(2).Cells(2, 6).Value 'desired test point to filter for
Sheets(1).Range(Sheets(1).Cells(1, TestPt), Sheets(1).Cells(rows, TestPt)).AutoFilter Field:=1, Criteria1:=Value 'autofilters data for desired test point
Else
'Clear all auto filters
If Sheets(1).AutoFilterMode Then
Sheets(1).ShowAllData
End If
End If
End Sub
Related
New to forum and vba but want to learn more.
Got two tables of large data and want to look for a cell value equal to the cell value to the left of my active cell in table 1 and then find that value in the 2nd table. When value is found I want to return the cell value found in the 5th column to the right of column A in the 2nd table.
The macro I have created works well - if it hadn't been that it always looks for the same value "10.136.32.10" i.e. this value does not change as the active cell moves down table 1. I would like the value to change depending on what is actually copied from the cell to the left. Is there a way to do this? I use Ctrl+f function and then paste in the cell value copied from table 1
Have the following macro:
Sub Makro2()
'
' Makro2 Makro
'
'
ActiveCell.Offset(0, -1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:="10.136.32.10", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
Here is the code by which you can do your job. This macro searches immediately on all rows. If you only need to search for an active cell, then you need to remove the loop.
Sub macro2()
Dim lr As Long, r As Long, c As Long
Dim str As String
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 1 To lr
str = Cells(r, c).Offset(0, -1)
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:=str, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
Cells(r, c + 1).past
Next r
End Sub
i am very new to Excel VB. I am hoping you can assist with my problem.
I have several tables like this
The tables can be edited such as by inserting new rows, etc by the user.
I want to highlight and copy the latest table (the one at the very last row) and paste it at the next available space, 3 rows down.
I managed to do this, but I can only highlight and copy the cells with data. So should there be an empty row in the middle of the table, or an empty last row with borders, it would not copy correctly, as shown here:
I do still need to copy everything within the borders including empty rows, but I lack the skills and knowledge to do so. I hope you can assist.
The following is my code:
Sub CopyPaste()
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
FirstColumn = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
LastColumn = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
BelowLastName = Cells.Find("", After:=Cells(LastRow, FirstColumn), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row
Cells(BelowLastName, FirstColumn).Select 'This selects C5 (refer image)
Selection.Offset(-1).Select 'This selects C4 (refer image)
Range(Selection, Cells(LastRow, LastColumn)).Select 'This highlights whole table
Selection.Copy
Cells(LastRow, FirstColumn).Select
ActiveCell.Offset(3).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try this
Sub x()
Dim rEnd As Range, rStart As Range, i As Long
Set rEnd = Range("C" & Rows.Count).End(xlUp)
Do While rEnd.Offset(i).Borders(xlEdgeBottom).LineStyle = xlContinuous
i = i - 1
Loop
Set rStart = rEnd.Offset(i + 1)
Range(rStart, rEnd).Resize(, 8).Copy rEnd.Offset(3)
End Sub
i'm having troubles with a Macro that i can't figure it out how to do it, i need a Macro that can scan trough a workbook, Find 3 values, "Data: ", "N°" and "Rodovia:", make a offset of 1 column from them and select that value, and paste it in another sheet like this:
Data: | 10/03/2014
N°: | L02.020.22C
Rodovida: | GO-020
So it must select "10/03/2014", "L02.020.22C" and "GO-020"
I can do it using find and Resize i know, but they are not in the same row or column, they are located in random rows and columns, thats the problem, i tried to use a Range().Select with multiples .Find().offset() inside but it didn't worked
After that i need it to do it with all the cases in the workbook, so i need it to give me that
Case1Data|Case1N°|Case1Rodovia
Case2Data|Case2N°|Case2Rodovia
Case3Data|Case3N°|Case3Rodovia
UPDATE, Code so far:
Sub Gather_Values()
Dim Rng As Range
With Sheets("01").Range("A:AJ")
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Selection.Copy
Sheets.Add.Name = "New"
Worksheets("New").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub
This subroutine will collate the 3 values for a single sheet named "01", searching the UsedRange of that sheet:
Sub Gather_Values()
Dim Rng As Range
Dim Sht As Worksheet
'create new worksheet, name it "New"
Set Sht = Sheets.Add
Sht.Name = "New"
'set column titles in the new sheet
Sht.Range("B1").Value = "Data"
Sht.Range("C1").Value = "N°"
Sht.Range("D1").Value = "Rodovia"
'search the entire UsedRange of sheet 01
With Sheets("01").UsedRange
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("B2").Value = Rng.Value 'put value from the Find into B column of new sheet
Set Rng = .Find(What:="N°", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("C2").Value = Rng.Value 'put value from the Find into C column of new sheet
Set Rng = .Find(What:="Rodovia:", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("D2").Value = Rng.Value 'put value from the Find into D column of new sheet
End With
End Sub
If the values appear more than once on a single sheet you will need to determine either what ranges of cells will always contain separate records or come up with a different way of parsing the data to make sure the values you find in sequence are of the same "case" or "record".
It would be helpful to see a sampling of the raw data that you are parsing to see if there is a better way to collate it than using Find(). It would be best to see an instance where there is more than one "case" in the raw data.
I am completely new for VBA.
I have excel data sheet containing numbers and strings. I want to search for certain string say 'CYP' in column I then look for a cell of its row at column C and copy entire rows containing the string of cell C. I want to paste in sheet 2 of the same workbook and loop it again to look for remaining CYPs in column.
Would you help me on this please?
After the suggestion from pnuts, here is my macro code
Sub Macro1()
'
' Macro1 Macro
'
'
Columns("I:I").Select
Range("I729").Activate
Selection.Find(What:="cyp", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveWindow.SmallScroll Down:=5
Range("C749").Select
Selection.Copy
Columns("C:C").Select
Range("C734").Activate
Selection.Find(What:="EPT001TT0601C000151", After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
Rows("746:750").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
End Sub
In this code the CYP was found in I749, cell C749 was copied as string and first row in column C containing the same string was searched followed by copying of the entire row and 4 more followed by it then pasting in sheet2 of the same workbook.
What I wanted was to loop this action again and again upto the end of column I and repeat the same action.
Thank you!
I managed to solve the problem with the help of Trebor76 at Excelforum. Here I am giving solution in that way it might be helpful for some newbies like myself with similar problem.
Option Explicit
Sub Macro1()
'Written and assisted by Trebor76
'Copy an entire row from Sheet1 to Sheet2 for each unique matching item in Col. C if the text in Col. I contains the text 'CYP' (case sensitive)
'http://www.excelforum.com/excel-programming-vba-macros/962511-vba-for-searching-string-in-a-column-and-copy-rows-depending-on-string-in-adjacent-cell.html
Dim rngCell As Range
Dim objMyUniqueArray As Object
Dim lngMyArrayCounter As Long
Dim lngMyRow As Long
Dim varMyItem As Variant
Application.ScreenUpdating = False
Set objMyUniqueArray = CreateObject("Scripting.Dictionary")
For Each rngCell In Sheets("Sheet1").Range("I1:I" & Sheets("Sheet1").Range("I" & Rows.Count).End(xlUp).Row)
If InStr(rngCell, "CYP") > 0 Then
If Not objMyUniqueArray.Exists(Trim(Cells(rngCell.Row, "C"))) Then
lngMyArrayCounter = lngMyArrayCounter + 1
objMyUniqueArray.Add (Trim(Cells(rngCell.Row, "C"))), lngMyArrayCounter
varMyItem = Sheets("Sheet1").Cells(rngCell.Row, "C")
For lngMyRow = 1 To Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
If Sheets("Sheet1").Cells(lngMyRow, "C") = varMyItem Then
Rows(lngMyRow).Copy Destination:=Sheets("Sheet2").Range("A" & Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1)
End If
Next lngMyRow
End If
End If
Next rngCell
Set objMyUniqueArray = Nothing
Application.ScreenUpdating = True
MsgBox "All applicable rows have been copied.", vbInformation
End Sub
Cheers!
How do I modify this line in VBA to only select the columns that have values?
Set rng = Range("A1", Range("A65536").End(xlUp)).SpecialCells(xlCellTypeVisible)
I don't think I'm doing something right since the CountLarge property is several billion cells
Here is a sample of my data
#SiddharthRout Yes I only need the rows that have data. I think I have it working now with End(xlToLeft) from #JMax ... Now that I'm iterating over the cells, I can just quit the For each loop once the last row is reached. I might have this working now. – makerofthings7 14 mins ago
For this neither you need .SpecialCells nor do you need to loop through the rows :)
Here is a sample code. This will copy all the rows which have data to Sheet2 (TRIED AND TESTED)
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim LastRow As Long, LastCol As Long
Set ws = Sheets("Sheet1")
With ws
LastRow = .Cells.Find(What:="*", After:=.Range("A1"), Lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
LastCol = .Cells.Find(What:="*", After:=.Range("A1"), Lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
MatchCase:=False).Column
With .Range("A1:" & Split(Cells(, LastCol).Address, "$")(1) & LastRow)
.AutoFilter Field:=1, Criteria1:="<>"
Set rng = ws.AutoFilter.Range
rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Copy _
Destination:=Sheets("Sheet2").Range("A1")
End With
End With
End Sub
SNAPSHOT
I am assuming that all cells in a particular row will have data and there won't be a case like this
#makerofthings7: I think I know what exactly you are trying to do :) you don't need to use loops to achieve what you want. Just a quick question. Is it possible that say Cell C10 might have a value but B10 might not? – Siddharth Rout 12 mins ago
If there is then we will have to set the autofilter criteria accordingly.
Edit:
WAY 2
The other way would be to sort your data, pushing the blanks way down and then copying the resulting range :)
HTH