I'm trying to compare two columns in two different WB let's say A and B which have only column each.
I'd like to msgbox a text whenever the value of cell in the column of A is also in the column of B.
I managed to put values in a variant variable and like now to compare them. I still get a 424 error at the final if statement that checks the correspondance.
Here is the code :
Option Explicit
Sub uniformisation()
Dim range1 As Variant
Dim range2 As Variant
Dim Tab1 As Variant, tab2 As Variant
Dim fichierM As Workbook
Dim fichierF As Workbook
Set fichierF = Workbooks.Open("thepath")
Set fichierMission = Workbooks.Open("thepath")
fichierF.Activate
fichierM.Activate
Dim wsF As Worksheet
Dim wsM As Worksheet
Set wsF = fichierF.Worksheets("test")
Set wsM = fichierM.Worksheets("A")
Dim C As range
Dim D As range
Set C = wsFlex.Columns(1)
Set D = wsMiss.Columns(1)
Dim TotalRows1 As Long
Dim TotalRows2 As Long
With wsF
TotalRows1 = C.Rows(Rows.Count).End(xlUp).Row
Tab1 = range(Cells(2, 1), Cells(TotalRows1, 1)).Value
MsgBox UBound(Tab1)
End With
With wsM
TotalRows2 = Rows(D.Rows.Count).End(xlUp).Row
tab2 = range(Cells(2, 2=1), Cells(TotalRows2, 1))
MsgBox UBound(tab2)
End With
For Each range1 In Tab1
For Each range2 In tab2
If range1.Value = range2.Value Then
MsgBox range1
End If
Next range2
Next range1
fichierM.Close
fichierF.Close
End Sub
Any help would be really apreciated, thanks !
you definitions are all over the place and the code is too long for what it is supposed to do. Also, you have chosen variant which is not really needed for what you want to do. Here is a shorter version that can get you started:
Sub CompareTwoColumns()
Dim rng1 As Range
Dim rng2 As Range
Dim WB1 As Workbook
Dim WB2 As Workbook
'make sure both workbooks are open
Set WB1 = Workbooks.Open("thepath1")
Set WB2 = Workbooks.Open("thepath2")
'loop through both columns and compare
For Each rng1 In WB1.Worksheets("Sheet1").UsedRange.Columns(1).Cells
For Each rng2 In WB2.Worksheets("Sheet1").UsedRange.Columns(1).Cells
If rng1.Value = rng2.Value Then
MsgBox rng1.Value
End If
Next rng2
Next rng1
End Sub
Related
I want to copy and paste a range from a fixed worksheet ("c4:c178") into a variable worksheet.
I have two dropdowns, one has a list of all the worksheet names and the other has the column number.
My hope is the user could select the worksheet name and column reference in the drop-down and then click the macro button to copy and paste the range to that reference.
Sub CopyPaste()
Dim Sheetname As String
Sheetname = ActiveSheet.Range("i3").Value
Dim Col As Long
Col = ActiveSheet.Range("i4").Value
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tracking Only")
Dim rng As Range
Set rng = ActiveSheet.Range("c4:C178")
With rng
ws.Cells(4, Col).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End Sub
I receive
"Run-time error '1004': Application-define or object-defined error"
It highlights the ws.cells code.
Break your process into steps, store the dropdown values using variables, and assign the .Value of the source range to the target range.
With ThisWorkbook.Worksheets("Tracking Only")
Dim sheetName As String
sheetName = .Range("I3").Value
Dim col As Long
col = .Range("I4").Value
Dim rng As Range
Set rng = .Range("C4:C178")
End With
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(sheetName)
With rng
ws.Cells(4, col).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
I am trying to do the following:
Check each row if "Order" column is empty in table1 from sheet1 (there is only one table in this sheet)
if it is "Order" column is empty, copy the "Notification" number from the same row AND then paste it into a new row of a table (table2) in another sheet (sheet2) under column "Notification".
if it is not empty, go to the next row in table1
I have the following code so far:
For Each TCell in Range ("Table1").ListObject.ListColumns("Order").DataBodyRange.Cells
If TCell.Value="" then
Table2.ListRows.Add AlwaysInsert:=True
Range(TCell.Row, "Notification").Copy Range("Table2") .ListObject. ListColumns ("Notification"
.DataBodyRange.End(xlDown).Offset (1,0)
End if
Next TCell
Any help would be greatly appreciated!
Thanks.
Append Table's Column to Another Table's Column
This is a basic solution that 'literally' does what is required (slow). By using object variables, it illustrates their application.
You could increase efficiency by introducing arrays, or especially by using AutoFilter.
Option Explicit
Sub AppendNotifications()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
Dim stbl As ListObject: Set stbl = sws.ListObjects("Table1")
Dim slcl As ListColumn: Set slcl = stbl.ListColumns("Order")
Dim svcl As ListColumn: Set svcl = stbl.ListColumns("Notification")
Dim scOffset As Long: scOffset = svcl.Index - slcl.Index
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet2")
Dim dtbl As ListObject: Set dtbl = dws.ListObjects("Table2")
Dim dvcl As ListColumn: Set dvcl = dtbl.ListColumns("Notification")
Dim dvCol As Long: dvCol = dvcl.Index
Dim sCell As Range
Dim dvrw As ListRow
For Each sCell In slcl.DataBodyRange
If Len(sCell.Value) = 0 Then
Set dvrw = dtbl.ListRows.Add
dvrw.Range(dvCol).Value = sCell.Offset(, scOffset).Value
End If
Next sCell
MsgBox "Notifications appended.", vbInformation
End Sub
Could try below codes:
Sub transform()
Dim cell As Range
Set rng1 = Sheet1.Range("Table1[Order]")
Set SheetTwo = ActiveWorkbook.Worksheets("Sheet2")
Set TableTwo = SheetTwo.ListObjects("Table2")
For Each cell In rng1
If Not IsEmpty(cell.Offset(0, 0).Value) Then
Dim newrow As ListRow
Set newrow = TableTwo.ListRows.Add
With newrow
.Range(1) = cell.Offset(0, 1).Value
End With
End If
Next cell
End Sub
Codes are self-explanatory.
Notes: Table2 only has a column.
I am trying to copy and paste data between two workbooks. I am using a third separate workbook where the user can indicate the copy range, the paste range and indicate if it is a cell or row that is to be copy-pasted. The layout is as follow:
Source Target Cell/Row
G29 G29 Cell
G30 G32 Cell
G31 G33 Row
For example based on the above the VBA code is supposed to copy what is in cell G29 in source workbook and paste it in G29 in target workbook and so on. I have defined the "Source" range as rng and loop through the range in order to define the target range and whether it is a cell or row that is to be copy-pasted. However, for some reason I get an error in first defining my cell_source, cell_target and cell_cellrow variables and also get errors when running the loop where I set the target cell in target workbook equal to the cell_source_input variable. I would much appreciate if anyone can help with this.
Sub transferScript()
Dim wbMain As Workbook: Set wbMain = ThisWorkbook
Dim wbMainDashboard As Worksheet: Set wbMainDashboard = wbMain.Worksheets("Dashboard")
Dim CopyLastRow As Long
Dim rng As Range: Set rng = Application.Range("Dashboard!E9:E15") 'change to E150 !!
sourceModel = wbMainDashboard.Range("FILE_SOURCE")
targetModel = wbMainDashboard.Range("FILE_TARGET")
Dim wbSource As Workbook: Set wbSource = Workbooks.Open(Filename:=sourceModel)
Dim wbTarget As Workbook: Set wbTarget = Workbooks.Open(Filename:=targetModel)
'Source workbook
Dim wsKpInput_source As Worksheet: Set wsKpInput_source = wbSource.Worksheets("INPUT (KP)")
Dim wsSCEInput_source As Worksheet: Set wsSCEInput_source = wbSource.Worksheets("INPUT (SCE)")
'Target workbook
Dim wsKpInput_target As Worksheet: Set wsKpInput_target = wbTarget.Worksheets("INPUT (KP)")
Dim wsSCEInput_target As Worksheet: Set wsSCEInput_target = wbTarget.Worksheets("INPUT (SCE)")
'Error handling
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Dim i As Integer
Dim cell_source As String
Dim cell_target As String
Dim cell_cellrow As String
Dim cell_source_input As Variant
For i = 0 To rng.Rows.Count
'Definition of source cell, target cell, and cell_row input
cell_source = rng.Cells
cell_target = rng.Cells.Offset(rowOffset:=0, columnOffset:=1)
cell_cellrow = rng.Cells.Offset(rowOffset:=0, columnOffset:=3)
cell_source_input = wsKpInput_source.Range(cell_source)
If cell_cellrow = "Cell" Then
wsKpInput_target.Range(cell_source) = cell_source_input
End If
Next
End Sub
Supposing that there is no mistake on the previous code:
Dim i As Integer
Dim cell_source As String
Dim cell_target As String
Dim cell_cellrow As String
Dim cell_source_input As Variant
For i = 0 To rng.Rows.Count
'Definition of source cell, target cell, and cell_row input
cell_source = rng.Cells
cell_target = rng.Cells.Offset(rowOffset:=0, columnOffset:=1)
cell_cellrow = rng.Cells.Offset(rowOffset:=0, columnOffset:=3)
cell_source_input = wsKpInput_source.Range(cell_source)
If cell_cellrow = "Cell" Then
wsKpInput_target.Range(cell_source) = cell_source_input
End If
Next
Should be:
Dim i As Integer
Dim cell_source As String
Dim cell_cellrow As String
Dim cell_source_input As Variant
For i = 0 To rng.Rows.Count
'Definition of source cell, target cell, and cell_row input
cell_source = rng.Cells(i,1).Value 'It seems to, but it is not clear with no sample
cell_cellrow = rng.Cells(i,1).Offset(0, 3).Value
cell_source_input = wsKpInput_source.Range(cell_source)
If cell_cellrow = "Cell" Then
wsKpInput_target.Range(cell_source) = cell_source_input
End If
Next
Hope it helps... Always be better if you provide some sample of the input and expected output. Anyhow, in the code previous to this procedure there are a few issues: sourceModel is not defined and it seems to be a Range, targetModel is not defined and it seems to be a Range, Workbooks.Open(Filename:=sourceModel) it is trying to open one file with a Filename that it is taking a Range... check them...
I'm trying to develop a simply copy+paste values where the macro will take a date that populates into a cell, searches for the date in the next sheet, and pastes the values from A2:X2 where it finds the date.
Sub Copy_PasteVal()
Dim dDate As Range
Dim shtTrack As Worksheet
Dim shtData As Worksheet
Dim c As Range
Dim DestCell As Range
Set shtdata = Sheets(“Daily Total”)
Set shtTrack = Sheets("Overall Daily Tracking")
Set dDate = shtData.Range(“A2”)
Worksheets("shtData").Range("A2:X2").Copy
With Worksheets(shtTrack).Range("a1:a1000")
Set DestCell = .Find(dDate, LookIn:=xlValues)
End With
Worksheets(“shtTrack”).Range(DestCell).PasteSpecial Paste:=xlPasteValues
End Sub
It's not compiling and I'm hoping some gurus out there can help me figure out coding in VBA!
Edit: I may have it backwards? Also, not sure if you intended to wipe out column A...
Dim dDate
Dim shtTrack As Worksheet
Dim shtData As Worksheet
Dim c As Range
Dim DestCell As Range
Set shtData = Sheets("Daily Total")
Set shtTrack = Sheets("Overall Daily Tracking")
dDate = shtData.Range("A2").Value
Set c = shtData.Range("A2:X2")
With shtTrack.Range("a1:a1000")
Set DestCell = .Find(dDate, LookIn:=xlValues)
If Not DestCell Is Nothing Then
r = DestCell.Row
Else
Exit Sub
End If
End With
With shtTrack
.Range(.Cells(r, 1), .Cells(r, 24)).Value = c.Value
End With
I have the below code which transfers all visible data from "Prepsheet" to "Contract".
The code refers to each visible section in Prepsheet, resizes the area in contract and then transfers the data.
I want to refer to specific columns within the filtered area, so that I can transfer column specific data individually. For example, I may only want to transfer the 1st and 6th columns. Please can someone assist
Public rnga As Range
Sub test()
Dim wb As Excel.Workbook
Set wb = ActiveWorkbook
Dim sourceWS As Excel.Worksheet
Set sourceWS = Prepsheet
Dim filteredDataRange As Excel.Range
Set filteredDataRange = sourceWS.AutoFilter.Range.Offset(1, 0)
Set filteredDataRange = filteredDataRange.Resize(filteredDataRange.Rows.CountLarge - 1)
Set filteredDataRange = filteredDataRange.SpecialCells(xlCellTypeVisible)
Dim destinationWS As Excel.Worksheet
Dim destinationRow As Long
destinationRow = 1
Dim area As Excel.Range
For Each area In filteredDataRange.Areas
Set rnga = area
MatchSelectionArea
Next area
End Sub
Sub MatchSelectionArea()
Dim rng As Range, cel As Range
Dim nRows As Long
Dim nCols As Long
Set cel = Contract.Range("a1048576").End(xlUp).Offset(1, 0)
nRows = rnga.Rows.Count
nCols = rnga.Columns.Count
Set rng = cel.Resize(nRows, nCols)
rng.Value = rnga.Value
End Sub
You are delving too deeply into the filtered rows and using the number of filtered rows to redefine the filtered range. You can copy straight out of a filtered range and you will only paste the visible rows.
Sub test()
Dim wb As Excel.Workbook, fdRng As Range, v As Long, vCOLs As Variant
Dim sourceWS As Worksheet, destinationWS As Worksheet
Set wb = ActiveWorkbook
Set sourceWS = wb.Worksheets("Prepsheet")
vCOLs = Array(1, 3, 5) 'columns A, C and E
With sourceWS
If .AutoFilterMode Then
With .AutoFilter.Range
With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
For v = LBound(vCOLs) To UBound(vCOLs)
.Columns(vCOLs(v)).Copy _
destination:='YOU HAVE PROVIDED NO DEFINED DESTINATION
Next v
End With
End With
End If
End With
End Sub
Sub MatchSelectionArea()
Dim rng As Range, cel As Range
Dim nRows As Long, nCols As Long
With Worksheets("Contract")
Set cel = .Range("a1048576").End(xlUp).Offset(1, 0)
nRows = rnga.Rows.Count
nCols = rnga.Columns.Count
'cannot determine what this actually does
Set rng = cel.Resize(nRows, nCols)
rng = rnga.Value
End With
End Sub