Excel VBA Coding - excel

I am new with VBA coding, could you help me towards this concern?
I am currently creating a macro which will search the specific text in the column AJ (e.g. "Chase") and if found, it will lookup for the entity from the column A and then copy paste it to other sheet.
Many thanks in advance!

Try this. Taken from the comments. Remember that the errors you might get is because of the VLookup formula. You need to look at how the VLookup formula is working.
=VLOOKUP(Value you want to look up, range where you want to lookup the value, the column number in the range containing the return value, Exact Match or Approximate Match – indicated as 0/FALSE or 1/TRUE).
The formula is searching for "Check". The errors you get is in the range you are searching in, you are searching for "AE7:A2693" that is column AE to A - it is not possible to search backwards, so it should be A2693:AE7. You have to find the correct range and change it in the code below.
Are you sure that column 31 is containing the return value?
Sub EachLoopExample()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim MyCell As Range
Dim sResult As String
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet3")
For Each MyCell In ws1.Range("AE:AE")
If MyCell.Value = "check" Then
sResult = Application.WorksheetFunction.VLookup("check", ws1.Range("B1:C3"), 1, False)
ws2.Range("A2").Value = sResult
End If
Next MyCell
End Sub

Sub EachLoop()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim FinalRow As Integer
Dim i As Integer
Set ws1 = Sheet1
Set ws2 = Sheet3
ws1.Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 7 To FinalRow
If Cells(i, 31) = "check" Then
Range(Cells(i, 1), Cells(i, 7)).Copy
ws2.Select
Range("A200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
ws1.Select
End If
Next i
ws2.Select
Range("B2").Select
Call EachLoop2
End Sub
Sub EachLoop2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim FinalRow As Integer
Dim i As Integer
Set ws1 = Sheet1
Set ws2 = Sheet3
ws1.Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 7 To FinalRow
If Cells(i, 32) = "check" Then
Range(Cells(i, 1), Cells(i, 13)).Copy
ws2.Select
Range("H200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
ws1.Select
End If
Next i
ws2.Select
Range("B2").Select
Call EachLoop2_ext
End Sub
Sub EachLoop2_ext()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim FinalRow As Integer
Dim i As Integer
Set ws1 = Sheet1
Set ws2 = Sheet3
ws2.Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To FinalRow
If Range(Cells(i, 9), Cells(i, 13)) = "" Then
ws2.Select
Range(Cells(i, 9), Cells(i, 13)).ClearContents
Range(Cells(i, 14), Cells(i, 20)).Cut
Range("I200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
Else
ws2.Select
Range(Cells(i, 9), Cells(i, 13)).ClearContents
Range(Cells(i, 14), Cells(i, 20)).Cut
Range("I200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
End If
Next i
ws2.Select
Range("I2").Select
End Sub

Related

Excel VBA - Copy data after autofiltering starting with column B

I_m trying to filter datas in a sheet with the autofilter and copy the results to another sheet. This works so far but I need the data starting with column B. I haven't found a solution so far. Hope someone can help me :)
Dim myRange As Range
lZeile = ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
MsgBox lZeile
ActiveSheet.Rows("7:7").AutoFilter Field:=12, Criteria1:="x"
On Error Resume Next
Set myRange = Range(Cells(8, 1), Cells(lZeile, 19)).SpecialCells(xlVisible)
On Error GoTo 0
If myRange Is Nothing Then
MsgBox "no cells"
Else
Intersect(Sheets("Sheet1").UsedRange, Sheets("Sheet1").UsedRange.Offset(7)).SpecialCells(xlCellTypeVisible).Copy
Worksheets("Sheet2").Activate
Worksheets("Sheet2").Cells(1, 1).PasteSpecial xlPasteValues
End If
The following code should give you your desired result - based on the headers for your data-to-copy being in row 7 of sheet 1. Let me know how it goes.
Option Explicit
Sub CopyFilterX()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim CheckRow As Long
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
With ws1.Cells(7, 1).CurrentRegion
.AutoFilter 12, "x"
CheckRow = ws1.Cells(Rows.Count, 2).End(xlUp).Row
If CheckRow = 7 Then
MsgBox "no cells"
.AutoFilter
Exit Sub
End If
.Offset(1, 1).Copy ws2.Cells(1, 1)
.AutoFilter
End With
End Sub
Replace this line:
Set myRange = Range(Cells(8, 1), Cells(lZeile, 19)).SpecialCells(xlVisible)
with this one:
Set myRange = Range(Cells(8, 2), Cells(lZeile, 19)).SpecialCells(xlVisible)

VBA Loop : Copy / Paste destination offsetting

First of all thanks for your help. I need to copy / paste datas. The idea is the next : depending on the cells content from the sheet AAA I want to copy / paste the datas to the corresponding sheet (XXX if XXX or to ZZZ if ZZZ).My macro worked but the issue is that I have an offset bothering me. Imagine , the first lap will paste the data to XXX , but the second lap will copy to ZZZ , in this case I have an issue because it will copy paste to the 3rd cells (3,1) whereas the cell(2,1) is empty
Sub CopyPastingMyDate()
Dim i As Long
Dim lrow As Long
Dim lcol As Long
Dim RngOne As Range
Dim RngTwo As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("AAA")
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
lcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set RngOne = ws.Range(ws.Cells(lrow, 1), ws.Cells(1, lcol))
For i = 2 To lrow
Set RngOne = ws.Range(ws.Cells(lrow, 1), ws.Cells(1, lcol))
If ws.Cells(i, 1) = "XXX" Then
Set RngTwo = ThisWorkbook.Worksheets("SheetXXX").Range(ThisWorkbook.Worksheets("SheetXXX").Cells(i, 1), ThisWorkbook.Worksheets("SheetXXX").Cells(i, lcol))
RngOne.Copy
RngTwo.PasteSpecial xlAll
End If
If ws.Cells(i, 1) = "ZZZ" Then
Set RngTwo = ThisWorkbook.Worksheets("SheetZZZ").Range(ThisWorkbook.Worksheets("SheetZZZ").Cells(i, 1), ThisWorkbook.Worksheets("SheetZZZ").Cells(i, lcol))
RngOne.Copy
RngTwo.PasteSpecial xlAll
End If
Next i
End Sub
How to fix it please ? I want to copy paste to from the first available cell. Thanks to all of you.
JaNa
Try this. I might be misunderstanding what you're copying though: I'm assuming each row needs to be copied to the correct sheet?
Sub CopyPastingMyDate()
Dim i As Long
Dim lrow As Long
Dim lcol As Long
Dim RngOne As Range
Dim RngTwo As Range
Dim ws As Worksheet, dest, wsDest As Worksheet
Set ws = ThisWorkbook.Worksheets("AAA")
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
lcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set RngOne = ws.Range(ws.Cells(lrow, 1), ws.Cells(1, lcol))
For i = 2 To lrow
Select Case ws.Cells(i, 1).Value 'which destination sheet?
Case "XXX": dest = "SheetXXX"
Case "ZZZ": dest = "SheetZZZ"
Case Else: dest = ""
End Select
If Len(dest) > 0 Then
ws.Cells(i, 1).Resize(1, lcol).Copy 'copy the row
Set wsDest = ThisWorkbook.Worksheets(dest)
wsDest.Cells(wsDest.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlAll
End If
Next i
End Sub
I have found a way to solve my issue by introducing a new variable x = last_row + 1 . I replaced in i in RngTwo by x .

Filter non zero value in a cell and copy values in seperate columns / sheet for a large data

I am trying to copy only values which are not zero values in a separate sheet / column. This needs to be done for large data.
I have tried the below vba-excel code, but getting stuck at for loop. Note the range in the below code is given, but in a large set of data it is unknown, so it needs to find all the rows until end.
Sub test()
Dim Lastrow As Long, i As Long
Dim reportsheet As Worksheet
Dim datasheet As Worksheet
'LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set datasheet = Sheet1
Set reportsheet = Sheet2
datasheet.Select
'finalrow = Cells(Rows.Count, 1).End(xlUp).Row
'With ThisWorkbook.Worksheets("Sheet1")
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
If Cells(i, 2).Value <> 0 Then
'Rows(i).Delete
Range(Cells(i, 1), Cells(i, 12)).Copy
reportsheet.Select
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Next i
End Sub
Sample Data -
Expected all the values at one column without zero value or #NA or blank cells. I need to plot a graph with this data. Any suggestions is appreciated. Thanks.
Try this. considering the columns have just numbers.
Sub FilterZeros()
Dim i As Long, lr As Long, lc As Long
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Sheet1")
Set sh2 = ThisWorkbook.Worksheets("Sheet2")
'Last column
lc = Cells(1, Columns.Count).End(xlToLeft).Column
'Loops between column in Sheet1 to filter valus <>0
For i = 1 To lc
sh1.Activate
Range("A1").AutoFilter Field:=i, Criteria1:="<>0"
n = Cells(Rows.Count, i).End(xlUp).Row
Range(Cells(2, i), Cells(n, i)).SpecialCells(xlCellTypeVisible).Copy
'find the last column and adds the copied data in Sheet2
sh2.Activate
lc2 = Cells(2, Columns.Count).End(xlToLeft).Column + 1
Cells(2, lc2).PasteSpecial Paste:=xlValues
sh1.Activate
Range("A1").AutoFilter
Next
End Sub
Use below the revised code without the loop to select any particular column to filter and copy the values.
Sub FilterZeros()
Dim i As Long, lr As Long, lc As Long
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Sheet1")
Set sh2 = ThisWorkbook.Worksheets("Sheet2")
'First Column to be filtered
sh1.Activate
i = "Enter your column no. 1 here"
Range("A1").AutoFilter Field:=i, Criteria1:="<>0"
n = Cells(Rows.Count, i).End(xlUp).Row
Range(Cells(2, i), Cells(n, i)).SpecialCells(xlCellTypeVisible).Copy
sh2.Activate
Range("A2").PasteSpecial xlPasteValues
'Second Column to be filtered
sh1.Activate
Range("A1").AutoFilter
i = "Enter your column no. 2 here"
Range("A1").AutoFilter Field:=i, Criteria1:="<>0"
n = Cells(Rows.Count, i).End(xlUp).Row
Range(Cells(2, i), Cells(n, i)).SpecialCells(xlCellTypeVisible).Copy
sh2.Activate
Range("B2").PasteSpecial xlPasteValues
sh1.Activate
Range("A1").AutoFilter
sh2.Activate
Range("A2").Select
End Sub
Thanks,
Hafeez

How to reference cells in range?

Based on the text ("SNV") present in column L of the "HiddenSheet" worksheet, I would like to select and copy cells in columns 1 to 6 for all rows for which the "SNV" text is present in column L.
Then I would like to paste the values of the copied cells in the SNVReports worksheet.
Sub Macro2()
a = Worksheets("HiddenSheet").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To a
If Worksheets("HiddenSheet").Cells(i, 12).Value = "SNV" Then
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Worksheets("SNVReports").Activate
b = Worksheets("SNVReports").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SNVReports").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("HiddenSheet").Activate
End If
Next
Application.CutCopyMode = False
End Sub
I sometimes receive:
"Application-defined or object-defined error"
and it is apparently related to my range:
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Your Cells(i,#) references aren't qualified. So if the SNVReports tab is active when the macro runs, it's confused as to what range you're talking about.
The whole code could do with a tidy-up:
Sub Macro2a()
Dim sourcesheet As Worksheet
Dim destsheet As Worksheet
Dim lastsourcerow as Long
Dim lastdestrow as Long
Dim i as Long
Set sourcesheet = Worksheets("HiddenSheet")
Set destsheet = Worksheets("SNVReports")
With sourcesheet
lastsourcerow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To lastsourcerow
If .Cells(i, 12).Value = "SNV" Then
lastdestrow = destsheet.Cells(destsheet.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(i, 1), .Cells(i, 6)).Copy destsheet.Cells(lastdestrow + 1, 1)
End If
Next
End With
End Sub

Search a datatable with loop and copy selected rows to another sheet

I have a datatable with string values, which I want to search for a specific value which I select from a dropdown menu. All matching values should be copied to another worksheet.
My code doesn't work. I dug through lots of stuff, but I am not able to figure out the problem.
Dim datasheet As Worksheet 'data copied from
Dim reportsheet As Worksheet 'data copied to
Dim abhaengigkeit As String
Dim finalrow As Integer
Dim i As Integer 'row counter
'sets vars
Set datasheet = Tabelle1
Set reportsheet = Tabelle44
abhaengigkeit = datasheet.Range("L3").Value
'goto datasheet and search and copy
datasheet.Select
finalrow = Cells(Rows.Count, 15).End(xlUp).Row
'loop to find records
For i = 2 To finalrow
If Cells(i, 15) = abhaengigkeit Then
''Copy Soll''
Range(Cells(i, 16), Cells(i, 23)).Copy 'copy column 1 to 10
reportsheet.Select 'goto reportsheet (Aenderungsfortpflanzung)
Range("A150").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll 'paste
under last entry
datasheet.Select
End If
Next i
My datasheet (Tabelle1) is where the dropdown and the datatable is. The reportsheet is my destination sheet to copy the matching results.
Cell L3 is the Dropdown menu, my datatable loop should run through column P and copy all values which are stated in the following 8 columns.
This works
Option Explicit
Public Sub test()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim abhaengigkeit As String
Dim finalrow As Long
Dim i As Long
Set datasheet = ThisWorkbook.Worksheets("Tabelle1") '<== I have set this up as sheet names not code names
Set reportsheet = ThisWorkbook.Worksheets("Tabelle44")
abhaengigkeit = datasheet.Range("L3").Value
With datasheet
finalrow = .Cells(.Rows.Count, 15).End(xlUp).Row
Dim unionRng As Range
For i = 2 To finalrow
If Cells(i, 15) = abhaengigkeit Then '<column 0
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, .Range(.Cells(i, 16), .Cells(i, 23))) ' 'P to W
Else
Set unionRng = .Range(.Cells(i, 16), .Cells(i, 23))
End If
End If
Next i
End With
If Not unionRng Is Nothing Then
If IsEmpty(reportsheet.Range("A150").End(xlUp)) And reportsheet.Range("A150").End(xlUp).Row = 1 Then
unionRng.Copy reportsheet.Range("A1")
Else
unionRng.Copy reportsheet.Range("A150").End(xlUp).Offset(1, 0)
End If
End If
End Sub

Resources