VBA how to find a particular column name from base workbook into another workbook - excel

What i am tryin to do is Copy Data from another workbook into this workbook based on column header names, columns are not in the same sequece and not all columns headers are present on whiuch i hv used on error resume next
i used find function to do it, is there ant other way i can do it.
how can i replace find what in "" with a dynamic range or cell reference
here i is the column from base file which i wanty to find in dump wb
Below is my conding
For i = 1 To 50
Windows("Base.xlsm").Activate
Columns(Columns(i).Address).Select 'i is column number
ActiveSheet.Cells(8, i).Select ' this is required column hader to find
Application.CutCopyMode = False
Selection.copy
Windows("Dump.xlsx").Activate
Rows("2:2").Select
Selection.Find(What:="items", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range(ActiveCell, Cells(ActiveCell.Row + 800000, ActiveCell.Column)).Select
Application.CutCopyMode = False
Selection.copy
Windows("Base.xlsm").Activate
Range("A9").Select 'how to select active cell in the workbook where i want to paste data.
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i

I understand about 10% of your question. but I'll answer:
how can i replace find what in "" with a dynamic range
you can replace Find(What:="items") with a value of a range, e.g. Find(What:=Range("A1").Value), leaving you with this:
Selection.Find(What:=Range("A1").Value, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate

Related

Increase Decrease Values from One Sheet to Another Sheet VBA

I have one sheet named Sheet3 and another are Sheet4. Sheet3: Column A Header is Product type, and Column B is their Quantity, Sheet4 has same column Header. Product Type and Quantity. But when I Run Below Macro in Sheet3, Sometime they add values correctly to
Sheet4 and sometime they doesn't work properly.
Sub Increase_Value()
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A2").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A2").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A3").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A3").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A4").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet4").Select
Range("A2:B4").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A2").Select
ActiveWorkbook.Save
End Sub
For Example: If Sheet3 Range A3 = **Coca Cola**, And B3 = **20**, And When I rum the macro Increase_Value(), The VBA should find the value Coca Cola in Sheet4 Column A and if value found in row 10 (A10) then add value 20 from sheet3 Range B3 to sheet4 Range B10.
If B10 is 47 then after running the macro it should be 67.
Looks like you are trying to do something that a vlookup could solve easily? You can do vlookups in VBA as well
Below is the exact formula -
'''Application.WorksheetFunction.vlookup(lookup_value, table_array, col_index_num, range_lookup)'''
You can find more information on this here:
https://excelmacromastery.com/vba-vlookup/
Thanks,

VBA delete cell below last pasted range

Sheets("MDCF").Select
Range("B6:B100").Select
Selection.Copy
Range("J6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Replace What:="10000", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("J6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("MDCF Fund").Select
Range("F7").Select
ActiveSheet.Paste
With Sheets("MDCF Fund")
Set FOUNDRANGE = .Columns("F:F").Find("*", After:=.Range("F81"), searchdirection:=xlPrevious,
LookIn:=xlValues)
If Not FOUNDRANGE Is Nothing Then LR1 = FOUNDRANGE.Row
End With
Range("F80:F" & LR1 + 1).Select
Selection.Clear
I am trying to delete the cells that are below last cell in new sheet but its not working if the cell range is same in both copy paste ranges. trying to find a way in which I will be able to delete only redundant cell that are above F80 and below the copied cell counts.
If you want to straight away delete all the data after F80 then the below code would work,
With Sheets("MDCF Fund")
LR1 = Range("F" & Rows.count).end(xlup).row
'If you want to keep the cells between the pasted data and F80 then just add the IF condition
If (LR1 > 80) Then
Range("F80:F" & LR1).Select
Selection.clear
end if

run time error 91 : Object Variable or With block variable not set in excel 2013

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

Find specific text but not set a specific cell reference

I am writing an Excel macro that needs to find specific text Client Remittance Details and then select and cut to the end of the sheet and then paste on another tab. The text can be in on a different row for each different workbook. The macro always writes a specific cell reference so it errors on the next file. Here is the section of the macro that seems to be the error.
Cells.Find(What:="Client Remittance Details", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A12").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Cut
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
Your Range("A12").Select is ruining your find. This:
Sub luxation()
Dim r1 As Range, rCopy As Range, rPaste As Range
Set r1 = Cells.Find(What:="Client Remittance Details", After:=Cells(1, 1), LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
Set rCopy = Range(r1, Cells.SpecialCells(xlCellTypeLastCell))
Sheets.Add After:=ActiveSheet
Set rPaste = Range("A1")
rCopy.Copy rPaste
End Sub
This sets rPaste to cell A1 on the newly added sheet.

Search, Copy, Insert row, Paste and Change Value

I am trying to find a value "PLGDY", copy data from this row, insert a new row above the one found, paste the data into new row, replace value "PLGDY" with "PLGDN".
I wrote a macro that instead of copying data into new row it pastes into cells to the right. It also changes values in both rows to "PLGDN".
I would like to use For Next loop, because I have plenty of values to change. Is it possible to check how many values to change? I would like to use this number as counter.
Sub Find_and_Change()
'
'Find a "PLGDY" and set an active cell
Cells.Find(What:="PLGDY", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'select a block of data in a row
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
'copy selected block of data
Selection.Copy
'insert a row above active cell
ActiveSheet.Cells(ActiveCell.Row, 1).Select
ActiveCell.EntireRow.Insert
'set an active cell at the beginig of a row and move into column A
ActiveSheet.Cells(ActiveCell.Row, 1).Select
' paste copied data into this cell
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Now I select whole row
ActiveCell.EntireRow.Select
'I need to replace PLGDY with PLGDN in this row
Selection.Replace What:="PLGDY", Replacement:="PLGDN", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'I need to move active cell 10 columns right and one row down because I want to find next PLGDY
ActiveCell.Offset(1, 10).Select
End Sub
mrbungle's Answer is spot on! Works great.
For anyone finding this and intends to use this code to duplicate rows with multiple values, there is one tweak to be made.
I was able to copy/paste the loop and change the variables to suit, only exception was I needed to add ActiveCell.EntireRow.Selectafter the ActiveCell.EntireRow.Insert otherwise when the second loop came through with the new values it replaced the original value as well as the new row was not selected. Updated code for my purposes is:
Private Sub LT2V()
Dim vCount As Integer
'Add Lesser tier of 2V
vCount = Application.WorksheetFunction.CountIf(Range("D:D"), "2V")
Do Until vCount = 0
Cells.Find(What:="2V", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert
ActiveCell.EntireRow.Select
Selection.Replace What:="2V", Replacement:="1V", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Offset(1, 10).Select
vCount = vCount - 1
Loop
End Sub
You being new I understand you might not know all the shortcuts and built in functions. I still learn new ones all the time. In this case I used the built in worksheet function CountIf to get the number of times the values appear. Then to loop through I like to use Do Until Loop and just subtract 1 through each loop until I reach 0.
Sub Find_and_Change()
vCount = Application.WorksheetFunction.CountIf(Range("A1:Z100"), "PLGDY")
Do Until vCount = 0
Cells.Find(What:="PLGDY", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert
Selection.Replace What:="PLGDY", Replacement:="PLGDN", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Offset(1, 10).Select
vCount = vCount - 1
Loop
End Sub

Resources