I'm trying to get a macro in Excel working.
Right now I have a worksheet called "Forms" with 3 columns - the headings (in row 1) are A = Form Number, B = Form Name, C = Parts I also have a worksheet called Ins, which has the same exact headings and is populated with the information already.
I'm trying to get it so that I can enter in the form numbers on "Forms" in column A and have the information from Ins automatically copy over for columns B and C. I have EntireRow in the code right now, but I would prefer it if I could have it specifically only copy to column A to C, but I can't think of how.
Here is the code I'm currently trying to use:
Private Sub Auto()
Application.ScreenUpdating = False
Dim wks1 As Worksheet, wks2 As Worksheet
Dim j As Integer
Dim i As Integer
Set wks1 = Sheets("Forms")
Set wks2 = Sheets("Ins")
lastline = wks1.UsedRange.Rows.Count
For i = 2 To lastline
wks2.Cells(1, 1).CurrentRegion.AutoFilter
wks2.Cells(1, 1).CurrentRegion.AutoFilter 1, wks1.Cells(i, 1).Value
wks2.Cells(1, 1).CurrentRegion.EntireRow.Copy wks1.Cells(i, 1)
wks2.Cells(1, 1).CurrentRegion.AutoFilter
Next i
End Sub
wks2.Cells(1, 1).CurrentRegion.Resize(,3).Copy wks1.Cells(i, 1)
EDIT: something like this would be better I think
Private Sub Auto()
Application.ScreenUpdating = False
Dim wks1 As Worksheet, wks2 As Worksheet
Dim f As Range, frmNum
Dim lastLine As Long
Dim j As Integer
Dim i As Integer
Set wks1 = Sheets("Forms")
Set wks2 = Sheets("Ins")
lastLine = wks1.UsedRange.Rows.Count
For i = 2 To lastLine
frmNum = wks1.Cells(i, 4).Value
If Len(frmNum) > 0 Then
Set f = wks2.Columns(1).Find(frmNum, LookIn:=xlValues, lookat:=xlWhole)
If Not f Is Nothing Then
f.Offset(0, 1).Resize(1, 9).Copy wks1.Cells(i, 5)
Else
wks1.Cells(i, 5).Value = "??"
End If
End If
Next i
End Sub
Here is more on what i mean in my comment, If you just want what you asked for it can be accomplished using formulas:
The formula would be:
B2 = =IF(A2<>"",VLOOKUP(A2,Ins!$A$1:$C$14,2,FALSE),"")
and
C2 = =IF(A2<>"",VLOOKUP(A2,Ins!$A$1:$C$14,3,FALSE),"")
If you Ins worksheet looks like this:
Then you Forms worksheet will look like this after the formulas are dragged down:
I ended up getting this to work by adding a third workbook and entering the form numbers in column A there!
Private Sub Auto()
Application.ScreenUpdating = False
Dim wks1 As Worksheet, wks2 As Worksheet
Dim j As Integer
Dim i As Integer
Set wks1 = Sheets("Form Worksheet")
Set wks2 = Sheets("Instructions")
Set wks3 = Sheets("To Do")
lastline = wks1.UsedRange.Rows.Count
For i = 2 To lastline
wks2.Cells(2, 1).CurrentRegion.AutoFilter
wks2.Cells(2, 1).CurrentRegion.AutoFilter 1, wks3.Cells(i, 1).Value
wks2.Cells(2, 1).CurrentRegion.Offset(1).Resize(, 10).Copy
wks1.Cells(i, 4).PasteSpecial Paste:=xlPasteValues
wks2.Cells(2, 1).CurrentRegion.AutoFilter
Next i
End Sub
But I ended up using Tim's version.
Thanks guys!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.