So I am new to Excel/VBA and was advised to try and take on a stretch task and automate a process. I need to grab every Ticket ID on sheet 1 (one at a time) and check if it's in the second sheet. Then same process for sheet2 comparing sheet1. Ticket IDs are on column 2 of the sheets. If I don't find the Ticket ID on the other sheet, I need to add that entire row of the ticket ID to the third sheet.
Dim i As Integer, TicketId As Range, CellId As Range
Dim SearchRange1 As Range, SearchRange2 As Range
Dim rowCount1 As Integer, rowCount2 As Integer, x As Integer
x = 1
'Get row counts from both sheets and set search ranges##
Sheets("Sheet1").Select
rowCount1 = Range("A2", Range("A2").End(xlDown)).Count
Set SearchRange1 = Range("B2", Range("B2").End(xlDown))
Sheets("Sheet2").Select
rowCount2 = Range("A2", Range("A2").End(xlDown)).Count
Set SearchRange2 = Range("B2", Range("B2").End(xlDown))
'loop to compare differences in sheet1 to sheet2 and add to third sheet##
For i = 2 To rowCount1
Sheets("Sheet1").Select
Set TicketId = Cells(i, 2)
Set CellId = SearchRange2.Find(What:=TicketId)
If CellId Is Nothing Then
Rows(i).Select
Selection.Copy
Sheets("Sheet3").Select
Cells(x, 1).Select
Cells(x, 1).PasteSpecial
Rows(x).Select
Selection.Columns.AutoFit
'Cells(x, 1).Value = TicketId.Value##
x = x + 1
End If
Next i
'Sheets("Sheet2").Select##
Application.CutCopyMode = False
My issue is that when I get the data into the third sheet, I can't get the columns to fit correctly and I can't see all the data. I researched and found the AutoFit command but I don't know how to implement it in my code. I've seen other implementations of AutoFit but the coder always knows the range he's using specifically. I don't know the row I'm putting the data on. I've included one of the many implementations I've tried: selecting the row, and autofitting my selection. Suggestions? As a side note, my pasting method is also the only one that would work for me, I attempted to use pastespecial with :=xlPasteValues and :=xlPasteFormats but it didn't work either.
Rows(x).Select |Selection.Columns.AutoFit
you select row then tries to autofit column.... which is it??? Edit: also, Batman will link you to avoid usingselect
. – findwindow