I am using Excel 2007. I try to copy Unit-price from the Excel file-2 data to the Excel file-1 when certain columns data matching from file-1 with file-2.
Thanks for the helps & guidance.
My VBA Code:
Sub mySales()
Dim LastRow As Integer, i As Integer, erow As Integer, Pipe_Class As String, Pipe_Description As String, End_Type As String, Pipe_Size As String
Dim wbk As Workbook
strPriceFile = "C:\Temp\File-2.xlsx"
LastRow = ActiveSheet.Range(“A” & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Pipe_Class = ""
Pipe_Description = ""
End_Type = ""
Pipe_Size = ""
Pipe_Class = ActiveSheet.Cells(i, 1).Value
Pipe_Description = ActiveSheet.Cells(i, 2).Value
End_Type = ActiveSheet.Cells(i, 3).Value
Pipe_Size = ActiveSheet.Cells(i, 4).Value
Set wbk = Workbooks.Open(strPriceFile)
Worksheets("SOR2").Select
If Cells(i, 1) = Pipe_Class And Cells(i, 2) = Pipe_Description And Cells(i, 3) = End_Type And Cells(i, 4) = Pipe_Size Then
Range(Cells(i, 12), Cells(i, 12)).Select
Selection.Copy
??? After Here how select my current file & paste ????????
Worksheets("SOR1").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 12).Select
ActiveSheet.Paste
ActiveWorkbook.Save
End If
Next i
ActiveWorkbook.Close
Application.CutCopyMode = False
End Sub
I haven't checked all your code, but I have refactored what you have in your question in an attempt to open the Workbook once and to assign proper objects so that you can keep track of what action is being applied to which worksheet.
Sub mySales()
Dim LastRow As Integer, i As Integer, erow As Integer
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim wbDst As Workbook
Dim wsDst As Worksheet
Dim strPriceFile As String
Set wbDst = ActiveWorkbook
Set wsDst = ActiveSheet
strPriceFile = "C:\Temp\File-2.xlsx"
Set wbSrc = Workbooks.Open(strPriceFile)
Set wsSrc = wbSrc.Worksheets("SOR2")
LastRow = wsDst.Range("A" & wsDst.Rows.Count).End(xlUp).Row
erow = LastRow + 1
For i = 2 To LastRow
If wsSrc.Cells(i, 1).Value = wsDst.Cells(i, 1).Value And _
wsSrc.Cells(i, 2).Value = wsDst.Cells(i, 2).Value And _
wsSrc.Cells(i, 3).Value = wsDst.Cells(i, 3).Value And _
wsSrc.Cells(i, 4).Value = wsDst.Cells(i, 4).Value Then
wsSrc.Cells(i, 12).Copy wsDst.Cells(erow, 12)
erow = erow + 1 ' your current code would always copies to the same row,
' but I **think** you probably want to copy to the
' next row each time
End If
Next i
wbSrc.Close
If erow > LastRow + 1 Then
wbDst.Save
End If
wbDst.Close
End Sub
The code is completely untested but, even if it doesn't work, at least it should give you an idea of how you should be processing multiple workbooks and multiple worksheets.
Related
I want to delete entire row when all 3 numeric values in cells in columns G,H,I are equal. I wrote a vba code and it does not delete nothing. can Someone advise?
Sub remove_dup()
Dim rng As Range
Dim NumRows As Long
Dim i As Long
Set rng = Range("G2", Range("G2").End(xlDown))
NumRows = Range("G2", Range("G2").End(xlDown)).Rows.Count
For i = 2 To NumRows
Cells(i, 7).Select
If Cells(i, 7).Value = Cells(i, 8).Value = Cells(i, 9).Value Then
EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Next i
End Sub
Try this code. When deleting rows, always start from last row and work towards first one. That way you are sure you wont skip any row.
Sub remove_dup()
Dim rng As Range
Dim NumRows As Long
Dim i As Long
NumRows = Range("G2", Range("G2").End(xlDown)).Rows.Count
For i = NumRows + 1 To 2 Step -1
If Cells(i, 7).Value = Cells(i, 8).Value And Cells(i, 7).Value = Cells(i, 9).Value Then
Cells(i, 7).EntireRow.Delete
Else
End If
Next i
End Sub
Remember when you delete rows, all you need to loop in reverse order.
Please give this a try...
Sub remove_dup()
Dim NumRows As Long
Dim i As Long
NumRows = Cells(Rows.Count, "G").End(xlUp).Row
For i = NumRows To 2 Step -1
If Application.CountIf(Range(Cells(i, 7), Cells(i, 9)), Cells(i, 7)) = 3 Then
Rows(i).Delete
End If
Next i
End Sub
You can delete all rows together using UNION. Try this
Sub remove_dup()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim cel As Range, rng As Range
Set ws = ThisWorkbook.Sheets("Sheet4") 'change Sheet3 to your data range
With ws
lastRow = .Cells(.Rows.Count, "G").End(xlUp).Row 'last row with data in Column G
For i = lastRow To 2 Step -1 'loop from bottom to top
If .Range("G" & i).Value = .Range("H" & i).Value And .Range("G" & i).Value = .Range("I" & i).Value Then
If rng Is Nothing Then 'put cell in a range
Set rng = .Range("G" & i)
Else
Set rng = Union(rng, .Range("G" & i))
End If
End If
Next i
End With
rng.EntireRow.Delete 'delete all rows together
End Sub
I am trying to write a vba script that will filter on two columns, column A and column D. Preferably, I want to create a button that will execute once I have chosen the filter criteria. Sample of input data below.
Sub Compiler()
Dim i
Dim LastRow As Integer
LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet4").Range("A2:J6768").ClearContents
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Values = Sheets("Sheet3").Cells(3, "B").Values And Sheets("Sheet1").Cells(i, "D").Values = Sheets("Sheet3").Cells(3, "D").Values Then
Sheets("Sheet1").Cells(i, "A" & "D").EntireRow.Copy Destination:=Sheets("Sheet4").Range("A" + Rows.Count).End(xlUp)
End If
Next i
End Sub
Sample Data to run vba script
I have included my previous answer's changes into the full code block that is now provided below.
Sub Compiler()
Dim i
Dim LastRow, Pasterow As Integer
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Sheet4")
LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet4").Range("A2:J6768").ClearContents
For i = 2 To LastRow
If Sheets("Sheet1").Range("A" & i).Value = Sheets("Sheet3").Range("B3").Value And Sheets("Sheet1").Range("D" & i).Value = Sheets("Sheet3").Range("D3").Value Then
Pasterow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row + 1
Sheets("Sheet1").Rows(i).EntireRow.Copy Destination:=Sheets("Sheet4").Range("A" & Pasterow)
End If
Next i
Sheets("sheet4").Rows(1).Delete
End Sub
Sheets("Sheet1").Cells(i, "A").Values
Sheets("Sheet3").Cells(3, "B").Values
etc
You keep using values. Don't you mean value?
This answered the question I was asking, I tried to work with Dan's answer but didn't get very far.
Private Sub CommandButton1_Click()
FinalRow = Sheets("Sheet1").Cells(rows.Count, 1).End(xlUp).Row
Sheets("Sheet4").Range(Sheets("Sheet4").Cells(1, "A"), Sheets("Sheet4").Cells(FinalRow, "K")).ClearContents
If Sheets("Sheet4").Cells(1, "A").Value = "" Then
Sheets("Sheet1").Range("A1:K1").Copy
Sheets("Sheet4").Range(Sheets("Sheet4").Cells(1, "A"), Sheets("Sheet4").Cells(1, "K")).PasteSpecial (xlPasteValues)
End If
For x = 2 To FinalRow
ThisValue = Sheets("Sheet1").Cells(x, "A").Value
ThatValue = Sheets("Sheet1").Cells(x, "D").Value
If ThisValue = Sheets("Sheet3").Cells(3, "B").Value And ThatValue = Sheets("Sheet3").Cells(3, "D").Value Then
Sheets("Sheet1").Range(Sheets("Sheet1").Cells(x, 1), Sheets("Sheet1").Cells(x, 11)).Copy
Sheets("Sheet4").Select
NextRow = Sheets("Sheet4").Cells(rows.Count, 1).End(xlUp).Row + 1
With Sheets("Sheet4").Range(Sheets("Sheet4").Cells(NextRow, 1), Sheets("Sheet4").Cells(NextRow, 11))
.PasteSpecial (xlPasteFormats)
.PasteSpecial (xlPasteValues)
End With
End If
Next x
Worksheets("Sheet4").Cells.EntireColumn.AutoFit
End Sub
i have an error in the loop FOR. I don't understand why.My purpose is to activate the option "automatic calculation" then delete all old rows and finally add new ones.
Sub refresh()
'
' refresh Macro
'
' Touche de raccourci du clavier: Ctrl+y
'
Dim LastRow As Integer, i As Integer
Application.Calculation = xlAutomatic
Range("A6:AP1000").Select
Application.DisplayAlerts = False
Selection.Delete
Application.DisplayAlerts = True
Range("A6:AP1000").Select
Selection.ClearContents
Sheets("PTR").Range(“A” & Rows.Count).Select
For i = 2 To Sheets("PTR").Range(“A” & Rows.Count).End(xlUp).Row
If Cells(i, 1) = "X" Then
Range(Cells(i, 1), Cells(i, 20)).Select
Selection.Copy
Sheets("Analyse de risque").Range("B" & Rows.Count).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End If
Next i
End Sub
Dim LastRow1 As Long
Dim LastRow2 As Long
Dim i As Integer
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Set WS1 = Worksheets("PTR")
LastRow1 = WS1.Cells(1048576, 1).End(xlUp).Row ' COLUMN 1 ?????????
Set WS2 = Worksheets("Analyse de risque")
LastRow2 = WS2.Cells(1048576, 1).End(xlUp).Row ' COLUMN 1 ?????
For i = 2 To LastRow1
If Cells(i, 1) = "X" Then
Range(Cells(i, 1), Cells(i, 20)).Copy
WS2.Cells(LastRow2, 1).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
LastRow2 = LastRow2 + 1
End If
Next i
I have two Workbooks where I need to compare ID's from the one where I write the script and to other one I need to access from the script.
I loop through all numbers in sheet and record each ID in "tmpFisNo". After that I make one loop for iterating through other Workbook's rows and compare them.
However, it says "Mismatch Error" in line For j = ws.Cells(9, 9).Value To ws.Cells(10, 9).Value
Dim i As Integer
Dim j As Integer
Dim tmpFisNo As String
Dim tmpFisNo2 As String
Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim wbPath As String
Dim ws As Worksheet
Sub Dogrula_Click()
wbPath = Cells(8, 9).Value
Set wbThis = ActiveWorkbook
Set wbTarget = Workbooks.Open("2015_OCAK_MUTABAKAT_RAPORU")
Set ws = wbTarget.Sheets(2)
For i = Cells(5, 9).Value To Cells(6, 9).Value
tmpFisNo = Cells(i, 2).Text
For j = ws.Cells(9, 9).Value To ws.Cells(10, 9).Value
tmpFisNo2 = ws.Cells(j, 4).Text
If tmpFisNo = tmpFisNo2 Then
Cells(i, 7).Value = 1
End If
Next j
Next i
End Sub
Try
Dim i As Long
Dim j As Long
Dim tmpFisNo As String
Dim tmpFisNo2 As String
Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim wbPath As String
Dim ws As Worksheet
Sub Dogrula_Click()
wbPath = Cells(8, 9).Value
Set wbThis = ActiveWorkbook
Set wbTarget = Workbooks.Open("2015_OCAK_MUTABAKAT_RAPORU")
Set ws = wbTarget.Sheets(2)
For i = Cells(5, 9).Value To Cells(6, 9).Value
tmpFisNo = Cells(i, 2).Text
dim a as Long, b as Long
a = CLng(ws.Cells(9, 9).Value)
b = CLng(ws.Cells(10, 9).Value)
For j = a To b
tmpFisNo2 = ws.Cells(j, 4).Text
If tmpFisNo = tmpFisNo2 Then
Cells(i, 7).Value = 1
End If
Next j
Next i
End Sub
If it gives you an error on the CLngs then it's because you have some values in the spreadsheet in cells (9,9) or (10,9) which aren't integers
I would also recommend using Long instead of Integer - you can't reach all the cells in the worksheet using Integer (even for the old versions of Excel)
I have sheet 1 (formatted as table) and sheet 2 (formatted as table). I want to copy the active row from from sheet 1 to sheet 2. I have tried the below and its works well for normal range but not for sheets formatted as table.
Private Sub CommandButton1_Click()
Dim lastrow As Long
With ThisWorkbook.Worksheets("Sheet2")
lastrow = Application.Max(4, .Cells(.Rows.Count, "B").End(xlUp).Row + 1)
.Range("B" & lastrow).Resize(, 5).Value = _
Range("A" & ActiveCell.Row).Resize(, 5).Value
End With
End Sub
UPDATE:
Private Sub CommandButton1_Click()
Dim tbl As ListObject
Dim tblRow As ListRow
Dim lastRow As Long
If UCase(Range("F" & ActiveCell.Row)) <> "YES" Then Exit Sub
With ThisWorkbook.Worksheets("Sheet3")
'change Sheet3 to destination sheet - where you need to paste values
If Not IsError(Application.Match(Range("B" & ActiveCell.Row), .Range("B:B"), 0)) Then Exit Sub
Set tbl = .ListObjects(1)
If tbl.Range(tbl.Range.Rows.Count, "B") = "" Then
lastRow = Application.Min(tbl.Range(tbl.Range.Rows.Count, "B").End(xlUp).Row + 1, _
Application.Max(4, .Cells(.Rows.Count, "B").End(xlUp).Row + 1))
Else
lastRow = tbl.ListRows.add.Range.Row
End If
End With
tbl.Range(lastRow, "B").Resize(, 5).Value = _
Range("A" & ActiveCell.Row).Resize(, 5).Value
End Sub