I'm relatively new to VBA, I have only some experience with Python and only very little experience looking at other VBA macros and adjusting them to my need, so I'm trying to do what I can.
What I am trying to do is for each part number pasted in worksheet B (worksheet B, row A) I want to find the same part number from a different worksheet containing all part numbers (worksheet D, row A) and copy the description (worksheet D, row H) from worksheet D to another column, (worksheet B, row D) then check the next part number in the row and repeat.
The current error that I'm getting is that there is "Compile error: Else without if", I'm sorry that I am not very proficient, but any help would be greatly appreciated.
Other information:
-My part numbers to search through in worksheet B, column B are filled in from worksheet A, is it okay to just make it =A!B2 or =CONCATENATE(A!B2)?
Sub Description()
Dim wsA As Worksheet, wsB As Worksheet, wsC As Worksheet, wsD As Worksheet
Dim Rng As Range
Set wsB = Worksheets("B")
Set wsD = Worksheets("D")
Do: aRow = 2
If wsB.Cells(aRow, 2) <> "" Then
With Worksheets("D").Range("A:A")
x = wsB.Cells(aRow, 2)
Set Rng = .Find(What:=x, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Selection.Copy
wsB.Cells(dRow, 2).Paste
dRow = dRow + 1
Else
aRow = aRow + 1
Loop Until wsB.Cells(aRow, 2) = ""
End Sub
Thanks again!
Edit: Can't Execute code in break mode is current error
Sub Description()
Dim wsA As Worksheet, wsB As Worksheet, wsC As Worksheet, wsD As Worksheet
Dim Rng As Range
Set wsB = Worksheets("B")
Set wsD = Worksheets("D")
aRow = 2
dRow = 2
Do:
If wsB.Cells(aRow, 1) <> "" Then
With Worksheets("D").Range("A:A")
Set Rng = .Find(What:=wsB.Cells(aRow, 1), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Rng.Copy
Rng.Offset(0, 3).Paste (Cells(aRow, 4))
dRow = dRow + 1
aRow = aRow + 1
End With
End If
Loop Until wsB.Cells(aRow, 1) = ""
End Sub
Can you try to put End If on the next line after aRow = aRow + 1. See MSDN for syntax msdn.microsoft.com/en-us/library/752y8abs.aspx
In Excel we usually call vertical range as column, and horizontal one as row.
From your code and question description, I assume what you said "row A" is column A.
Also, your code scan through wsB.Cells(aRow, 2). It is column B not column A.
Anyway, this is just a minor problem.
The following code will check cells of column B of worksheet B. If the same value is found
in column A of worksheet D, then the cooresponding cell in column H of worksheet D will
be copied to the cell in column B of worksheet B.
Option Explicit
Sub Description()
Dim wsB As Worksheet, wsD As Worksheet, aRow As Long
Dim rngSearchRange As Range, rngFound As Range
Set wsB = Worksheets("B")
Set wsD = Worksheets("D")
Set rngSearchRange = wsD.Range("A:A")
aRow = 2
Do While wsB.Cells(aRow, 2).Value <> ""
Set rngFound = rngSearchRange.Find(What:=wsB.Cells(aRow, 2).Value, LookAt:=xlWhole)
If Not rngFound Is Nothing Then
wsD.Cells(rngFound.Row, 8).Copy Destination:=wsB.Cells(aRow, 4) ' Indexes of Column H, D are respectively 8, 4
End If
aRow = aRow + 1
Loop
End Sub
Here's what worked for me.
Sub Description()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Sheets("B").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
Dim foundRng As Range
For Each rng In Sheets("B").Range("B2:B" & LastRow)
Set foundRng = Sheets("D").Range("A:A").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
If Not foundRng Is Nothing Then
Sheets("B").Cells(rng.Row, "D") = Sheets("D").Cells(foundRng.Row, "H")
End If
Next rng
Application.ScreenUpdating = True
End Sub
Related
i have 2 workbook and i want to match workbook-1 sheet one column with another workbook-2 sheet one column,if 2 cell is same match ID then return header.
For example:
i want to return the Workbook-2 Header such as like A001 if my "X" marked ID all is match with workbook-1 ID.
The expected outcome like the "Output" Sheet in workbook-2
Workbook-1
workbook-2
outcome example
I took a stab at this even though it doesn't make much sense to me. It requires that the three sheets you're using use the exact sheet names in the screenshot and no other sheets share the name. I didn't test it but
Sub WhatTheHeckisThis()
Dim WKBK As Workbook, wsStart As Worksheet, wsLookup As Worksheet, wsOutput As Worksheet
Dim lineLookup As Range, i As Long, yCell As Range, rCell As Range
Dim tWSstart As String: tWSstart = "Datasheet"
Dim tWSLookup As String: tWSLookup = "MATCH"
Dim twsOutput As String: twsOutput = "OUTPUT"
For Each WKBK In Application.Workbooks
For Each WS In WKBK.Sheets
If UCase(WS.Name) = UCase(tWSstart) Then
Set wsStart = WS
ElseIf UCase(WS.Name) = (tWSLookup) Then
Set wsLookup = WS
ElseIf UCase(WS.Name) = UCase(twsOutput) Then
Set wsOutput = WS
End If
Next WS
Next WKBK
For Each rCell In Intersect(wsStart.Range("B2:B" & Rows.Count), wsStart.UsedRange).Cells
Set lineLookup = Nothing
Set lineLookup = wsLookup.Cells.Find(rCell.Value, _
After:=wsLookup.Cells.Cells(1, 1), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not lineLookup Is Nothing Then
For Each yCell In Intersect(lineLookup.EntireRow, wsLookup.UsedRange).Cells
If UCase(yCell.Value) = "X" Then
With wsOutput
i = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(i, 1).Value = i - 1
.Cells(i, 2).Value = wsLookup.Cells(1, yCell.Column).Value
End With
End If
Next yCell
End If
Next rCell
End Sub
I am trying to update record on all worksheets in a workbook.
My search values are in column No 6 and the replace values are in column No 9
The code is only working for a worksheet not the entire worksheet at a time.
I tried this below but it is giving me an error
Sub AllWorksheetsLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.count
' Begin the loop.
For I = 1 To WS_Count
Dim N As Long
Dim count As Long
N = Cells(Rows.count, 1).End(xlUp).Row
For count = 1 To N
v1 = Cells(count, 6).Value
If v1 = "Palm Tree (M)" Then Cells(count, 9).Value = "='Project Comp Rate Akwa Ibom'!K7"
Next count
Next I
End Sub
Kindly assists pls
It sounds like you want something like, search column F of each worksheet for "Palm Tree (M)"; if found, enter the stated formula in the cell, on the same row, 3 columns to the right. I assume that you exclude the sheet referenced in the formula from being searched.
Option Explicit
Public Sub FindThatPhrase()
Application.ScreenUpdating = False
Dim ws As Worksheet, found As Range
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Project Comp Rate Akwa Ibom" Then
Set found = GetAllMatches("Palm Tree (M)", ws.Columns("F"))
If Not found Is Nothing Then
Debug.Print ws.Name, found.Address
found.Offset(, 3) = "='Project Comp Rate Akwa Ibom'!$K$7"
End If
End If
Next ws
Application.ScreenUpdating = True
End Sub
Public Function GetAllMatches(ByVal findString As String, ByVal searchRng As Range) As Range
Dim foundCell As Range
Dim gatheredRange As Range
With searchRng
Set foundCell = searchRng.Find(findString)
Set gatheredRange = foundCell
Dim currMatch As Long
For currMatch = 1 To WorksheetFunction.CountIf(.Cells, findString)
Set foundCell = .Find(What:=findString, After:=foundCell, _
LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not gatheredRange Is Nothing Then
Set gatheredRange = Union(gatheredRange, foundCell)
Else
Set gatheredRange = foundCell
End If
Next currMatch
End With
Set GetAllMatches = gatheredRange
End Function
I am trying to parse through many different sheets and pick out specific data. I then need to place this data into specific columns and rows that depend on which column and row they are in the original sheet. In order to determine the row I need to create a range that contains the column all the way down to the current cell. I found code that explains how to do that (Select from ActiveCell to first cell in column. Data in column includes blanks), but when I run it, it throws the error message "Method "Range" not supported for Object "Worksheet"". I have already tried removing xSheet before the Range statement. Is there maybe something else wrong with my code? Thanks a lot for your help in advance!
Dim xSheet As Worksheet, DestSh As Worksheet
Dim Last As Long, crow As Long, ccol As Long
Dim copyRng As Range, destRng As Range, colSrc As Range, rowSrc As Range
Dim cRange As Range, copyTemp As Range, copyEnd As Range, copyStart As Range
Dim exchDest As Range, rowRange As Range
Dim numCol As Long, numRow As Long
Dim c As Range, q As Range
Dim uniqueVal() As Variant, x As Long
For Each xSheet In ActiveWorkbook.Worksheets
'Edit
Set copyStart = xSheet.Range("A1")
crow = xSheet.Cells(Rows.Count, 1).End(xlUp).Row
ccol = xSheet.Cells(1, Columns.Count).End(xlToRight).Column 'find a smarter way of doing this
Set copyEnd = xSheet.Cells(crow, ccol)
Set copyRng = xSheet.Range(copyStart, copyEnd)
If InStr(1, xSheet.Name, "ACCOUNT") And xSheet.Range("B1") <> "No Summary Available" Then _
For Each c In copyRng.SpecialCells(xlCellTypeVisible)
If IsNumeric(c) And Not c.Value = "0" Then _
Set rowRange = xSheet.Range(c.EntireColumn.Cells(1), c) 'problem line
For Each q In rowRange.SpecialCells(xlCellTypeVisible)
If InStr(1, q.Value, "C-") Then _
Set rowSrc = q
Next q
Set colSrc = c.EntireRow.Offset(0).Cells(1)
numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Set destination
Set destRng = DestSh.Cells(numRow, numCol)
'Copy to destination Range
c.Copy destRng
'If destRng.Column > 40 Then _
' Set destRng = destRng.Offset(1, -30)
End If
Next c
End If
Next xSheet
I have a code that's intended to:
1) Find a name from a table using a searchbox
2) Copy cells in the row with the name on to another sheet
3) This should work for all entries in the table associated with this name.
Code sample:
Sub Printout()
Dim LR2 As Long
Dim c As Variant
Dim txt As Variant
c = InputBox("Enter Last Name")
txt = CStr(c)
Sheets("B").Select
Sheets("B").Range("K3").Value = txt
Sheets("A").Select
Sheets("A").Columns(2).Find(What:=txt, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate
LR2 = Sheets("A").Cells(Rows.Count, "a").End(xlUp).Row
Sheets("A").Range(Cells(ActiveCell.Row, 2), Cells(LR2, 10)).Select
Selection.Copy Destination:=Sheets("B").Range("A2:J2")
End Sub
Problem:
Currently, the code doesn't just copy the specific name from the searchbox input, but all entries under the name as well. Ie if "Johnson" is entry 3, 6, and 11, I want columns 2 to 10 for those three rows. Currently it finds the first entry and seem to copy everything in columns 2 to 10 underneath it. Can someone please help me troubleshoot this code so that I can make it do what I want?
Thanks in advance!
this should be what youre after. Season to taste but itll do what you want
Private Sub derp()
Dim this As String
this = InputBox("Enter Last Name")
Dim rng As Range
Dim rcell As Range
Dim lastrow As Long
Dim that As Variant
lastrow = ThisWorkbook.Sheets("Sheet3").UsedRange.Rows.Count
Set rng = ThisWorkbook.Sheets("Sheet2").Range("A1:a40")
For Each rcell In rng.Cells
If rcell.Value <> vbNullString Then
If rcell.Value = this Then
that = ThisWorkbook.Sheets("Sheet2").Range("A" & rcell.Row & ":H" & rcell.Row)
ThisWorkbook.Sheets("Sheet3").Range("A" & lastrow & ":H" & lastrow).Value2 = that
lastrow = lastrow + 1
End If
End If
Next rcell
End Sub
This is my best guess
Sub Printout()
Dim LR2 As Long
Dim c As Variant
Dim txt As Variant
Dim r As Range
Dim s As String
c = InputBox("Enter Last Name")
txt = CStr(c)
Sheets("B").Range("K3").Value = txt
With Sheets("A")
Set r = .Columns(2).Find(What:=txt, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
If Not r Is Nothing Then
s = r.Address
Do
LR2 = Sheets("B").Cells(Rows.Count, "a").End(xlUp).Row
.Range(.Cells(r.Row, 2), .Cells(r.Row, 10)).Copy Destination:=Sheets("B").Range("A" & LR2)
Set r = .Columns(2).FindNext(r)
Loop While r.Address <> s
End If
End With
End Sub
It is sometimes problematic to work with code, that is not created by you. In your case, you want to select and copy the cells, which you have found in column 2.
If you take a look at this code and edit it a bit, you would be able to do it.
Option Explicit
Option Private Module
Sub Printout()
Dim txt As Variant
Dim rngUnion As Range
Dim rngCell As Range
txt = "vi"
With ActiveSheet
For Each rngCell In .Range(.Cells(1, 1), .Cells(9, 1))
If InStr(1, rngCell, txt) Then
If rngUnion Is Nothing Then
Set rngUnion = .Range(.Cells(rngCell.Row, 2), .Cells(rngCell.Row, 5))
Else
Set rngUnion = Union(rngUnion, .Range(.Cells(rngCell.Row, 2), .Cells(rngCell.Row, 5)))
End If
End If
Next rngCell
End With
rngUnion.Select
End Sub
Your ActiveSheet should like this:
What the code does:
It loops through the Cells from A1 to A9.
If it finds vi in one of those cells, it adds 4 cells of the same row to a union - rngUnion
At the end it selects a union, just to show you which one is it. You can copy the selection or copy the range, without selecting it.
Good luck, have fun!
Below is the code I wrote, which deletes the rows that contain the value "PRODUCTION" in column M
Sub DeleteProducts()
Dim LR, i As Long
Dim RNG As Range
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
LR = ws.Cells(Rows.Count, "M").End(xlUp).Row
For i = LR To 2 Step -1
Select
Case ws.Cells(i, "M").Value
Case Is <> "Production"
ws.Cells(i, "M").EntireRow.Delete shift:=xlUp
End Select
Next i
Next ws
End Sub
I need rows to be deleted in multiple sheets according to the column header because column name may change (M to something else) but the header will be the same in every sheet.
I assume that the header of the column is in the first row of each worksheet:
Sub DeleteProducts()
Dim LR as Long, LC as Long, i As Long, j As Long
Dim RNG As Range
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
LC = ws.Cells(1, Columns.Count).End(xlToRight).Column
For i = LC To 2 Step -1
If ws.Cells(1, i) = "YOURnameHERE" Then
LR = ws.Cells(Rows.Count, i).End(xlUp).Row
Exit For
End If
Next
For j = LR To 2 Step -1
If ws.Cells(j, i).Value <> "Production" Then ws.Cells(j, i).EntireRow.Delete shift:=xlUp
Next
Next ws
End Sub
This will find the name of the column, and then store in i that column's number. With that information you can then find the last row of that very column, and look for every value that is not = "Production".
I also corrected some other bits of code, just for it to be cleaner.
Her is my shot at the task. The code searches for the desired header in the first row on all sheets. If the header is found, the search for "Production" continues in the column in witch the header was found.
EDIT: Did some minor cleanup of the code.
Sub DeleteRowProduction()
Dim Header As Range
Dim FoundCell As Range
Dim ws As Worksheet
Dim HeaderToFind As String
Dim ValueToFind As String
HeaderToFind = "aaa"
ValueToFind = "Production"
For Each ws In Worksheets
Set Header = ws.Rows(1).Find(what:=HeaderToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If Not Header Is Nothing Then
Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
Do While Not FoundCell Is Nothing
ws.Rows(FoundCell.Row).Delete
Set FoundCell = Nothing
Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
Loop
End If
Next ws
End Sub
Please use Range.Find to find the target column. Modified your code below.
Sub DeleteProducts()
Dim LR, i As Long
Dim RNG As Range
Dim ws As Worksheet
Dim rngTargetColumn as range
For Each ws In ActiveWorkbook.Sheets
Set rngTargetColumn = ws.Range("1:1").Find("*<Column Heading>*") 'Replace <Column Heading> with your column header string
if not rngTargetColumn is Nothing then
LR = ws.Cells(Rows.Count, rngTargetColumn.Column).End(xlUp).Row
For i = LR To 2 Step -1
If ws.Cells(i, rngTargetColumn.Column).Value <> "Production" Then
ws.Cells(i, rngTargetColumn.Column).EntireRow.Delete shift:=xlUp
End If
Next i
Set rngTargetColumn = Nothing
End If
Next ws
End Sub